| 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 "core/templates/vset.h" |
| 34 |
#include "scene/3d/physics/physics_body_3d.h" |
| 35 |
|
| 36 |
class RigidBody3D : public PhysicsBody3D { |
| 37 |
GDCLASS(RigidBody3D, PhysicsBody3D); |
| 38 |
|
| 39 |
public: |
| 40 |
enum FreezeMode { |
| 41 |
FREEZE_MODE_STATIC, |
| 42 |
FREEZE_MODE_KINEMATIC, |
| 43 |
}; |
| 44 |
|
| 45 |
enum CenterOfMassMode { |
| 46 |
CENTER_OF_MASS_MODE_AUTO, |
| 47 |
CENTER_OF_MASS_MODE_CUSTOM, |
| 48 |
}; |
| 49 |
|
| 50 |
enum DampMode { |
| 51 |
DAMP_MODE_COMBINE, |
| 52 |
DAMP_MODE_REPLACE, |
| 53 |
}; |
| 54 |
|
| 55 |
private: |
| 56 |
bool can_sleep = true; |
| 57 |
bool lock_rotation = false; |
| 58 |
bool freeze = false; |
| 59 |
FreezeMode freeze_mode = FREEZE_MODE_STATIC; |
| 60 |
|
| 61 |
real_t mass = 1.0; |
| 62 |
Vector3 inertia; |
| 63 |
CenterOfMassMode center_of_mass_mode = CENTER_OF_MASS_MODE_AUTO; |
| 64 |
Vector3 center_of_mass; |
| 65 |
|
| 66 |
Ref<PhysicsMaterial> physics_material_override; |
| 67 |
|
| 68 |
Vector3 linear_velocity; |
| 69 |
Vector3 angular_velocity; |
| 70 |
Basis inverse_inertia_tensor; |
| 71 |
real_t gravity_scale = 1.0; |
| 72 |
|
| 73 |
DampMode linear_damp_mode = DAMP_MODE_COMBINE; |
| 74 |
DampMode angular_damp_mode = DAMP_MODE_COMBINE; |
| 75 |
|
| 76 |
real_t linear_damp = 0.0; |
| 77 |
real_t angular_damp = 0.0; |
| 78 |
|
| 79 |
bool sleeping = false; |
| 80 |
bool ccd = false; |
| 81 |
|
| 82 |
int max_contacts_reported = 0; |
| 83 |
int contact_count = 0; |
| 84 |
|
| 85 |
bool custom_integrator = false; |
| 86 |
|
| 87 |
struct ShapePair { |
| 88 |
int body_shape = 0; |
| 89 |
int local_shape = 0; |
| 90 |
bool tagged = false; |
| 91 |
bool operator<(const ShapePair &p_sp) const { |
| 92 |
if (body_shape == p_sp.body_shape) { |
| 93 |
return local_shape < p_sp.local_shape; |
| 94 |
} else { |
| 95 |
return body_shape < p_sp.body_shape; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
ShapePair() {} |
| 100 |
ShapePair(int p_bs, int p_ls) { |
| 101 |
body_shape = p_bs; |
| 102 |
local_shape = p_ls; |
| 103 |
tagged = false; |
| 104 |
} |
| 105 |
}; |
| 106 |
struct RigidBody3D_RemoveAction { |
| 107 |
RID rid; |
| 108 |
ObjectID body_id; |
| 109 |
ShapePair pair; |
| 110 |
}; |
| 111 |
struct BodyState { |
| 112 |
RID rid; |
| 113 |
|
| 114 |
bool in_tree = false; |
| 115 |
VSet<ShapePair> shapes; |
| 116 |
}; |
| 117 |
|
| 118 |
struct ContactMonitor { |
| 119 |
bool locked = false; |
| 120 |
HashMap<ObjectID, BodyState> body_map; |
| 121 |
}; |
| 122 |
|
| 123 |
ContactMonitor *contact_monitor = nullptr; |
| 124 |
void _body_enter_tree(ObjectID p_id); |
| 125 |
void _body_exit_tree(ObjectID p_id); |
| 126 |
|
| 127 |
void _body_inout(int p_status, const RID &p_body, ObjectID p_instance, int p_body_shape, int p_local_shape); |
| 128 |
static void _body_state_changed_callback(void *p_instance, PhysicsDirectBodyState3D *p_state); |
| 129 |
|
| 130 |
void _sync_body_state(PhysicsDirectBodyState3D *p_state); |
| 131 |
|
| 132 |
protected: |
| 133 |
void _notification(int p_what); |
| 134 |
static void _bind_methods(); |
| 135 |
|
| 136 |
void _validate_property(PropertyInfo &p_property) const; |
| 137 |
|
| 138 |
GDVIRTUAL1(_integrate_forces, RequiredParam<PhysicsDirectBodyState3D>) |
| 139 |
|
| 140 |
virtual void _body_state_changed(PhysicsDirectBodyState3D *p_state); |
| 141 |
|
| 142 |
void _apply_body_mode(); |
| 143 |
|
| 144 |
public: |
| 145 |
void set_lock_rotation_enabled(bool p_lock_rotation); |
| 146 |
bool is_lock_rotation_enabled() const; |
| 147 |
|
| 148 |
void set_freeze_enabled(bool p_freeze); |
| 149 |
bool is_freeze_enabled() const; |
| 150 |
|
| 151 |
void set_freeze_mode(FreezeMode p_freeze_mode); |
| 152 |
FreezeMode get_freeze_mode() const; |
| 153 |
|
| 154 |
void set_mass(real_t p_mass); |
| 155 |
real_t get_mass() const; |
| 156 |
|
| 157 |
virtual real_t get_inverse_mass() const override { return 1.0 / mass; } |
| 158 |
|
| 159 |
void set_inertia(const Vector3 &p_inertia); |
| 160 |
const Vector3 &get_inertia() const; |
| 161 |
|
| 162 |
void set_center_of_mass_mode(CenterOfMassMode p_mode); |
| 163 |
CenterOfMassMode get_center_of_mass_mode() const; |
| 164 |
|
| 165 |
void set_center_of_mass(const Vector3 &p_center_of_mass); |
| 166 |
const Vector3 &get_center_of_mass() const; |
| 167 |
|
| 168 |
void set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override); |
| 169 |
Ref<PhysicsMaterial> get_physics_material_override() const; |
| 170 |
|
| 171 |
void set_linear_velocity(const Vector3 &p_velocity); |
| 172 |
Vector3 get_linear_velocity() const override; |
| 173 |
|
| 174 |
void set_axis_velocity(const Vector3 &p_axis); |
| 175 |
|
| 176 |
void set_angular_velocity(const Vector3 &p_velocity); |
| 177 |
Vector3 get_angular_velocity() const override; |
| 178 |
|
| 179 |
Basis get_inverse_inertia_tensor() const; |
| 180 |
|
| 181 |
void set_gravity_scale(real_t p_gravity_scale); |
| 182 |
real_t get_gravity_scale() const; |
| 183 |
|
| 184 |
void set_linear_damp_mode(DampMode p_mode); |
| 185 |
DampMode get_linear_damp_mode() const; |
| 186 |
|
| 187 |
void set_angular_damp_mode(DampMode p_mode); |
| 188 |
DampMode get_angular_damp_mode() const; |
| 189 |
|
| 190 |
void set_linear_damp(real_t p_linear_damp); |
| 191 |
real_t get_linear_damp() const; |
| 192 |
|
| 193 |
void set_angular_damp(real_t p_angular_damp); |
| 194 |
real_t get_angular_damp() const; |
| 195 |
|
| 196 |
void set_use_custom_integrator(bool p_enable); |
| 197 |
bool is_using_custom_integrator(); |
| 198 |
|
| 199 |
void set_sleeping(bool p_sleeping); |
| 200 |
bool is_sleeping() const; |
| 201 |
|
| 202 |
void set_can_sleep(bool p_active); |
| 203 |
bool is_able_to_sleep() const; |
| 204 |
|
| 205 |
void set_contact_monitor(bool p_enabled); |
| 206 |
bool is_contact_monitor_enabled() const; |
| 207 |
|
| 208 |
void set_max_contacts_reported(int p_amount); |
| 209 |
int get_max_contacts_reported() const; |
| 210 |
int get_contact_count() const; |
| 211 |
|
| 212 |
void set_use_continuous_collision_detection(bool p_enable); |
| 213 |
bool is_using_continuous_collision_detection() const; |
| 214 |
|
| 215 |
TypedArray<Node3D> get_colliding_bodies() const; |
| 216 |
|
| 217 |
void apply_central_impulse(const Vector3 &p_impulse); |
| 218 |
void apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position = Vector3()); |
| 219 |
void apply_torque_impulse(const Vector3 &p_impulse); |
| 220 |
|
| 221 |
void apply_central_force(const Vector3 &p_force); |
| 222 |
void apply_force(const Vector3 &p_force, const Vector3 &p_position = Vector3()); |
| 223 |
void apply_torque(const Vector3 &p_torque); |
| 224 |
|
| 225 |
void add_constant_central_force(const Vector3 &p_force); |
| 226 |
void add_constant_force(const Vector3 &p_force, const Vector3 &p_position = Vector3()); |
| 227 |
void add_constant_torque(const Vector3 &p_torque); |
| 228 |
|
| 229 |
void set_constant_force(const Vector3 &p_force); |
| 230 |
Vector3 get_constant_force() const; |
| 231 |
|
| 232 |
void set_constant_torque(const Vector3 &p_torque); |
| 233 |
Vector3 get_constant_torque() const; |
| 234 |
|
| 235 |
virtual PackedStringArray get_configuration_warnings() const override; |
| 236 |
|
| 237 |
RigidBody3D(); |
| 238 |
~RigidBody3D(); |
| 239 |
|
| 240 |
private: |
| 241 |
void _reload_physics_characteristics(); |
| 242 |
}; |
| 243 |
|
| 244 |
VARIANT_ENUM_CAST(RigidBody3D::FreezeMode); |
| 245 |
VARIANT_ENUM_CAST(RigidBody3D::CenterOfMassMode); |
| 246 |
VARIANT_ENUM_CAST(RigidBody3D::DampMode); |