physical_bone_3d.h 10.0 KB

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