| 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 "../shapes/jolt_shape_instance_3d.h" |
| 34 |
|
| 35 |
#include "core/math/vector3.h" |
| 36 |
#include "core/object/object.h" |
| 37 |
#include "core/string/ustring.h" |
| 38 |
#include "core/templates/local_vector.h" |
| 39 |
#include "core/templates/rid.h" |
| 40 |
|
| 41 |
#include "Jolt/Jolt.h" |
| 42 |
|
| 43 |
#include "Jolt/Physics/Body/Body.h" |
| 44 |
#include "Jolt/Physics/Collision/BroadPhase/BroadPhaseLayer.h" |
| 45 |
#include "Jolt/Physics/Collision/ObjectLayer.h" |
| 46 |
|
| 47 |
class JoltArea3D; |
| 48 |
class JoltBody3D; |
| 49 |
class JoltShapedObject3D; |
| 50 |
class JoltShape3D; |
| 51 |
class JoltSoftBody3D; |
| 52 |
class JoltSpace3D; |
| 53 |
|
| 54 |
class JoltObject3D { |
| 55 |
public: |
| 56 |
enum ObjectType : char { |
| 57 |
OBJECT_TYPE_INVALID, |
| 58 |
OBJECT_TYPE_BODY, |
| 59 |
OBJECT_TYPE_SOFT_BODY, |
| 60 |
OBJECT_TYPE_AREA, |
| 61 |
}; |
| 62 |
|
| 63 |
protected: |
| 64 |
LocalVector<JoltShapeInstance3D> shapes; |
| 65 |
|
| 66 |
RID rid; |
| 67 |
ObjectID instance_id; |
| 68 |
JoltSpace3D *space = nullptr; |
| 69 |
JPH::Body *jolt_body = nullptr; |
| 70 |
|
| 71 |
uint32_t collision_layer = 1; |
| 72 |
uint32_t collision_mask = 1; |
| 73 |
|
| 74 |
ObjectType object_type = OBJECT_TYPE_INVALID; |
| 75 |
|
| 76 |
bool pickable = false; |
| 77 |
|
| 78 |
virtual JPH::BroadPhaseLayer _get_broad_phase_layer() const = 0; |
| 79 |
virtual JPH::ObjectLayer _get_object_layer() const = 0; |
| 80 |
|
| 81 |
virtual void _add_to_space() = 0; |
| 82 |
virtual void _remove_from_space(); |
| 83 |
|
| 84 |
void _reset_space(); |
| 85 |
|
| 86 |
void _update_object_layer(); |
| 87 |
|
| 88 |
virtual void _collision_layer_changed(); |
| 89 |
virtual void _collision_mask_changed(); |
| 90 |
|
| 91 |
virtual void _space_changing() {} |
| 92 |
virtual void _space_changed() {} |
| 93 |
|
| 94 |
public: |
| 95 |
explicit JoltObject3D(ObjectType p_object_type); |
| 96 |
virtual ~JoltObject3D() = 0; |
| 97 |
|
| 98 |
ObjectType get_type() const { return object_type; } |
| 99 |
|
| 100 |
bool is_body() const { return object_type == OBJECT_TYPE_BODY; } |
| 101 |
bool is_soft_body() const { return object_type == OBJECT_TYPE_SOFT_BODY; } |
| 102 |
bool is_area() const { return object_type == OBJECT_TYPE_AREA; } |
| 103 |
bool is_shaped() const { return object_type != OBJECT_TYPE_SOFT_BODY; } |
| 104 |
|
| 105 |
JoltShapedObject3D *as_shaped() { return is_shaped() ? reinterpret_cast<JoltShapedObject3D *>(this) : nullptr; } |
| 106 |
const JoltShapedObject3D *as_shaped() const { return is_shaped() ? reinterpret_cast<const JoltShapedObject3D *>(this) : nullptr; } |
| 107 |
|
| 108 |
JoltBody3D *as_body() { return is_body() ? reinterpret_cast<JoltBody3D *>(this) : nullptr; } |
| 109 |
const JoltBody3D *as_body() const { return is_body() ? reinterpret_cast<const JoltBody3D *>(this) : nullptr; } |
| 110 |
|
| 111 |
JoltSoftBody3D *as_soft_body() { return is_soft_body() ? reinterpret_cast<JoltSoftBody3D *>(this) : nullptr; } |
| 112 |
const JoltSoftBody3D *as_soft_body() const { return is_soft_body() ? reinterpret_cast<const JoltSoftBody3D *>(this) : nullptr; } |
| 113 |
|
| 114 |
JoltArea3D *as_area() { return is_area() ? reinterpret_cast<JoltArea3D *>(this) : nullptr; } |
| 115 |
const JoltArea3D *as_area() const { return is_area() ? reinterpret_cast<const JoltArea3D *>(this) : nullptr; } |
| 116 |
|
| 117 |
RID get_rid() const { return rid; } |
| 118 |
void set_rid(const RID &p_rid) { rid = p_rid; } |
| 119 |
|
| 120 |
ObjectID get_instance_id() const { return instance_id; } |
| 121 |
void set_instance_id(ObjectID p_id) { instance_id = p_id; } |
| 122 |
Object *get_instance() const; |
| 123 |
|
| 124 |
JPH::Body *get_jolt_body() const { return jolt_body; } |
| 125 |
JPH::BodyID get_jolt_id() const { return jolt_body->GetID(); } |
| 126 |
|
| 127 |
JoltSpace3D *get_space() const { return space; } |
| 128 |
void set_space(JoltSpace3D *p_space); |
| 129 |
bool in_space() const { return space != nullptr && jolt_body != nullptr; } |
| 130 |
|
| 131 |
uint32_t get_collision_layer() const { return collision_layer; } |
| 132 |
void set_collision_layer(uint32_t p_layer); |
| 133 |
|
| 134 |
uint32_t get_collision_mask() const { return collision_mask; } |
| 135 |
void set_collision_mask(uint32_t p_mask); |
| 136 |
|
| 137 |
virtual Vector3 get_velocity_at_position(const Vector3 &p_position) const = 0; |
| 138 |
|
| 139 |
bool is_pickable() const { return pickable; } |
| 140 |
void set_pickable(bool p_enabled) { pickable = p_enabled; } |
| 141 |
|
| 142 |
bool can_collide_with(const JoltObject3D &p_other) const; |
| 143 |
bool can_interact_with(const JoltObject3D &p_other) const; |
| 144 |
|
| 145 |
virtual bool can_interact_with(const JoltBody3D &p_other) const = 0; |
| 146 |
virtual bool can_interact_with(const JoltSoftBody3D &p_other) const = 0; |
| 147 |
virtual bool can_interact_with(const JoltArea3D &p_other) const = 0; |
| 148 |
|
| 149 |
virtual bool reports_contacts() const = 0; |
| 150 |
|
| 151 |
virtual void pre_step(float p_step, JPH::Body &p_jolt_body) {} |
| 152 |
|
| 153 |
String to_string() const; |
| 154 |
}; |