joints_2d_sw.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*************************************************************************/
  2. /* joints_2d_sw.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 JOINTS_2D_SW_H
  31. #define JOINTS_2D_SW_H
  32. #include "body_2d_sw.h"
  33. #include "constraint_2d_sw.h"
  34. class Joint2DSW : public Constraint2DSW {
  35. real_t max_force;
  36. real_t bias;
  37. real_t max_bias;
  38. public:
  39. _FORCE_INLINE_ void set_max_force(real_t p_force) { max_force = p_force; }
  40. _FORCE_INLINE_ real_t get_max_force() const { return max_force; }
  41. _FORCE_INLINE_ void set_bias(real_t p_bias) { bias = p_bias; }
  42. _FORCE_INLINE_ real_t get_bias() const { return bias; }
  43. _FORCE_INLINE_ void set_max_bias(real_t p_bias) { max_bias = p_bias; }
  44. _FORCE_INLINE_ real_t get_max_bias() const { return max_bias; }
  45. virtual Physics2DServer::JointType get_type() const = 0;
  46. Joint2DSW(Body2DSW **p_body_ptr = NULL, int p_body_count = 0) :
  47. Constraint2DSW(p_body_ptr, p_body_count) {
  48. bias = 0;
  49. max_force = max_bias = 3.40282e+38;
  50. };
  51. };
  52. class PinJoint2DSW : public Joint2DSW {
  53. union {
  54. struct {
  55. Body2DSW *A;
  56. Body2DSW *B;
  57. };
  58. Body2DSW *_arr[2];
  59. };
  60. Transform2D M;
  61. Vector2 rA, rB;
  62. Vector2 anchor_A;
  63. Vector2 anchor_B;
  64. Vector2 bias;
  65. Vector2 P;
  66. real_t softness;
  67. public:
  68. virtual Physics2DServer::JointType get_type() const { return Physics2DServer::JOINT_PIN; }
  69. virtual bool setup(real_t p_step);
  70. virtual void solve(real_t p_step);
  71. void set_param(Physics2DServer::PinJointParam p_param, real_t p_value);
  72. real_t get_param(Physics2DServer::PinJointParam p_param) const;
  73. PinJoint2DSW(const Vector2 &p_pos, Body2DSW *p_body_a, Body2DSW *p_body_b = NULL);
  74. ~PinJoint2DSW();
  75. };
  76. class GrooveJoint2DSW : public Joint2DSW {
  77. union {
  78. struct {
  79. Body2DSW *A;
  80. Body2DSW *B;
  81. };
  82. Body2DSW *_arr[2];
  83. };
  84. Vector2 A_groove_1;
  85. Vector2 A_groove_2;
  86. Vector2 A_groove_normal;
  87. Vector2 B_anchor;
  88. Vector2 jn_acc;
  89. Vector2 gbias;
  90. real_t jn_max;
  91. real_t clamp;
  92. Vector2 xf_normal;
  93. Vector2 rA, rB;
  94. Vector2 k1, k2;
  95. bool correct;
  96. public:
  97. virtual Physics2DServer::JointType get_type() const { return Physics2DServer::JOINT_GROOVE; }
  98. virtual bool setup(real_t p_step);
  99. virtual void solve(real_t p_step);
  100. GrooveJoint2DSW(const Vector2 &p_a_groove1, const Vector2 &p_a_groove2, const Vector2 &p_b_anchor, Body2DSW *p_body_a, Body2DSW *p_body_b);
  101. ~GrooveJoint2DSW();
  102. };
  103. class DampedSpringJoint2DSW : public Joint2DSW {
  104. union {
  105. struct {
  106. Body2DSW *A;
  107. Body2DSW *B;
  108. };
  109. Body2DSW *_arr[2];
  110. };
  111. Vector2 anchor_A;
  112. Vector2 anchor_B;
  113. real_t rest_length;
  114. real_t damping;
  115. real_t stiffness;
  116. Vector2 rA, rB;
  117. Vector2 n;
  118. real_t n_mass;
  119. real_t target_vrn;
  120. real_t v_coef;
  121. public:
  122. virtual Physics2DServer::JointType get_type() const { return Physics2DServer::JOINT_DAMPED_SPRING; }
  123. virtual bool setup(real_t p_step);
  124. virtual void solve(real_t p_step);
  125. void set_param(Physics2DServer::DampedStringParam p_param, real_t p_value);
  126. real_t get_param(Physics2DServer::DampedStringParam p_param) const;
  127. DampedSpringJoint2DSW(const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, Body2DSW *p_body_a, Body2DSW *p_body_b);
  128. ~DampedSpringJoint2DSW();
  129. };
  130. #endif // JOINTS_2D_SW_H