godot_joints_2d.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. Vector2 P;
  78. real_t softness = 0.0;
  79. public:
  80. virtual PhysicsServer2D::JointType get_type() const override { return PhysicsServer2D::JOINT_TYPE_PIN; }
  81. virtual bool setup(real_t p_step) override;
  82. virtual bool pre_solve(real_t p_step) override;
  83. virtual void solve(real_t p_step) override;
  84. void set_param(PhysicsServer2D::PinJointParam p_param, real_t p_value);
  85. real_t get_param(PhysicsServer2D::PinJointParam p_param) const;
  86. GodotPinJoint2D(const Vector2 &p_pos, GodotBody2D *p_body_a, GodotBody2D *p_body_b = nullptr);
  87. };
  88. class GodotGrooveJoint2D : public GodotJoint2D {
  89. union {
  90. struct {
  91. GodotBody2D *A;
  92. GodotBody2D *B;
  93. };
  94. GodotBody2D *_arr[2] = { nullptr, nullptr };
  95. };
  96. Vector2 A_groove_1;
  97. Vector2 A_groove_2;
  98. Vector2 A_groove_normal;
  99. Vector2 B_anchor;
  100. Vector2 jn_acc;
  101. Vector2 gbias;
  102. real_t jn_max = 0.0;
  103. real_t clamp = 0.0;
  104. Vector2 xf_normal;
  105. Vector2 rA, rB;
  106. Vector2 k1, k2;
  107. bool correct = false;
  108. public:
  109. virtual PhysicsServer2D::JointType get_type() const override { return PhysicsServer2D::JOINT_TYPE_GROOVE; }
  110. virtual bool setup(real_t p_step) override;
  111. virtual bool pre_solve(real_t p_step) override;
  112. virtual void solve(real_t p_step) override;
  113. GodotGrooveJoint2D(const Vector2 &p_a_groove1, const Vector2 &p_a_groove2, const Vector2 &p_b_anchor, GodotBody2D *p_body_a, GodotBody2D *p_body_b);
  114. };
  115. class GodotDampedSpringJoint2D : public GodotJoint2D {
  116. union {
  117. struct {
  118. GodotBody2D *A;
  119. GodotBody2D *B;
  120. };
  121. GodotBody2D *_arr[2] = { nullptr, nullptr };
  122. };
  123. Vector2 anchor_A;
  124. Vector2 anchor_B;
  125. real_t rest_length = 0.0;
  126. real_t damping = 1.5;
  127. real_t stiffness = 20.0;
  128. Vector2 rA, rB;
  129. Vector2 n;
  130. Vector2 j;
  131. real_t n_mass = 0.0;
  132. real_t target_vrn = 0.0;
  133. real_t v_coef = 0.0;
  134. public:
  135. virtual PhysicsServer2D::JointType get_type() const override { return PhysicsServer2D::JOINT_TYPE_DAMPED_SPRING; }
  136. virtual bool setup(real_t p_step) override;
  137. virtual bool pre_solve(real_t p_step) override;
  138. virtual void solve(real_t p_step) override;
  139. void set_param(PhysicsServer2D::DampedSpringParam p_param, real_t p_value);
  140. real_t get_param(PhysicsServer2D::DampedSpringParam p_param) const;
  141. GodotDampedSpringJoint2D(const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, GodotBody2D *p_body_a, GodotBody2D *p_body_b);
  142. };
  143. #endif // GODOT_JOINTS_2D_H