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