Path: blob/master/modules/godot_physics_3d/godot_collision_object_3d.cpp
10277 views
/**************************************************************************/1/* godot_collision_object_3d.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_3d.h"3132#include "godot_physics_server_3d.h"33#include "godot_space_3d.h"3435void GodotCollisionObject3D::add_shape(GodotShape3D *p_shape, const Transform3D &p_transform, bool p_disabled) {36Shape s;37s.shape = p_shape;38s.xform = p_transform;39s.xform_inv = s.xform.affine_inverse();40s.bpid = 0; //needs update41s.disabled = p_disabled;42shapes.push_back(s);43p_shape->add_owner(this);4445if (!pending_shape_update_list.in_list()) {46GodotPhysicsServer3D::godot_singleton->pending_shape_update_list.add(&pending_shape_update_list);47}48}4950void GodotCollisionObject3D::set_shape(int p_index, GodotShape3D *p_shape) {51ERR_FAIL_INDEX(p_index, shapes.size());52shapes[p_index].shape->remove_owner(this);53shapes.write[p_index].shape = p_shape;5455p_shape->add_owner(this);56if (!pending_shape_update_list.in_list()) {57GodotPhysicsServer3D::godot_singleton->pending_shape_update_list.add(&pending_shape_update_list);58}59}6061void GodotCollisionObject3D::set_shape_transform(int p_index, const Transform3D &p_transform) {62ERR_FAIL_INDEX(p_index, shapes.size());6364shapes.write[p_index].xform = p_transform;65shapes.write[p_index].xform_inv = p_transform.affine_inverse();66if (!pending_shape_update_list.in_list()) {67GodotPhysicsServer3D::godot_singleton->pending_shape_update_list.add(&pending_shape_update_list);68}69}7071void GodotCollisionObject3D::set_shape_disabled(int p_idx, bool p_disabled) {72ERR_FAIL_INDEX(p_idx, shapes.size());7374GodotCollisionObject3D::Shape &shape = shapes.write[p_idx];75if (shape.disabled == p_disabled) {76return;77}7879shape.disabled = p_disabled;8081if (!space) {82return;83}8485if (p_disabled && shape.bpid != 0) {86space->get_broadphase()->remove(shape.bpid);87shape.bpid = 0;88if (!pending_shape_update_list.in_list()) {89GodotPhysicsServer3D::godot_singleton->pending_shape_update_list.add(&pending_shape_update_list);90}91} else if (!p_disabled && shape.bpid == 0) {92if (!pending_shape_update_list.in_list()) {93GodotPhysicsServer3D::godot_singleton->pending_shape_update_list.add(&pending_shape_update_list);94}95}96}9798void GodotCollisionObject3D::remove_shape(GodotShape3D *p_shape) {99//remove a shape, all the times it appears100for (int i = 0; i < shapes.size(); i++) {101if (shapes[i].shape == p_shape) {102remove_shape(i);103i--;104}105}106}107108void GodotCollisionObject3D::remove_shape(int p_index) {109//remove anything from shape to be erased to end, so subindices don't change110ERR_FAIL_INDEX(p_index, shapes.size());111for (int i = p_index; i < shapes.size(); i++) {112if (shapes[i].bpid == 0) {113continue;114}115//should never get here with a null owner116space->get_broadphase()->remove(shapes[i].bpid);117shapes.write[i].bpid = 0;118}119shapes[p_index].shape->remove_owner(this);120shapes.remove_at(p_index);121122if (!pending_shape_update_list.in_list()) {123GodotPhysicsServer3D::godot_singleton->pending_shape_update_list.add(&pending_shape_update_list);124}125}126127void GodotCollisionObject3D::_set_static(bool p_static) {128if (_static == p_static) {129return;130}131_static = p_static;132133if (!space) {134return;135}136for (int i = 0; i < get_shape_count(); i++) {137const Shape &s = shapes[i];138if (s.bpid > 0) {139space->get_broadphase()->set_static(s.bpid, _static);140}141}142}143144void GodotCollisionObject3D::_unregister_shapes() {145for (int i = 0; i < shapes.size(); i++) {146Shape &s = shapes.write[i];147if (s.bpid > 0) {148space->get_broadphase()->remove(s.bpid);149s.bpid = 0;150}151}152}153154void GodotCollisionObject3D::_update_shapes() {155if (!space) {156return;157}158159for (int i = 0; i < shapes.size(); i++) {160Shape &s = shapes.write[i];161if (s.disabled) {162continue;163}164165//not quite correct, should compute the next matrix..166AABB shape_aabb = s.shape->get_aabb();167Transform3D xform = transform * s.xform;168shape_aabb = xform.xform(shape_aabb);169shape_aabb.grow_by((s.aabb_cache.size.x + s.aabb_cache.size.y) * 0.5 * 0.05);170s.aabb_cache = shape_aabb;171172Vector3 scale = xform.get_basis().get_scale();173s.area_cache = s.shape->get_volume() * scale.x * scale.y * scale.z;174175if (s.bpid == 0) {176s.bpid = space->get_broadphase()->create(this, i, shape_aabb, _static);177space->get_broadphase()->set_static(s.bpid, _static);178}179180space->get_broadphase()->move(s.bpid, shape_aabb);181}182}183184void GodotCollisionObject3D::_update_shapes_with_motion(const Vector3 &p_motion) {185if (!space) {186return;187}188189for (int i = 0; i < shapes.size(); i++) {190Shape &s = shapes.write[i];191if (s.disabled) {192continue;193}194195//not quite correct, should compute the next matrix..196AABB shape_aabb = s.shape->get_aabb();197Transform3D xform = transform * s.xform;198shape_aabb = xform.xform(shape_aabb);199shape_aabb.merge_with(AABB(shape_aabb.position + p_motion, shape_aabb.size)); //use motion200s.aabb_cache = shape_aabb;201202if (s.bpid == 0) {203s.bpid = space->get_broadphase()->create(this, i, shape_aabb, _static);204space->get_broadphase()->set_static(s.bpid, _static);205}206207space->get_broadphase()->move(s.bpid, shape_aabb);208}209}210211void GodotCollisionObject3D::_set_space(GodotSpace3D *p_space) {212GodotSpace3D *old_space = space;213space = p_space;214215if (old_space) {216old_space->remove_object(this);217218for (int i = 0; i < shapes.size(); i++) {219Shape &s = shapes.write[i];220if (s.bpid) {221old_space->get_broadphase()->remove(s.bpid);222s.bpid = 0;223}224}225}226227if (space) {228space->add_object(this);229_update_shapes();230}231}232233void GodotCollisionObject3D::_shape_changed() {234_update_shapes();235_shapes_changed();236}237238GodotCollisionObject3D::GodotCollisionObject3D(Type p_type) :239pending_shape_update_list(this) {240type = p_type;241}242243244