| 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 "scene/3d/physics/physics_body_3d.h" |
| 34 |
#include "scene/3d/skeleton_3d.h" |
| 35 |
|
| 36 |
class PhysicalBoneSimulator3D; |
| 37 |
|
| 38 |
class PhysicalBone3D : public PhysicsBody3D { |
| 39 |
GDCLASS(PhysicalBone3D, PhysicsBody3D); |
| 40 |
|
| 41 |
public: |
| 42 |
enum DampMode { |
| 43 |
DAMP_MODE_COMBINE, |
| 44 |
DAMP_MODE_REPLACE, |
| 45 |
}; |
| 46 |
|
| 47 |
enum JointType { |
| 48 |
JOINT_TYPE_NONE, |
| 49 |
JOINT_TYPE_PIN, |
| 50 |
JOINT_TYPE_CONE, |
| 51 |
JOINT_TYPE_HINGE, |
| 52 |
JOINT_TYPE_SLIDER, |
| 53 |
JOINT_TYPE_6DOF |
| 54 |
}; |
| 55 |
|
| 56 |
struct JointData { |
| 57 |
virtual JointType get_joint_type() { return JOINT_TYPE_NONE; } |
| 58 |
|
| 59 |
|
| 60 |
virtual bool _set(const StringName &p_name, const Variant &p_value, RID j); |
| 61 |
virtual bool _get(const StringName &p_name, Variant &r_ret) const; |
| 62 |
virtual void _get_property_list(List<PropertyInfo> *p_list) const; |
| 63 |
|
| 64 |
virtual ~JointData() {} |
| 65 |
}; |
| 66 |
|
| 67 |
struct PinJointData : public JointData { |
| 68 |
virtual JointType get_joint_type() { return JOINT_TYPE_PIN; } |
| 69 |
|
| 70 |
virtual bool _set(const StringName &p_name, const Variant &p_value, RID j); |
| 71 |
virtual bool _get(const StringName &p_name, Variant &r_ret) const; |
| 72 |
virtual void _get_property_list(List<PropertyInfo> *p_list) const; |
| 73 |
|
| 74 |
real_t bias = 0.3; |
| 75 |
real_t damping = 1.0; |
| 76 |
real_t impulse_clamp = 0.0; |
| 77 |
}; |
| 78 |
|
| 79 |
struct ConeJointData : public JointData { |
| 80 |
virtual JointType get_joint_type() { return JOINT_TYPE_CONE; } |
| 81 |
|
| 82 |
virtual bool _set(const StringName &p_name, const Variant &p_value, RID j); |
| 83 |
virtual bool _get(const StringName &p_name, Variant &r_ret) const; |
| 84 |
virtual void _get_property_list(List<PropertyInfo> *p_list) const; |
| 85 |
|
| 86 |
real_t swing_span = Math::PI * 0.25; |
| 87 |
real_t twist_span = Math::PI; |
| 88 |
real_t bias = 0.3; |
| 89 |
real_t softness = 0.8; |
| 90 |
real_t relaxation = 1.; |
| 91 |
}; |
| 92 |
|
| 93 |
struct HingeJointData : public JointData { |
| 94 |
virtual JointType get_joint_type() { return JOINT_TYPE_HINGE; } |
| 95 |
|
| 96 |
virtual bool _set(const StringName &p_name, const Variant &p_value, RID j); |
| 97 |
virtual bool _get(const StringName &p_name, Variant &r_ret) const; |
| 98 |
virtual void _get_property_list(List<PropertyInfo> *p_list) const; |
| 99 |
|
| 100 |
bool angular_limit_enabled = false; |
| 101 |
real_t angular_limit_upper = Math::PI * 0.5; |
| 102 |
real_t angular_limit_lower = -Math::PI * 0.5; |
| 103 |
real_t angular_limit_bias = 0.3; |
| 104 |
real_t angular_limit_softness = 0.9; |
| 105 |
real_t angular_limit_relaxation = 1.; |
| 106 |
}; |
| 107 |
|
| 108 |
struct SliderJointData : public JointData { |
| 109 |
virtual JointType get_joint_type() { return JOINT_TYPE_SLIDER; } |
| 110 |
|
| 111 |
virtual bool _set(const StringName &p_name, const Variant &p_value, RID j); |
| 112 |
virtual bool _get(const StringName &p_name, Variant &r_ret) const; |
| 113 |
virtual void _get_property_list(List<PropertyInfo> *p_list) const; |
| 114 |
|
| 115 |
real_t linear_limit_upper = 1.0; |
| 116 |
real_t linear_limit_lower = -1.0; |
| 117 |
real_t linear_limit_softness = 1.0; |
| 118 |
real_t linear_limit_restitution = 0.7; |
| 119 |
real_t linear_limit_damping = 1.0; |
| 120 |
real_t angular_limit_upper = 0.0; |
| 121 |
real_t angular_limit_lower = 0.0; |
| 122 |
real_t angular_limit_softness = 1.0; |
| 123 |
real_t angular_limit_restitution = 0.7; |
| 124 |
real_t angular_limit_damping = 1.0; |
| 125 |
}; |
| 126 |
|
| 127 |
struct SixDOFJointData : public JointData { |
| 128 |
struct SixDOFAxisData { |
| 129 |
bool linear_limit_enabled = true; |
| 130 |
real_t linear_limit_upper = 0.0; |
| 131 |
real_t linear_limit_lower = 0.0; |
| 132 |
real_t linear_limit_softness = 0.7; |
| 133 |
real_t linear_restitution = 0.5; |
| 134 |
real_t linear_damping = 1.0; |
| 135 |
bool linear_spring_enabled = false; |
| 136 |
real_t linear_spring_stiffness = 0.0; |
| 137 |
real_t linear_spring_damping = 0.0; |
| 138 |
real_t linear_equilibrium_point = 0.0; |
| 139 |
bool angular_limit_enabled = true; |
| 140 |
real_t angular_limit_upper = 0.0; |
| 141 |
real_t angular_limit_lower = 0.0; |
| 142 |
real_t angular_limit_softness = 0.5; |
| 143 |
real_t angular_restitution = 0.0; |
| 144 |
real_t angular_damping = 1.0; |
| 145 |
real_t erp = 0.5; |
| 146 |
bool angular_spring_enabled = false; |
| 147 |
real_t angular_spring_stiffness = 0.0; |
| 148 |
real_t angular_spring_damping = 0.0; |
| 149 |
real_t angular_equilibrium_point = 0.0; |
| 150 |
}; |
| 151 |
|
| 152 |
virtual JointType get_joint_type() { return JOINT_TYPE_6DOF; } |
| 153 |
|
| 154 |
virtual bool _set(const StringName &p_name, const Variant &p_value, RID j); |
| 155 |
virtual bool _get(const StringName &p_name, Variant &r_ret) const; |
| 156 |
virtual void _get_property_list(List<PropertyInfo> *p_list) const; |
| 157 |
|
| 158 |
SixDOFAxisData axis_data[3]; |
| 159 |
}; |
| 160 |
|
| 161 |
private: |
| 162 |
#ifdef TOOLS_ENABLED |
| 163 |
|
| 164 |
bool gizmo_move_joint = false; |
| 165 |
#endif |
| 166 |
|
| 167 |
JointData *joint_data = nullptr; |
| 168 |
Transform3D joint_offset; |
| 169 |
RID joint; |
| 170 |
|
| 171 |
ObjectID simulator_id; |
| 172 |
Transform3D body_offset; |
| 173 |
Transform3D body_offset_inverse; |
| 174 |
bool simulate_physics = false; |
| 175 |
bool _internal_simulate_physics = false; |
| 176 |
int bone_id = -1; |
| 177 |
|
| 178 |
String bone_name; |
| 179 |
real_t bounce = 0.0; |
| 180 |
real_t mass = 1.0; |
| 181 |
real_t friction = 1.0; |
| 182 |
Vector3 linear_velocity; |
| 183 |
Vector3 angular_velocity; |
| 184 |
real_t gravity_scale = 1.0; |
| 185 |
bool can_sleep = true; |
| 186 |
|
| 187 |
bool custom_integrator = false; |
| 188 |
|
| 189 |
DampMode linear_damp_mode = DAMP_MODE_COMBINE; |
| 190 |
DampMode angular_damp_mode = DAMP_MODE_COMBINE; |
| 191 |
|
| 192 |
real_t linear_damp = 0.0; |
| 193 |
real_t angular_damp = 0.0; |
| 194 |
|
| 195 |
protected: |
| 196 |
bool _set(const StringName &p_name, const Variant &p_value); |
| 197 |
bool _get(const StringName &p_name, Variant &r_ret) const; |
| 198 |
void _get_property_list(List<PropertyInfo> *p_list) const; |
| 199 |
void _notification(int p_what); |
| 200 |
GDVIRTUAL1(_integrate_forces, RequiredParam<PhysicsDirectBodyState3D>) |
| 201 |
static void _body_state_changed_callback(void *p_instance, PhysicsDirectBodyState3D *p_state); |
| 202 |
void _body_state_changed(PhysicsDirectBodyState3D *p_state); |
| 203 |
|
| 204 |
static void _bind_methods(); |
| 205 |
|
| 206 |
private: |
| 207 |
void _sync_body_state(PhysicsDirectBodyState3D *p_state); |
| 208 |
|
| 209 |
void _update_joint_offset(); |
| 210 |
void _fix_joint_offset(); |
| 211 |
void _reload_joint(); |
| 212 |
|
| 213 |
void _update_simulator_path(); |
| 214 |
|
| 215 |
public: |
| 216 |
void _on_bone_parent_changed(); |
| 217 |
|
| 218 |
PhysicalBoneSimulator3D *get_simulator() const; |
| 219 |
Skeleton3D *get_skeleton() const; |
| 220 |
|
| 221 |
void set_linear_velocity(const Vector3 &p_velocity); |
| 222 |
Vector3 get_linear_velocity() const override; |
| 223 |
|
| 224 |
void set_angular_velocity(const Vector3 &p_velocity); |
| 225 |
Vector3 get_angular_velocity() const override; |
| 226 |
|
| 227 |
void set_use_custom_integrator(bool p_enable); |
| 228 |
bool is_using_custom_integrator(); |
| 229 |
|
| 230 |
#ifdef TOOLS_ENABLED |
| 231 |
void _set_gizmo_move_joint(bool p_move_joint); |
| 232 |
virtual Transform3D get_global_gizmo_transform() const override; |
| 233 |
virtual Transform3D get_local_gizmo_transform() const override; |
| 234 |
#endif |
| 235 |
|
| 236 |
const JointData *get_joint_data() const; |
| 237 |
|
| 238 |
int get_bone_id() const { |
| 239 |
return bone_id; |
| 240 |
} |
| 241 |
|
| 242 |
void set_joint_type(JointType p_joint_type); |
| 243 |
JointType get_joint_type() const; |
| 244 |
|
| 245 |
void set_joint_offset(const Transform3D &p_offset); |
| 246 |
const Transform3D &get_joint_offset() const; |
| 247 |
|
| 248 |
void set_joint_rotation(const Vector3 &p_euler_rad); |
| 249 |
Vector3 get_joint_rotation() const; |
| 250 |
|
| 251 |
void set_body_offset(const Transform3D &p_offset); |
| 252 |
const Transform3D &get_body_offset() const; |
| 253 |
|
| 254 |
void set_simulate_physics(bool p_simulate); |
| 255 |
bool get_simulate_physics(); |
| 256 |
bool is_simulating_physics(); |
| 257 |
|
| 258 |
void set_bone_name(const String &p_name); |
| 259 |
const String &get_bone_name() const; |
| 260 |
|
| 261 |
void set_mass(real_t p_mass); |
| 262 |
real_t get_mass() const; |
| 263 |
|
| 264 |
void set_friction(real_t p_friction); |
| 265 |
real_t get_friction() const; |
| 266 |
|
| 267 |
void set_bounce(real_t p_bounce); |
| 268 |
real_t get_bounce() const; |
| 269 |
|
| 270 |
void set_gravity_scale(real_t p_gravity_scale); |
| 271 |
real_t get_gravity_scale() const; |
| 272 |
|
| 273 |
void set_linear_damp_mode(DampMode p_mode); |
| 274 |
DampMode get_linear_damp_mode() const; |
| 275 |
|
| 276 |
void set_angular_damp_mode(DampMode p_mode); |
| 277 |
DampMode get_angular_damp_mode() const; |
| 278 |
|
| 279 |
void set_linear_damp(real_t p_linear_damp); |
| 280 |
real_t get_linear_damp() const; |
| 281 |
|
| 282 |
void set_angular_damp(real_t p_angular_damp); |
| 283 |
real_t get_angular_damp() const; |
| 284 |
|
| 285 |
void set_can_sleep(bool p_active); |
| 286 |
bool is_able_to_sleep() const; |
| 287 |
|
| 288 |
void apply_central_impulse(const Vector3 &p_impulse); |
| 289 |
void apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position = Vector3()); |
| 290 |
|
| 291 |
void reset_physics_simulation_state(); |
| 292 |
void reset_to_rest_position(); |
| 293 |
|
| 294 |
PhysicalBone3D(); |
| 295 |
~PhysicalBone3D(); |
| 296 |
|
| 297 |
private: |
| 298 |
void update_bone_id(); |
| 299 |
void update_offset(); |
| 300 |
|
| 301 |
void _start_physics_simulation(); |
| 302 |
void _stop_physics_simulation(); |
| 303 |
}; |
| 304 |
|
| 305 |
VARIANT_ENUM_CAST(PhysicalBone3D::JointType); |
| 306 |
VARIANT_ENUM_CAST(PhysicalBone3D::DampMode); |