physics_joint.h 12 KB

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