physics_joint.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*************************************************************************/
  2. /* physics_joint.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef PHYSICS_JOINT_H
  30. #define PHYSICS_JOINT_H
  31. #include "scene/3d/spatial.h"
  32. #include "scene/3d/physics_body.h"
  33. class Joint : public Spatial {
  34. OBJ_TYPE(Joint,Spatial);
  35. RID ba,bb;
  36. RID joint;
  37. NodePath a;
  38. NodePath b;
  39. int solver_priority;
  40. bool exclude_from_collision;
  41. protected:
  42. void _update_joint(bool p_only_free=false);
  43. void _notification(int p_what);
  44. virtual RID _configure_joint(PhysicsBody *body_a,PhysicsBody *body_b)=0;
  45. static void _bind_methods();
  46. public:
  47. void set_node_a(const NodePath& p_node_a);
  48. NodePath get_node_a() const;
  49. void set_node_b(const NodePath& p_node_b);
  50. NodePath get_node_b() const;
  51. void set_solver_priority(int p_priority);
  52. int get_solver_priority() const;
  53. void set_exclude_nodes_from_collision(bool p_enable);
  54. bool get_exclude_nodes_from_collision() const;
  55. RID get_joint() const { return joint; }
  56. Joint();
  57. };
  58. ///////////////////////////////////////////
  59. class PinJoint : public Joint {
  60. OBJ_TYPE(PinJoint,Joint);
  61. public:
  62. enum Param {
  63. PARAM_BIAS=PhysicsServer::PIN_JOINT_BIAS,
  64. PARAM_DAMPING=PhysicsServer::PIN_JOINT_DAMPING,
  65. PARAM_IMPULSE_CLAMP=PhysicsServer::PIN_JOINT_IMPULSE_CLAMP
  66. };
  67. protected:
  68. float params[3];
  69. virtual RID _configure_joint(PhysicsBody *body_a, PhysicsBody *body_b);
  70. static void _bind_methods();
  71. public:
  72. void set_param(Param p_param,float p_value);
  73. float get_param(Param p_param) const;
  74. PinJoint();
  75. };
  76. VARIANT_ENUM_CAST(PinJoint::Param);
  77. class HingeJoint : public Joint {
  78. OBJ_TYPE(HingeJoint,Joint);
  79. public:
  80. enum Param {
  81. PARAM_BIAS=PhysicsServer::HINGE_JOINT_BIAS,
  82. PARAM_LIMIT_UPPER=PhysicsServer::HINGE_JOINT_LIMIT_UPPER,
  83. PARAM_LIMIT_LOWER=PhysicsServer::HINGE_JOINT_LIMIT_LOWER,
  84. PARAM_LIMIT_BIAS=PhysicsServer::HINGE_JOINT_LIMIT_BIAS,
  85. PARAM_LIMIT_SOFTNESS=PhysicsServer::HINGE_JOINT_LIMIT_SOFTNESS,
  86. PARAM_LIMIT_RELAXATION=PhysicsServer::HINGE_JOINT_LIMIT_RELAXATION,
  87. PARAM_MOTOR_TARGET_VELOCITY=PhysicsServer::HINGE_JOINT_MOTOR_TARGET_VELOCITY,
  88. PARAM_MOTOR_MAX_IMPULSE=PhysicsServer::HINGE_JOINT_MOTOR_MAX_IMPULSE,
  89. PARAM_MAX=PhysicsServer::HINGE_JOINT_MAX
  90. };
  91. enum Flag {
  92. FLAG_USE_LIMIT=PhysicsServer::HINGE_JOINT_FLAG_USE_LIMIT,
  93. FLAG_ENABLE_MOTOR=PhysicsServer::HINGE_JOINT_FLAG_ENABLE_MOTOR,
  94. FLAG_MAX=PhysicsServer::HINGE_JOINT_FLAG_MAX
  95. };
  96. protected:
  97. float params[PARAM_MAX];
  98. bool flags[FLAG_MAX];
  99. virtual RID _configure_joint(PhysicsBody *body_a, PhysicsBody *body_b);
  100. static void _bind_methods();
  101. void _set_upper_limit(float p_limit);
  102. float _get_upper_limit() const;
  103. void _set_lower_limit(float p_limit);
  104. float _get_lower_limit() const;
  105. public:
  106. void set_param(Param p_param,float p_value);
  107. float get_param(Param p_param) const;
  108. void set_flag(Flag p_flag,bool p_value);
  109. bool get_flag(Flag p_flag) const;
  110. HingeJoint();
  111. };
  112. VARIANT_ENUM_CAST(HingeJoint::Param);
  113. VARIANT_ENUM_CAST(HingeJoint::Flag);
  114. class SliderJoint : public Joint {
  115. OBJ_TYPE(SliderJoint,Joint);
  116. public:
  117. enum Param {
  118. PARAM_LINEAR_LIMIT_UPPER=PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_UPPER,
  119. PARAM_LINEAR_LIMIT_LOWER=PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_LOWER,
  120. PARAM_LINEAR_LIMIT_SOFTNESS=PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS,
  121. PARAM_LINEAR_LIMIT_RESTITUTION=PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION,
  122. PARAM_LINEAR_LIMIT_DAMPING=PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_DAMPING,
  123. PARAM_LINEAR_MOTION_SOFTNESS=PhysicsServer::SLIDER_JOINT_LINEAR_MOTION_SOFTNESS,
  124. PARAM_LINEAR_MOTION_RESTITUTION=PhysicsServer::SLIDER_JOINT_LINEAR_MOTION_RESTITUTION,
  125. PARAM_LINEAR_MOTION_DAMPING=PhysicsServer::SLIDER_JOINT_LINEAR_MOTION_DAMPING,
  126. PARAM_LINEAR_ORTHOGONAL_SOFTNESS=PhysicsServer::SLIDER_JOINT_LINEAR_ORTHOGONAL_SOFTNESS,
  127. PARAM_LINEAR_ORTHOGONAL_RESTITUTION=PhysicsServer::SLIDER_JOINT_LINEAR_ORTHOGONAL_RESTITUTION,
  128. PARAM_LINEAR_ORTHOGONAL_DAMPING=PhysicsServer::SLIDER_JOINT_LINEAR_ORTHOGONAL_DAMPING,
  129. PARAM_ANGULAR_LIMIT_UPPER=PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_UPPER,
  130. PARAM_ANGULAR_LIMIT_LOWER=PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_LOWER,
  131. PARAM_ANGULAR_LIMIT_SOFTNESS=PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS,
  132. PARAM_ANGULAR_LIMIT_RESTITUTION=PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_RESTITUTION,
  133. PARAM_ANGULAR_LIMIT_DAMPING=PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_DAMPING,
  134. PARAM_ANGULAR_MOTION_SOFTNESS=PhysicsServer::SLIDER_JOINT_ANGULAR_MOTION_SOFTNESS,
  135. PARAM_ANGULAR_MOTION_RESTITUTION=PhysicsServer::SLIDER_JOINT_ANGULAR_MOTION_RESTITUTION,
  136. PARAM_ANGULAR_MOTION_DAMPING=PhysicsServer::SLIDER_JOINT_ANGULAR_MOTION_DAMPING,
  137. PARAM_ANGULAR_ORTHOGONAL_SOFTNESS=PhysicsServer::SLIDER_JOINT_ANGULAR_ORTHOGONAL_SOFTNESS,
  138. PARAM_ANGULAR_ORTHOGONAL_RESTITUTION=PhysicsServer::SLIDER_JOINT_ANGULAR_ORTHOGONAL_RESTITUTION,
  139. PARAM_ANGULAR_ORTHOGONAL_DAMPING=PhysicsServer::SLIDER_JOINT_ANGULAR_ORTHOGONAL_DAMPING,
  140. PARAM_MAX=PhysicsServer::SLIDER_JOINT_MAX
  141. };
  142. protected:
  143. void _set_upper_limit_angular(float p_limit_angular);
  144. float _get_upper_limit_angular() const;
  145. void _set_lower_limit_angular(float p_limit_angular);
  146. float _get_lower_limit_angular() const;
  147. float params[PARAM_MAX];
  148. virtual RID _configure_joint(PhysicsBody *body_a, PhysicsBody *body_b);
  149. static void _bind_methods();
  150. public:
  151. void set_param(Param p_param,float p_value);
  152. float get_param(Param p_param) const;
  153. SliderJoint();
  154. };
  155. VARIANT_ENUM_CAST(SliderJoint::Param);
  156. class ConeTwistJoint : public Joint {
  157. OBJ_TYPE(ConeTwistJoint,Joint);
  158. public:
  159. enum Param {
  160. PARAM_SWING_SPAN,
  161. PARAM_TWIST_SPAN,
  162. PARAM_BIAS,
  163. PARAM_SOFTNESS,
  164. PARAM_RELAXATION,
  165. PARAM_MAX
  166. };
  167. protected:
  168. void _set_swing_span(float p_limit_angular);
  169. float _get_swing_span() const;
  170. void _set_twist_span(float p_limit_angular);
  171. float _get_twist_span() const;
  172. float params[PARAM_MAX];
  173. virtual RID _configure_joint(PhysicsBody *body_a, PhysicsBody *body_b);
  174. static void _bind_methods();
  175. public:
  176. void set_param(Param p_param,float p_value);
  177. float get_param(Param p_param) const;
  178. ConeTwistJoint();
  179. };
  180. VARIANT_ENUM_CAST(ConeTwistJoint::Param);
  181. class Generic6DOFJoint : public Joint {
  182. OBJ_TYPE(Generic6DOFJoint,Joint);
  183. public:
  184. enum Param {
  185. PARAM_LINEAR_LOWER_LIMIT=PhysicsServer::G6DOF_JOINT_LINEAR_LOWER_LIMIT,
  186. PARAM_LINEAR_UPPER_LIMIT=PhysicsServer::G6DOF_JOINT_LINEAR_UPPER_LIMIT,
  187. PARAM_LINEAR_LIMIT_SOFTNESS=PhysicsServer::G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS,
  188. PARAM_LINEAR_RESTITUTION=PhysicsServer::G6DOF_JOINT_LINEAR_RESTITUTION,
  189. PARAM_LINEAR_DAMPING=PhysicsServer::G6DOF_JOINT_LINEAR_DAMPING,
  190. PARAM_ANGULAR_LOWER_LIMIT=PhysicsServer::G6DOF_JOINT_ANGULAR_LOWER_LIMIT,
  191. PARAM_ANGULAR_UPPER_LIMIT=PhysicsServer::G6DOF_JOINT_ANGULAR_UPPER_LIMIT,
  192. PARAM_ANGULAR_LIMIT_SOFTNESS=PhysicsServer::G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS,
  193. PARAM_ANGULAR_DAMPING=PhysicsServer::G6DOF_JOINT_ANGULAR_DAMPING,
  194. PARAM_ANGULAR_RESTITUTION=PhysicsServer::G6DOF_JOINT_ANGULAR_RESTITUTION,
  195. PARAM_ANGULAR_FORCE_LIMIT=PhysicsServer::G6DOF_JOINT_ANGULAR_FORCE_LIMIT,
  196. PARAM_ANGULAR_ERP=PhysicsServer::G6DOF_JOINT_ANGULAR_ERP,
  197. PARAM_ANGULAR_MOTOR_TARGET_VELOCITY=PhysicsServer::G6DOF_JOINT_ANGULAR_MOTOR_TARGET_VELOCITY,
  198. PARAM_ANGULAR_MOTOR_FORCE_LIMIT=PhysicsServer::G6DOF_JOINT_ANGULAR_MOTOR_FORCE_LIMIT,
  199. PARAM_MAX=PhysicsServer::G6DOF_JOINT_MAX,
  200. };
  201. enum Flag {
  202. FLAG_ENABLE_LINEAR_LIMIT=PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT,
  203. FLAG_ENABLE_ANGULAR_LIMIT=PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT,
  204. FLAG_ENABLE_MOTOR=PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_MOTOR,
  205. FLAG_MAX=PhysicsServer::G6DOF_JOINT_FLAG_MAX
  206. };
  207. protected:
  208. void _set_angular_hi_limit_x(float p_limit_angular);
  209. float _get_angular_hi_limit_x() const;
  210. void _set_angular_hi_limit_y(float p_limit_angular);
  211. float _get_angular_hi_limit_y() const;
  212. void _set_angular_hi_limit_z(float p_limit_angular);
  213. float _get_angular_hi_limit_z() const;
  214. void _set_angular_lo_limit_x(float p_limit_angular);
  215. float _get_angular_lo_limit_x() const;
  216. void _set_angular_lo_limit_y(float p_limit_angular);
  217. float _get_angular_lo_limit_y() const;
  218. void _set_angular_lo_limit_z(float p_limit_angular);
  219. float _get_angular_lo_limit_z() const;
  220. float params_x[PARAM_MAX];
  221. bool flags_x[FLAG_MAX];
  222. float params_y[PARAM_MAX];
  223. bool flags_y[FLAG_MAX];
  224. float params_z[PARAM_MAX];
  225. bool flags_z[FLAG_MAX];
  226. virtual RID _configure_joint(PhysicsBody *body_a, PhysicsBody *body_b);
  227. static void _bind_methods();
  228. public:
  229. void set_param_x(Param p_param,float p_value);
  230. float get_param_x(Param p_param) const;
  231. void set_param_y(Param p_param,float p_value);
  232. float get_param_y(Param p_param) const;
  233. void set_param_z(Param p_param,float p_value);
  234. float get_param_z(Param p_param) const;
  235. void set_flag_x(Flag p_flag,bool p_enabled);
  236. bool get_flag_x(Flag p_flag) const;
  237. void set_flag_y(Flag p_flag,bool p_enabled);
  238. bool get_flag_y(Flag p_flag) const;
  239. void set_flag_z(Flag p_flag,bool p_enabled);
  240. bool get_flag_z(Flag p_flag) const;
  241. Generic6DOFJoint();
  242. };
  243. VARIANT_ENUM_CAST(Generic6DOFJoint::Param);
  244. VARIANT_ENUM_CAST(Generic6DOFJoint::Flag);
  245. #if 0
  246. class PhysicsJoint : public Spatial {
  247. OBJ_TYPE(PhysicsJoint,Spatial);
  248. OBJ_CATEGORY("3D Physics Nodes");
  249. NodePath body_A;
  250. NodePath body_B;
  251. bool active;
  252. bool no_collision;
  253. RID indicator_instance;
  254. RID _get_visual_instance_rid() const;
  255. protected:
  256. RID joint;
  257. RID indicator;
  258. bool _set(const StringName& p_name, const Variant& p_value);
  259. bool _get(const StringName& p_name,Variant &r_ret) const;
  260. void _get_property_list( List<PropertyInfo> *p_list) const;
  261. void _notification(int p_what);
  262. static void _bind_methods();
  263. virtual RID create(PhysicsBody*A,PhysicsBody*B)=0;
  264. virtual void _update_indicator()=0;
  265. void _disconnect();
  266. void _connect();
  267. public:
  268. void set_body_A(const NodePath& p_path);
  269. void set_body_B(const NodePath& p_path);
  270. NodePath get_body_A() const;
  271. NodePath get_body_B() const;
  272. void set_active(bool p_active);
  273. bool is_active() const;
  274. void set_disable_collision(bool p_active);
  275. bool has_disable_collision() const;
  276. void reconnect();
  277. RID get_rid();
  278. PhysicsJoint();
  279. ~PhysicsJoint();
  280. };
  281. class PhysicsJointPin : public PhysicsJoint {
  282. OBJ_TYPE( PhysicsJointPin, PhysicsJoint );
  283. protected:
  284. virtual void _update_indicator();
  285. virtual RID create(PhysicsBody*A,PhysicsBody*B);
  286. public:
  287. PhysicsJointPin();
  288. };
  289. #endif // PHYSICS_JOINT_H
  290. #endif