| 1 | /**************************************************************************/ |
| 2 | /* jolt_shape_instance_3d.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "jolt_shape_instance_3d.h" |
| 32 | |
| 33 | #include "jolt_shape_3d.h" |
| 34 | |
| 35 | #include "Jolt/Physics/Collision/Shape/DecoratedShape.h" |
| 36 | |
| 37 | JoltShapeInstance3D::ShapeReference::ShapeReference(JoltShapedObject3D *p_parent, JoltShape3D *p_shape) : |
| 38 | parent(p_parent), |
| 39 | shape(p_shape) { |
| 40 | if (shape != nullptr) { |
| 41 | shape->add_owner(parent); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | JoltShapeInstance3D::ShapeReference::ShapeReference(ShapeReference &&p_other) : |
| 46 | parent(p_other.parent), |
| 47 | shape(p_other.shape) { |
| 48 | p_other.parent = nullptr; |
| 49 | p_other.shape = nullptr; |
| 50 | } |
| 51 | |
| 52 | JoltShapeInstance3D::ShapeReference::~ShapeReference() { |
| 53 | if (shape != nullptr) { |
| 54 | shape->remove_owner(parent); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | JoltShapeInstance3D::ShapeReference &JoltShapeInstance3D::ShapeReference::operator=(ShapeReference &&p_other) { |
| 59 | if (this != &p_other) { |
| 60 | SWAP(parent, p_other.parent); |
| 61 | SWAP(shape, p_other.shape); |
| 62 | } |
| 63 | |
| 64 | return *this; |
| 65 | } |
| 66 | |
| 67 | JoltShapeInstance3D::JoltShapeInstance3D(JoltShapedObject3D *p_parent, JoltShape3D *p_shape, const Transform3D &p_transform, const Vector3 &p_scale, bool p_disabled) : |
| 68 | transform(p_transform), |
| 69 | scale(p_scale), |
| 70 | shape(p_parent, p_shape), |
| 71 | disabled(p_disabled) { |
| 72 | } |
| 73 | |
| 74 | AABB JoltShapeInstance3D::get_aabb() const { |
| 75 | return get_transform_scaled().xform(shape->get_aabb()); |
| 76 | } |
| 77 | |
| 78 | bool JoltShapeInstance3D::try_build() { |
| 79 | ERR_FAIL_COND_V(is_disabled(), false); |
| 80 | |
| 81 | const JPH::ShapeRefC maybe_new_shape = shape->try_build(); |
| 82 | |
| 83 | if (maybe_new_shape == nullptr) { |
| 84 | jolt_ref = nullptr; |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | if (jolt_ref != nullptr) { |
| 89 | const JPH::DecoratedShape *outer_shape = static_cast<const JPH::DecoratedShape *>(jolt_ref.GetPtr()); |
| 90 | |
| 91 | if (outer_shape->GetInnerShape() == maybe_new_shape) { |
| 92 | return true; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | jolt_ref = JoltShape3D::with_user_data(maybe_new_shape, (uint64_t)id); |
| 97 | |
| 98 | return true; |
| 99 | } |