physics_joint.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /**************************************************************************/
  2. /* physics_joint.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 PHYSICS_JOINT_H
  31. #define PHYSICS_JOINT_H
  32. #include "scene/3d/physics_body.h"
  33. #include "scene/3d/spatial.h"
  34. class Joint : public Spatial {
  35. GDCLASS(Joint, Spatial);
  36. RID ba, bb;
  37. RID joint;
  38. NodePath a;
  39. NodePath b;
  40. int solver_priority;
  41. bool exclude_from_collision;
  42. String warning;
  43. protected:
  44. void _disconnect_signals();
  45. void _body_exit_tree();
  46. void _update_joint(bool p_only_free = false);
  47. void _notification(int p_what);
  48. virtual RID _configure_joint(PhysicsBody *body_a, PhysicsBody *body_b) = 0;
  49. static void _bind_methods();
  50. public:
  51. virtual String get_configuration_warning() const;
  52. void set_node_a(const NodePath &p_node_a);
  53. NodePath get_node_a() const;
  54. void set_node_b(const NodePath &p_node_b);
  55. NodePath get_node_b() const;
  56. void set_solver_priority(int p_priority);
  57. int get_solver_priority() const;
  58. void set_exclude_nodes_from_collision(bool p_enable);
  59. bool get_exclude_nodes_from_collision() const;
  60. RID get_joint() const { return joint; }
  61. Joint();
  62. };
  63. ///////////////////////////////////////////
  64. class PinJoint : public Joint {
  65. GDCLASS(PinJoint, Joint);
  66. public:
  67. enum Param {
  68. PARAM_BIAS = PhysicsServer::PIN_JOINT_BIAS,
  69. PARAM_DAMPING = PhysicsServer::PIN_JOINT_DAMPING,
  70. PARAM_IMPULSE_CLAMP = PhysicsServer::PIN_JOINT_IMPULSE_CLAMP
  71. };
  72. protected:
  73. float params[3];
  74. virtual RID _configure_joint(PhysicsBody *body_a, PhysicsBody *body_b);
  75. static void _bind_methods();
  76. public:
  77. void set_param(Param p_param, float p_value);
  78. float get_param(Param p_param) const;
  79. PinJoint();
  80. };
  81. VARIANT_ENUM_CAST(PinJoint::Param);
  82. class HingeJoint : public Joint {
  83. GDCLASS(HingeJoint, Joint);
  84. public:
  85. enum Param {
  86. PARAM_BIAS = PhysicsServer::HINGE_JOINT_BIAS,
  87. PARAM_LIMIT_UPPER = PhysicsServer::HINGE_JOINT_LIMIT_UPPER,
  88. PARAM_LIMIT_LOWER = PhysicsServer::HINGE_JOINT_LIMIT_LOWER,
  89. PARAM_LIMIT_BIAS = PhysicsServer::HINGE_JOINT_LIMIT_BIAS,
  90. PARAM_LIMIT_SOFTNESS = PhysicsServer::HINGE_JOINT_LIMIT_SOFTNESS,
  91. PARAM_LIMIT_RELAXATION = PhysicsServer::HINGE_JOINT_LIMIT_RELAXATION,
  92. PARAM_MOTOR_TARGET_VELOCITY = PhysicsServer::HINGE_JOINT_MOTOR_TARGET_VELOCITY,
  93. PARAM_MOTOR_MAX_IMPULSE = PhysicsServer::HINGE_JOINT_MOTOR_MAX_IMPULSE,
  94. PARAM_MAX = PhysicsServer::HINGE_JOINT_MAX
  95. };
  96. enum Flag {
  97. FLAG_USE_LIMIT = PhysicsServer::HINGE_JOINT_FLAG_USE_LIMIT,
  98. FLAG_ENABLE_MOTOR = PhysicsServer::HINGE_JOINT_FLAG_ENABLE_MOTOR,
  99. FLAG_MAX = PhysicsServer::HINGE_JOINT_FLAG_MAX
  100. };
  101. protected:
  102. float params[PARAM_MAX];
  103. bool flags[FLAG_MAX];
  104. virtual RID _configure_joint(PhysicsBody *body_a, PhysicsBody *body_b);
  105. static void _bind_methods();
  106. void _set_upper_limit(float p_limit);
  107. float _get_upper_limit() const;
  108. void _set_lower_limit(float p_limit);
  109. float _get_lower_limit() const;
  110. public:
  111. void set_param(Param p_param, float p_value);
  112. float get_param(Param p_param) const;
  113. void set_flag(Flag p_flag, bool p_value);
  114. bool get_flag(Flag p_flag) const;
  115. HingeJoint();
  116. };
  117. VARIANT_ENUM_CAST(HingeJoint::Param);
  118. VARIANT_ENUM_CAST(HingeJoint::Flag);
  119. class SliderJoint : public Joint {
  120. GDCLASS(SliderJoint, Joint);
  121. public:
  122. enum Param {
  123. PARAM_LINEAR_LIMIT_UPPER = PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_UPPER,
  124. PARAM_LINEAR_LIMIT_LOWER = PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_LOWER,
  125. PARAM_LINEAR_LIMIT_SOFTNESS = PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS,
  126. PARAM_LINEAR_LIMIT_RESTITUTION = PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION,
  127. PARAM_LINEAR_LIMIT_DAMPING = PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_DAMPING,
  128. PARAM_LINEAR_MOTION_SOFTNESS = PhysicsServer::SLIDER_JOINT_LINEAR_MOTION_SOFTNESS,
  129. PARAM_LINEAR_MOTION_RESTITUTION = PhysicsServer::SLIDER_JOINT_LINEAR_MOTION_RESTITUTION,
  130. PARAM_LINEAR_MOTION_DAMPING = PhysicsServer::SLIDER_JOINT_LINEAR_MOTION_DAMPING,
  131. PARAM_LINEAR_ORTHOGONAL_SOFTNESS = PhysicsServer::SLIDER_JOINT_LINEAR_ORTHOGONAL_SOFTNESS,
  132. PARAM_LINEAR_ORTHOGONAL_RESTITUTION = PhysicsServer::SLIDER_JOINT_LINEAR_ORTHOGONAL_RESTITUTION,
  133. PARAM_LINEAR_ORTHOGONAL_DAMPING = PhysicsServer::SLIDER_JOINT_LINEAR_ORTHOGONAL_DAMPING,
  134. PARAM_ANGULAR_LIMIT_UPPER = PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_UPPER,
  135. PARAM_ANGULAR_LIMIT_LOWER = PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_LOWER,
  136. PARAM_ANGULAR_LIMIT_SOFTNESS = PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS,
  137. PARAM_ANGULAR_LIMIT_RESTITUTION = PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_RESTITUTION,
  138. PARAM_ANGULAR_LIMIT_DAMPING = PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_DAMPING,
  139. PARAM_ANGULAR_MOTION_SOFTNESS = PhysicsServer::SLIDER_JOINT_ANGULAR_MOTION_SOFTNESS,
  140. PARAM_ANGULAR_MOTION_RESTITUTION = PhysicsServer::SLIDER_JOINT_ANGULAR_MOTION_RESTITUTION,
  141. PARAM_ANGULAR_MOTION_DAMPING = PhysicsServer::SLIDER_JOINT_ANGULAR_MOTION_DAMPING,
  142. PARAM_ANGULAR_ORTHOGONAL_SOFTNESS = PhysicsServer::SLIDER_JOINT_ANGULAR_ORTHOGONAL_SOFTNESS,
  143. PARAM_ANGULAR_ORTHOGONAL_RESTITUTION = PhysicsServer::SLIDER_JOINT_ANGULAR_ORTHOGONAL_RESTITUTION,
  144. PARAM_ANGULAR_ORTHOGONAL_DAMPING = PhysicsServer::SLIDER_JOINT_ANGULAR_ORTHOGONAL_DAMPING,
  145. PARAM_MAX = PhysicsServer::SLIDER_JOINT_MAX
  146. };
  147. protected:
  148. void _set_upper_limit_angular(float p_limit_angular);
  149. float _get_upper_limit_angular() const;
  150. void _set_lower_limit_angular(float p_limit_angular);
  151. float _get_lower_limit_angular() const;
  152. float params[PARAM_MAX];
  153. virtual RID _configure_joint(PhysicsBody *body_a, PhysicsBody *body_b);
  154. static void _bind_methods();
  155. public:
  156. void set_param(Param p_param, float p_value);
  157. float get_param(Param p_param) const;
  158. SliderJoint();
  159. };
  160. VARIANT_ENUM_CAST(SliderJoint::Param);
  161. class ConeTwistJoint : public Joint {
  162. GDCLASS(ConeTwistJoint, Joint);
  163. public:
  164. enum Param {
  165. PARAM_SWING_SPAN,
  166. PARAM_TWIST_SPAN,
  167. PARAM_BIAS,
  168. PARAM_SOFTNESS,
  169. PARAM_RELAXATION,
  170. PARAM_MAX
  171. };
  172. protected:
  173. void _set_swing_span(float p_limit_angular);
  174. float _get_swing_span() const;
  175. void _set_twist_span(float p_limit_angular);
  176. float _get_twist_span() const;
  177. float params[PARAM_MAX];
  178. virtual RID _configure_joint(PhysicsBody *body_a, PhysicsBody *body_b);
  179. static void _bind_methods();
  180. public:
  181. void set_param(Param p_param, float p_value);
  182. float get_param(Param p_param) const;
  183. ConeTwistJoint();
  184. };
  185. VARIANT_ENUM_CAST(ConeTwistJoint::Param);
  186. class Generic6DOFJoint : public Joint {
  187. GDCLASS(Generic6DOFJoint, Joint);
  188. public:
  189. enum Param {
  190. PARAM_LINEAR_LOWER_LIMIT = PhysicsServer::G6DOF_JOINT_LINEAR_LOWER_LIMIT,
  191. PARAM_LINEAR_UPPER_LIMIT = PhysicsServer::G6DOF_JOINT_LINEAR_UPPER_LIMIT,
  192. PARAM_LINEAR_LIMIT_SOFTNESS = PhysicsServer::G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS,
  193. PARAM_LINEAR_RESTITUTION = PhysicsServer::G6DOF_JOINT_LINEAR_RESTITUTION,
  194. PARAM_LINEAR_DAMPING = PhysicsServer::G6DOF_JOINT_LINEAR_DAMPING,
  195. PARAM_LINEAR_MOTOR_TARGET_VELOCITY = PhysicsServer::G6DOF_JOINT_LINEAR_MOTOR_TARGET_VELOCITY,
  196. PARAM_LINEAR_MOTOR_FORCE_LIMIT = PhysicsServer::G6DOF_JOINT_LINEAR_MOTOR_FORCE_LIMIT,
  197. PARAM_LINEAR_SPRING_STIFFNESS = PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_STIFFNESS,
  198. PARAM_LINEAR_SPRING_DAMPING = PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_DAMPING,
  199. PARAM_LINEAR_SPRING_EQUILIBRIUM_POINT = PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_EQUILIBRIUM_POINT,
  200. PARAM_ANGULAR_LOWER_LIMIT = PhysicsServer::G6DOF_JOINT_ANGULAR_LOWER_LIMIT,
  201. PARAM_ANGULAR_UPPER_LIMIT = PhysicsServer::G6DOF_JOINT_ANGULAR_UPPER_LIMIT,
  202. PARAM_ANGULAR_LIMIT_SOFTNESS = PhysicsServer::G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS,
  203. PARAM_ANGULAR_DAMPING = PhysicsServer::G6DOF_JOINT_ANGULAR_DAMPING,
  204. PARAM_ANGULAR_RESTITUTION = PhysicsServer::G6DOF_JOINT_ANGULAR_RESTITUTION,
  205. PARAM_ANGULAR_FORCE_LIMIT = PhysicsServer::G6DOF_JOINT_ANGULAR_FORCE_LIMIT,
  206. PARAM_ANGULAR_ERP = PhysicsServer::G6DOF_JOINT_ANGULAR_ERP,
  207. PARAM_ANGULAR_MOTOR_TARGET_VELOCITY = PhysicsServer::G6DOF_JOINT_ANGULAR_MOTOR_TARGET_VELOCITY,
  208. PARAM_ANGULAR_MOTOR_FORCE_LIMIT = PhysicsServer::G6DOF_JOINT_ANGULAR_MOTOR_FORCE_LIMIT,
  209. PARAM_ANGULAR_SPRING_STIFFNESS = PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_STIFFNESS,
  210. PARAM_ANGULAR_SPRING_DAMPING = PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_DAMPING,
  211. PARAM_ANGULAR_SPRING_EQUILIBRIUM_POINT = PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_EQUILIBRIUM_POINT,
  212. PARAM_MAX = PhysicsServer::G6DOF_JOINT_MAX,
  213. };
  214. enum Flag {
  215. FLAG_ENABLE_LINEAR_LIMIT = PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT,
  216. FLAG_ENABLE_ANGULAR_LIMIT = PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT,
  217. FLAG_ENABLE_LINEAR_SPRING = PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_SPRING,
  218. FLAG_ENABLE_ANGULAR_SPRING = PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_SPRING,
  219. FLAG_ENABLE_MOTOR = PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_MOTOR,
  220. FLAG_ENABLE_LINEAR_MOTOR = PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_MOTOR,
  221. FLAG_MAX = PhysicsServer::G6DOF_JOINT_FLAG_MAX
  222. };
  223. protected:
  224. void _set_angular_hi_limit_x(float p_limit_angular);
  225. float _get_angular_hi_limit_x() const;
  226. void _set_angular_hi_limit_y(float p_limit_angular);
  227. float _get_angular_hi_limit_y() const;
  228. void _set_angular_hi_limit_z(float p_limit_angular);
  229. float _get_angular_hi_limit_z() const;
  230. void _set_angular_lo_limit_x(float p_limit_angular);
  231. float _get_angular_lo_limit_x() const;
  232. void _set_angular_lo_limit_y(float p_limit_angular);
  233. float _get_angular_lo_limit_y() const;
  234. void _set_angular_lo_limit_z(float p_limit_angular);
  235. float _get_angular_lo_limit_z() const;
  236. float params_x[PARAM_MAX];
  237. bool flags_x[FLAG_MAX];
  238. float params_y[PARAM_MAX];
  239. bool flags_y[FLAG_MAX];
  240. float params_z[PARAM_MAX];
  241. bool flags_z[FLAG_MAX];
  242. virtual RID _configure_joint(PhysicsBody *body_a, PhysicsBody *body_b);
  243. static void _bind_methods();
  244. public:
  245. void set_param_x(Param p_param, float p_value);
  246. float get_param_x(Param p_param) const;
  247. void set_param_y(Param p_param, float p_value);
  248. float get_param_y(Param p_param) const;
  249. void set_param_z(Param p_param, float p_value);
  250. float get_param_z(Param p_param) const;
  251. void set_flag_x(Flag p_flag, bool p_enabled);
  252. bool get_flag_x(Flag p_flag) const;
  253. void set_flag_y(Flag p_flag, bool p_enabled);
  254. bool get_flag_y(Flag p_flag) const;
  255. void set_flag_z(Flag p_flag, bool p_enabled);
  256. bool get_flag_z(Flag p_flag) const;
  257. Generic6DOFJoint();
  258. };
  259. VARIANT_ENUM_CAST(Generic6DOFJoint::Param);
  260. VARIANT_ENUM_CAST(Generic6DOFJoint::Flag);
  261. #endif // PHYSICS_JOINT_H