physical_bone_3d.h 10 KB

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