godot_joints_2d.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**************************************************************************/
  2. /* godot_joints_2d.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 GODOT_JOINTS_2D_H
  31. #define GODOT_JOINTS_2D_H
  32. #include "godot_body_2d.h"
  33. #include "godot_constraint_2d.h"
  34. class GodotJoint2D : public GodotConstraint2D {
  35. real_t bias = 0;
  36. real_t max_bias = 3.40282e+38;
  37. real_t max_force = 3.40282e+38;
  38. protected:
  39. bool dynamic_A = false;
  40. bool dynamic_B = false;
  41. public:
  42. _FORCE_INLINE_ void set_max_force(real_t p_force) { max_force = p_force; }
  43. _FORCE_INLINE_ real_t get_max_force() const { return max_force; }
  44. _FORCE_INLINE_ void set_bias(real_t p_bias) { bias = p_bias; }
  45. _FORCE_INLINE_ real_t get_bias() const { return bias; }
  46. _FORCE_INLINE_ void set_max_bias(real_t p_bias) { max_bias = p_bias; }
  47. _FORCE_INLINE_ real_t get_max_bias() const { return max_bias; }
  48. virtual bool setup(real_t p_step) override { return false; }
  49. virtual bool pre_solve(real_t p_step) override { return false; }
  50. virtual void solve(real_t p_step) override {}
  51. void copy_settings_from(GodotJoint2D *p_joint);
  52. virtual PhysicsServer2D::JointType get_type() const { return PhysicsServer2D::JOINT_TYPE_MAX; }
  53. GodotJoint2D(GodotBody2D **p_body_ptr = nullptr, int p_body_count = 0) :
  54. GodotConstraint2D(p_body_ptr, p_body_count) {}
  55. virtual ~GodotJoint2D() {
  56. for (int i = 0; i < get_body_count(); i++) {
  57. GodotBody2D *body = get_body_ptr()[i];
  58. if (body) {
  59. body->remove_constraint(this, i);
  60. }
  61. }
  62. }
  63. };
  64. class GodotPinJoint2D : public GodotJoint2D {
  65. union {
  66. struct {
  67. GodotBody2D *A;
  68. GodotBody2D *B;
  69. };
  70. GodotBody2D *_arr[2] = { nullptr, nullptr };
  71. };
  72. Transform2D M;
  73. Vector2 rA, rB;
  74. Vector2 anchor_A;
  75. Vector2 anchor_B;
  76. Vector2 bias;
  77. real_t initial_angle = 0.0;
  78. real_t bias_velocity = 0.0;
  79. real_t jn_max = 0.0;
  80. real_t j_acc = 0.0;
  81. real_t i_sum = 0.0;
  82. Vector2 P;
  83. real_t softness = 0.0;
  84. real_t angular_limit_lower = 0.0;
  85. real_t angular_limit_upper = 0.0;
  86. real_t motor_target_velocity = 0.0;
  87. bool is_joint_at_limit = false;
  88. bool motor_enabled = false;
  89. bool angular_limit_enabled = false;
  90. public:
  91. virtual PhysicsServer2D::JointType get_type() const override { return PhysicsServer2D::JOINT_TYPE_PIN; }
  92. virtual bool setup(real_t p_step) override;
  93. virtual bool pre_solve(real_t p_step) override;
  94. virtual void solve(real_t p_step) override;
  95. void set_param(PhysicsServer2D::PinJointParam p_param, real_t p_value);
  96. real_t get_param(PhysicsServer2D::PinJointParam p_param) const;
  97. void set_flag(PhysicsServer2D::PinJointFlag p_flag, bool p_enabled);
  98. bool get_flag(PhysicsServer2D::PinJointFlag p_flag) const;
  99. GodotPinJoint2D(const Vector2 &p_pos, GodotBody2D *p_body_a, GodotBody2D *p_body_b = nullptr);
  100. };
  101. class GodotGrooveJoint2D : public GodotJoint2D {
  102. union {
  103. struct {
  104. GodotBody2D *A;
  105. GodotBody2D *B;
  106. };
  107. GodotBody2D *_arr[2] = { nullptr, nullptr };
  108. };
  109. Vector2 A_groove_1;
  110. Vector2 A_groove_2;
  111. Vector2 A_groove_normal;
  112. Vector2 B_anchor;
  113. Vector2 jn_acc;
  114. Vector2 gbias;
  115. real_t jn_max = 0.0;
  116. real_t clamp = 0.0;
  117. Vector2 xf_normal;
  118. Vector2 rA, rB;
  119. Vector2 k1, k2;
  120. bool correct = false;
  121. public:
  122. virtual PhysicsServer2D::JointType get_type() const override { return PhysicsServer2D::JOINT_TYPE_GROOVE; }
  123. virtual bool setup(real_t p_step) override;
  124. virtual bool pre_solve(real_t p_step) override;
  125. virtual void solve(real_t p_step) override;
  126. GodotGrooveJoint2D(const Vector2 &p_a_groove1, const Vector2 &p_a_groove2, const Vector2 &p_b_anchor, GodotBody2D *p_body_a, GodotBody2D *p_body_b);
  127. };
  128. class GodotDampedSpringJoint2D : public GodotJoint2D {
  129. union {
  130. struct {
  131. GodotBody2D *A;
  132. GodotBody2D *B;
  133. };
  134. GodotBody2D *_arr[2] = { nullptr, nullptr };
  135. };
  136. Vector2 anchor_A;
  137. Vector2 anchor_B;
  138. real_t rest_length = 0.0;
  139. real_t damping = 1.5;
  140. real_t stiffness = 20.0;
  141. Vector2 rA, rB;
  142. Vector2 n;
  143. Vector2 j;
  144. real_t n_mass = 0.0;
  145. real_t target_vrn = 0.0;
  146. real_t v_coef = 0.0;
  147. public:
  148. virtual PhysicsServer2D::JointType get_type() const override { return PhysicsServer2D::JOINT_TYPE_DAMPED_SPRING; }
  149. virtual bool setup(real_t p_step) override;
  150. virtual bool pre_solve(real_t p_step) override;
  151. virtual void solve(real_t p_step) override;
  152. void set_param(PhysicsServer2D::DampedSpringParam p_param, real_t p_value);
  153. real_t get_param(PhysicsServer2D::DampedSpringParam p_param) const;
  154. GodotDampedSpringJoint2D(const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, GodotBody2D *p_body_a, GodotBody2D *p_body_b);
  155. };
  156. #endif // GODOT_JOINTS_2D_H