| 1 | /**************************************************************************/ |
| 2 | /* godot_collision_object_3d.h */ |
| 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 | #ifndef GODOT_COLLISION_OBJECT_3D_H |
| 32 | #define GODOT_COLLISION_OBJECT_3D_H |
| 33 | |
| 34 | #include "godot_broad_phase_3d.h" |
| 35 | #include "godot_shape_3d.h" |
| 36 | |
| 37 | #include "core/templates/self_list.h" |
| 38 | #include "servers/physics_server_3d.h" |
| 39 | |
| 40 | #ifdef DEBUG_ENABLED |
| 41 | #define MAX_OBJECT_DISTANCE 3.1622776601683791e+18 |
| 42 | |
| 43 | #define MAX_OBJECT_DISTANCE_X2 (MAX_OBJECT_DISTANCE * MAX_OBJECT_DISTANCE) |
| 44 | #endif |
| 45 | |
| 46 | class GodotSpace3D; |
| 47 | |
| 48 | class GodotCollisionObject3D : public GodotShapeOwner3D { |
| 49 | public: |
| 50 | enum Type { |
| 51 | TYPE_AREA, |
| 52 | TYPE_BODY, |
| 53 | TYPE_SOFT_BODY, |
| 54 | }; |
| 55 | |
| 56 | private: |
| 57 | Type type; |
| 58 | RID self; |
| 59 | ObjectID instance_id; |
| 60 | uint32_t collision_layer = 1; |
| 61 | uint32_t collision_mask = 1; |
| 62 | real_t collision_priority = 1.0; |
| 63 | |
| 64 | struct Shape { |
| 65 | Transform3D xform; |
| 66 | Transform3D xform_inv; |
| 67 | GodotBroadPhase3D::ID bpid; |
| 68 | AABB aabb_cache; //for rayqueries |
| 69 | real_t area_cache = 0.0; |
| 70 | GodotShape3D *shape = nullptr; |
| 71 | bool disabled = false; |
| 72 | }; |
| 73 | |
| 74 | Vector<Shape> shapes; |
| 75 | GodotSpace3D *space = nullptr; |
| 76 | Transform3D transform; |
| 77 | Transform3D inv_transform; |
| 78 | bool _static = true; |
| 79 | |
| 80 | SelfList<GodotCollisionObject3D> pending_shape_update_list; |
| 81 | |
| 82 | void _update_shapes(); |
| 83 | |
| 84 | protected: |
| 85 | void _update_shapes_with_motion(const Vector3 &p_motion); |
| 86 | void _unregister_shapes(); |
| 87 | |
| 88 | _FORCE_INLINE_ void _set_transform(const Transform3D &p_transform, bool p_update_shapes = true) { |
| 89 | #ifdef DEBUG_ENABLED |
| 90 | |
| 91 | ERR_FAIL_COND_MSG(p_transform.origin.length_squared() > MAX_OBJECT_DISTANCE_X2, "Object went too far away (more than '" + itos(MAX_OBJECT_DISTANCE) + "' units from origin)." ); |
| 92 | #endif |
| 93 | |
| 94 | transform = p_transform; |
| 95 | if (p_update_shapes) { |
| 96 | _update_shapes(); |
| 97 | } |
| 98 | } |
| 99 | _FORCE_INLINE_ void _set_inv_transform(const Transform3D &p_transform) { inv_transform = p_transform; } |
| 100 | void _set_static(bool p_static); |
| 101 | |
| 102 | virtual void _shapes_changed() = 0; |
| 103 | void _set_space(GodotSpace3D *p_space); |
| 104 | |
| 105 | bool ray_pickable = true; |
| 106 | |
| 107 | GodotCollisionObject3D(Type p_type); |
| 108 | |
| 109 | public: |
| 110 | _FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; } |
| 111 | _FORCE_INLINE_ RID get_self() const { return self; } |
| 112 | |
| 113 | _FORCE_INLINE_ void set_instance_id(const ObjectID &p_instance_id) { instance_id = p_instance_id; } |
| 114 | _FORCE_INLINE_ ObjectID get_instance_id() const { return instance_id; } |
| 115 | |
| 116 | void _shape_changed() override; |
| 117 | |
| 118 | _FORCE_INLINE_ Type get_type() const { return type; } |
| 119 | void add_shape(GodotShape3D *p_shape, const Transform3D &p_transform = Transform3D(), bool p_disabled = false); |
| 120 | void set_shape(int p_index, GodotShape3D *p_shape); |
| 121 | void set_shape_transform(int p_index, const Transform3D &p_transform); |
| 122 | _FORCE_INLINE_ int get_shape_count() const { return shapes.size(); } |
| 123 | _FORCE_INLINE_ GodotShape3D *get_shape(int p_index) const { |
| 124 | CRASH_BAD_INDEX(p_index, shapes.size()); |
| 125 | return shapes[p_index].shape; |
| 126 | } |
| 127 | _FORCE_INLINE_ const Transform3D &get_shape_transform(int p_index) const { |
| 128 | CRASH_BAD_INDEX(p_index, shapes.size()); |
| 129 | return shapes[p_index].xform; |
| 130 | } |
| 131 | _FORCE_INLINE_ const Transform3D &get_shape_inv_transform(int p_index) const { |
| 132 | CRASH_BAD_INDEX(p_index, shapes.size()); |
| 133 | return shapes[p_index].xform_inv; |
| 134 | } |
| 135 | _FORCE_INLINE_ const AABB &get_shape_aabb(int p_index) const { |
| 136 | CRASH_BAD_INDEX(p_index, shapes.size()); |
| 137 | return shapes[p_index].aabb_cache; |
| 138 | } |
| 139 | _FORCE_INLINE_ real_t get_shape_area(int p_index) const { |
| 140 | CRASH_BAD_INDEX(p_index, shapes.size()); |
| 141 | return shapes[p_index].area_cache; |
| 142 | } |
| 143 | |
| 144 | _FORCE_INLINE_ const Transform3D &get_transform() const { return transform; } |
| 145 | _FORCE_INLINE_ const Transform3D &get_inv_transform() const { return inv_transform; } |
| 146 | _FORCE_INLINE_ GodotSpace3D *get_space() const { return space; } |
| 147 | |
| 148 | _FORCE_INLINE_ void set_ray_pickable(bool p_enable) { ray_pickable = p_enable; } |
| 149 | _FORCE_INLINE_ bool is_ray_pickable() const { return ray_pickable; } |
| 150 | |
| 151 | void set_shape_disabled(int p_idx, bool p_disabled); |
| 152 | _FORCE_INLINE_ bool is_shape_disabled(int p_idx) const { |
| 153 | ERR_FAIL_INDEX_V(p_idx, shapes.size(), false); |
| 154 | return shapes[p_idx].disabled; |
| 155 | } |
| 156 | |
| 157 | _FORCE_INLINE_ void set_collision_layer(uint32_t p_layer) { |
| 158 | collision_layer = p_layer; |
| 159 | _shape_changed(); |
| 160 | } |
| 161 | _FORCE_INLINE_ uint32_t get_collision_layer() const { return collision_layer; } |
| 162 | |
| 163 | _FORCE_INLINE_ void set_collision_mask(uint32_t p_mask) { |
| 164 | collision_mask = p_mask; |
| 165 | _shape_changed(); |
| 166 | } |
| 167 | _FORCE_INLINE_ uint32_t get_collision_mask() const { return collision_mask; } |
| 168 | |
| 169 | _FORCE_INLINE_ void set_collision_priority(real_t p_priority) { |
| 170 | ERR_FAIL_COND_MSG(p_priority <= 0, "Priority must be greater than 0." ); |
| 171 | collision_priority = p_priority; |
| 172 | _shape_changed(); |
| 173 | } |
| 174 | _FORCE_INLINE_ real_t get_collision_priority() const { return collision_priority; } |
| 175 | |
| 176 | _FORCE_INLINE_ bool collides_with(GodotCollisionObject3D *p_other) const { |
| 177 | return p_other->collision_layer & collision_mask; |
| 178 | } |
| 179 | |
| 180 | _FORCE_INLINE_ bool interacts_with(const GodotCollisionObject3D *p_other) const { |
| 181 | return collision_layer & p_other->collision_mask || p_other->collision_layer & collision_mask; |
| 182 | } |
| 183 | |
| 184 | void remove_shape(GodotShape3D *p_shape) override; |
| 185 | void remove_shape(int p_index); |
| 186 | |
| 187 | virtual void set_space(GodotSpace3D *p_space) = 0; |
| 188 | |
| 189 | _FORCE_INLINE_ bool is_static() const { return _static; } |
| 190 | |
| 191 | virtual ~GodotCollisionObject3D() {} |
| 192 | }; |
| 193 | |
| 194 | #endif // GODOT_COLLISION_OBJECT_3D_H |
| 195 | |