Path: blob/master/modules/godot_physics_2d/godot_collision_object_2d.cpp
10277 views
/**************************************************************************/1/* godot_collision_object_2d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "godot_collision_object_2d.h"31#include "godot_physics_server_2d.h"32#include "godot_space_2d.h"3334void GodotCollisionObject2D::add_shape(GodotShape2D *p_shape, const Transform2D &p_transform, bool p_disabled) {35Shape s;36s.shape = p_shape;37s.xform = p_transform;38s.xform_inv = s.xform.affine_inverse();39s.bpid = 0; //needs update40s.disabled = p_disabled;41s.one_way_collision = false;42s.one_way_collision_margin = 0;43shapes.push_back(s);44p_shape->add_owner(this);4546if (!pending_shape_update_list.in_list()) {47GodotPhysicsServer2D::godot_singleton->pending_shape_update_list.add(&pending_shape_update_list);48}49}5051void GodotCollisionObject2D::set_shape(int p_index, GodotShape2D *p_shape) {52ERR_FAIL_INDEX(p_index, shapes.size());53shapes[p_index].shape->remove_owner(this);54shapes.write[p_index].shape = p_shape;5556p_shape->add_owner(this);5758if (!pending_shape_update_list.in_list()) {59GodotPhysicsServer2D::godot_singleton->pending_shape_update_list.add(&pending_shape_update_list);60}61}6263void GodotCollisionObject2D::set_shape_transform(int p_index, const Transform2D &p_transform) {64ERR_FAIL_INDEX(p_index, shapes.size());6566shapes.write[p_index].xform = p_transform;67shapes.write[p_index].xform_inv = p_transform.affine_inverse();6869if (!pending_shape_update_list.in_list()) {70GodotPhysicsServer2D::godot_singleton->pending_shape_update_list.add(&pending_shape_update_list);71}72}7374void GodotCollisionObject2D::set_shape_disabled(int p_idx, bool p_disabled) {75ERR_FAIL_INDEX(p_idx, shapes.size());7677GodotCollisionObject2D::Shape &shape = shapes.write[p_idx];78if (shape.disabled == p_disabled) {79return;80}8182shape.disabled = p_disabled;8384if (!space) {85return;86}8788if (p_disabled && shape.bpid != 0) {89space->get_broadphase()->remove(shape.bpid);90shape.bpid = 0;91if (!pending_shape_update_list.in_list()) {92GodotPhysicsServer2D::godot_singleton->pending_shape_update_list.add(&pending_shape_update_list);93}94} else if (!p_disabled && shape.bpid == 0) {95if (!pending_shape_update_list.in_list()) {96GodotPhysicsServer2D::godot_singleton->pending_shape_update_list.add(&pending_shape_update_list);97}98}99}100101void GodotCollisionObject2D::remove_shape(GodotShape2D *p_shape) {102//remove a shape, all the times it appears103for (int i = 0; i < shapes.size(); i++) {104if (shapes[i].shape == p_shape) {105remove_shape(i);106i--;107}108}109}110111void GodotCollisionObject2D::remove_shape(int p_index) {112//remove anything from shape to be erased to end, so subindices don't change113ERR_FAIL_INDEX(p_index, shapes.size());114for (int i = p_index; i < shapes.size(); i++) {115if (shapes[i].bpid == 0) {116continue;117}118//should never get here with a null owner119space->get_broadphase()->remove(shapes[i].bpid);120shapes.write[i].bpid = 0;121}122shapes[p_index].shape->remove_owner(this);123shapes.remove_at(p_index);124125if (!pending_shape_update_list.in_list()) {126GodotPhysicsServer2D::godot_singleton->pending_shape_update_list.add(&pending_shape_update_list);127}128// _update_shapes();129// _shapes_changed();130}131132void GodotCollisionObject2D::_set_static(bool p_static) {133if (_static == p_static) {134return;135}136_static = p_static;137138if (!space) {139return;140}141for (int i = 0; i < get_shape_count(); i++) {142const Shape &s = shapes[i];143if (s.bpid > 0) {144space->get_broadphase()->set_static(s.bpid, _static);145}146}147}148149void GodotCollisionObject2D::_unregister_shapes() {150for (int i = 0; i < shapes.size(); i++) {151Shape &s = shapes.write[i];152if (s.bpid > 0) {153space->get_broadphase()->remove(s.bpid);154s.bpid = 0;155}156}157}158159void GodotCollisionObject2D::_update_shapes() {160if (!space) {161return;162}163164for (int i = 0; i < shapes.size(); i++) {165Shape &s = shapes.write[i];166if (s.disabled) {167continue;168}169170//not quite correct, should compute the next matrix..171Rect2 shape_aabb = s.shape->get_aabb();172Transform2D xform = transform * s.xform;173shape_aabb = xform.xform(shape_aabb);174shape_aabb.grow_by((s.aabb_cache.size.x + s.aabb_cache.size.y) * 0.5 * 0.05);175s.aabb_cache = shape_aabb;176177if (s.bpid == 0) {178s.bpid = space->get_broadphase()->create(this, i, shape_aabb, _static);179space->get_broadphase()->set_static(s.bpid, _static);180}181182space->get_broadphase()->move(s.bpid, shape_aabb);183}184}185186void GodotCollisionObject2D::_update_shapes_with_motion(const Vector2 &p_motion) {187if (!space) {188return;189}190191for (int i = 0; i < shapes.size(); i++) {192Shape &s = shapes.write[i];193if (s.disabled) {194continue;195}196197//not quite correct, should compute the next matrix..198Rect2 shape_aabb = s.shape->get_aabb();199Transform2D xform = transform * s.xform;200shape_aabb = xform.xform(shape_aabb);201shape_aabb = shape_aabb.merge(Rect2(shape_aabb.position + p_motion, shape_aabb.size)); //use motion202s.aabb_cache = shape_aabb;203204if (s.bpid == 0) {205s.bpid = space->get_broadphase()->create(this, i, shape_aabb, _static);206space->get_broadphase()->set_static(s.bpid, _static);207}208209space->get_broadphase()->move(s.bpid, shape_aabb);210}211}212213void GodotCollisionObject2D::_set_space(GodotSpace2D *p_space) {214GodotSpace2D *old_space = space;215space = p_space;216217if (old_space) {218old_space->remove_object(this);219220for (int i = 0; i < shapes.size(); i++) {221Shape &s = shapes.write[i];222if (s.bpid) {223old_space->get_broadphase()->remove(s.bpid);224s.bpid = 0;225}226}227}228229if (space) {230space->add_object(this);231_update_shapes();232}233}234235void GodotCollisionObject2D::_shape_changed() {236_update_shapes();237_shapes_changed();238}239240GodotCollisionObject2D::GodotCollisionObject2D(Type p_type) :241pending_shape_update_list(this) {242type = p_type;243}244245246