slider_joint_sw.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*************************************************************************/
  2. /* slider_joint_sw.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. /*
  31. Adapted to Godot from the Bullet library.
  32. */
  33. #ifndef SLIDER_JOINT_SW_H
  34. #define SLIDER_JOINT_SW_H
  35. #include "servers/physics/joints/jacobian_entry_sw.h"
  36. #include "servers/physics/joints_sw.h"
  37. /*
  38. Bullet Continuous Collision Detection and Physics Library
  39. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  40. This software is provided 'as-is', without any express or implied warranty.
  41. In no event will the authors be held liable for any damages arising from the use of this software.
  42. Permission is granted to anyone to use this software for any purpose,
  43. including commercial applications, and to alter it and redistribute it freely,
  44. subject to the following restrictions:
  45. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  46. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  47. 3. This notice may not be removed or altered from any source distribution.
  48. */
  49. /*
  50. Added by Roman Ponomarev (rponom@gmail.com)
  51. April 04, 2008
  52. */
  53. #define SLIDER_CONSTRAINT_DEF_SOFTNESS (real_t(1.0))
  54. #define SLIDER_CONSTRAINT_DEF_DAMPING (real_t(1.0))
  55. #define SLIDER_CONSTRAINT_DEF_RESTITUTION (real_t(0.7))
  56. //-----------------------------------------------------------------------------
  57. class SliderJointSW : public JointSW {
  58. protected:
  59. union {
  60. struct {
  61. BodySW *A;
  62. BodySW *B;
  63. };
  64. BodySW *_arr[2];
  65. };
  66. Transform m_frameInA;
  67. Transform m_frameInB;
  68. // linear limits
  69. real_t m_lowerLinLimit;
  70. real_t m_upperLinLimit;
  71. // angular limits
  72. real_t m_lowerAngLimit;
  73. real_t m_upperAngLimit;
  74. // softness, restitution and damping for different cases
  75. // DirLin - moving inside linear limits
  76. // LimLin - hitting linear limit
  77. // DirAng - moving inside angular limits
  78. // LimAng - hitting angular limit
  79. // OrthoLin, OrthoAng - against constraint axis
  80. real_t m_softnessDirLin;
  81. real_t m_restitutionDirLin;
  82. real_t m_dampingDirLin;
  83. real_t m_softnessDirAng;
  84. real_t m_restitutionDirAng;
  85. real_t m_dampingDirAng;
  86. real_t m_softnessLimLin;
  87. real_t m_restitutionLimLin;
  88. real_t m_dampingLimLin;
  89. real_t m_softnessLimAng;
  90. real_t m_restitutionLimAng;
  91. real_t m_dampingLimAng;
  92. real_t m_softnessOrthoLin;
  93. real_t m_restitutionOrthoLin;
  94. real_t m_dampingOrthoLin;
  95. real_t m_softnessOrthoAng;
  96. real_t m_restitutionOrthoAng;
  97. real_t m_dampingOrthoAng;
  98. // for interlal use
  99. bool m_solveLinLim;
  100. bool m_solveAngLim;
  101. JacobianEntrySW m_jacLin[3];
  102. real_t m_jacLinDiagABInv[3];
  103. JacobianEntrySW m_jacAng[3];
  104. real_t m_timeStep;
  105. Transform m_calculatedTransformA;
  106. Transform m_calculatedTransformB;
  107. Vector3 m_sliderAxis;
  108. Vector3 m_realPivotAInW;
  109. Vector3 m_realPivotBInW;
  110. Vector3 m_projPivotInW;
  111. Vector3 m_delta;
  112. Vector3 m_depth;
  113. Vector3 m_relPosA;
  114. Vector3 m_relPosB;
  115. real_t m_linPos;
  116. real_t m_angDepth;
  117. real_t m_kAngle;
  118. bool m_poweredLinMotor;
  119. real_t m_targetLinMotorVelocity;
  120. real_t m_maxLinMotorForce;
  121. real_t m_accumulatedLinMotorImpulse;
  122. bool m_poweredAngMotor;
  123. real_t m_targetAngMotorVelocity;
  124. real_t m_maxAngMotorForce;
  125. real_t m_accumulatedAngMotorImpulse;
  126. //------------------------
  127. void initParams();
  128. public:
  129. // constructors
  130. SliderJointSW(BodySW *rbA, BodySW *rbB, const Transform &frameInA, const Transform &frameInB);
  131. //SliderJointSW();
  132. // overrides
  133. // access
  134. const BodySW *getRigidBodyA() const { return A; }
  135. const BodySW *getRigidBodyB() const { return B; }
  136. const Transform &getCalculatedTransformA() const { return m_calculatedTransformA; }
  137. const Transform &getCalculatedTransformB() const { return m_calculatedTransformB; }
  138. const Transform &getFrameOffsetA() const { return m_frameInA; }
  139. const Transform &getFrameOffsetB() const { return m_frameInB; }
  140. Transform &getFrameOffsetA() { return m_frameInA; }
  141. Transform &getFrameOffsetB() { return m_frameInB; }
  142. real_t getLowerLinLimit() { return m_lowerLinLimit; }
  143. void setLowerLinLimit(real_t lowerLimit) { m_lowerLinLimit = lowerLimit; }
  144. real_t getUpperLinLimit() { return m_upperLinLimit; }
  145. void setUpperLinLimit(real_t upperLimit) { m_upperLinLimit = upperLimit; }
  146. real_t getLowerAngLimit() { return m_lowerAngLimit; }
  147. void setLowerAngLimit(real_t lowerLimit) { m_lowerAngLimit = lowerLimit; }
  148. real_t getUpperAngLimit() { return m_upperAngLimit; }
  149. void setUpperAngLimit(real_t upperLimit) { m_upperAngLimit = upperLimit; }
  150. real_t getSoftnessDirLin() { return m_softnessDirLin; }
  151. real_t getRestitutionDirLin() { return m_restitutionDirLin; }
  152. real_t getDampingDirLin() { return m_dampingDirLin; }
  153. real_t getSoftnessDirAng() { return m_softnessDirAng; }
  154. real_t getRestitutionDirAng() { return m_restitutionDirAng; }
  155. real_t getDampingDirAng() { return m_dampingDirAng; }
  156. real_t getSoftnessLimLin() { return m_softnessLimLin; }
  157. real_t getRestitutionLimLin() { return m_restitutionLimLin; }
  158. real_t getDampingLimLin() { return m_dampingLimLin; }
  159. real_t getSoftnessLimAng() { return m_softnessLimAng; }
  160. real_t getRestitutionLimAng() { return m_restitutionLimAng; }
  161. real_t getDampingLimAng() { return m_dampingLimAng; }
  162. real_t getSoftnessOrthoLin() { return m_softnessOrthoLin; }
  163. real_t getRestitutionOrthoLin() { return m_restitutionOrthoLin; }
  164. real_t getDampingOrthoLin() { return m_dampingOrthoLin; }
  165. real_t getSoftnessOrthoAng() { return m_softnessOrthoAng; }
  166. real_t getRestitutionOrthoAng() { return m_restitutionOrthoAng; }
  167. real_t getDampingOrthoAng() { return m_dampingOrthoAng; }
  168. void setSoftnessDirLin(real_t softnessDirLin) { m_softnessDirLin = softnessDirLin; }
  169. void setRestitutionDirLin(real_t restitutionDirLin) { m_restitutionDirLin = restitutionDirLin; }
  170. void setDampingDirLin(real_t dampingDirLin) { m_dampingDirLin = dampingDirLin; }
  171. void setSoftnessDirAng(real_t softnessDirAng) { m_softnessDirAng = softnessDirAng; }
  172. void setRestitutionDirAng(real_t restitutionDirAng) { m_restitutionDirAng = restitutionDirAng; }
  173. void setDampingDirAng(real_t dampingDirAng) { m_dampingDirAng = dampingDirAng; }
  174. void setSoftnessLimLin(real_t softnessLimLin) { m_softnessLimLin = softnessLimLin; }
  175. void setRestitutionLimLin(real_t restitutionLimLin) { m_restitutionLimLin = restitutionLimLin; }
  176. void setDampingLimLin(real_t dampingLimLin) { m_dampingLimLin = dampingLimLin; }
  177. void setSoftnessLimAng(real_t softnessLimAng) { m_softnessLimAng = softnessLimAng; }
  178. void setRestitutionLimAng(real_t restitutionLimAng) { m_restitutionLimAng = restitutionLimAng; }
  179. void setDampingLimAng(real_t dampingLimAng) { m_dampingLimAng = dampingLimAng; }
  180. void setSoftnessOrthoLin(real_t softnessOrthoLin) { m_softnessOrthoLin = softnessOrthoLin; }
  181. void setRestitutionOrthoLin(real_t restitutionOrthoLin) { m_restitutionOrthoLin = restitutionOrthoLin; }
  182. void setDampingOrthoLin(real_t dampingOrthoLin) { m_dampingOrthoLin = dampingOrthoLin; }
  183. void setSoftnessOrthoAng(real_t softnessOrthoAng) { m_softnessOrthoAng = softnessOrthoAng; }
  184. void setRestitutionOrthoAng(real_t restitutionOrthoAng) { m_restitutionOrthoAng = restitutionOrthoAng; }
  185. void setDampingOrthoAng(real_t dampingOrthoAng) { m_dampingOrthoAng = dampingOrthoAng; }
  186. void setPoweredLinMotor(bool onOff) { m_poweredLinMotor = onOff; }
  187. bool getPoweredLinMotor() { return m_poweredLinMotor; }
  188. void setTargetLinMotorVelocity(real_t targetLinMotorVelocity) { m_targetLinMotorVelocity = targetLinMotorVelocity; }
  189. real_t getTargetLinMotorVelocity() { return m_targetLinMotorVelocity; }
  190. void setMaxLinMotorForce(real_t maxLinMotorForce) { m_maxLinMotorForce = maxLinMotorForce; }
  191. real_t getMaxLinMotorForce() { return m_maxLinMotorForce; }
  192. void setPoweredAngMotor(bool onOff) { m_poweredAngMotor = onOff; }
  193. bool getPoweredAngMotor() { return m_poweredAngMotor; }
  194. void setTargetAngMotorVelocity(real_t targetAngMotorVelocity) { m_targetAngMotorVelocity = targetAngMotorVelocity; }
  195. real_t getTargetAngMotorVelocity() { return m_targetAngMotorVelocity; }
  196. void setMaxAngMotorForce(real_t maxAngMotorForce) { m_maxAngMotorForce = maxAngMotorForce; }
  197. real_t getMaxAngMotorForce() { return m_maxAngMotorForce; }
  198. real_t getLinearPos() { return m_linPos; }
  199. // access for ODE solver
  200. bool getSolveLinLimit() { return m_solveLinLim; }
  201. real_t getLinDepth() { return m_depth[0]; }
  202. bool getSolveAngLimit() { return m_solveAngLim; }
  203. real_t getAngDepth() { return m_angDepth; }
  204. // shared code used by ODE solver
  205. void calculateTransforms(void);
  206. void testLinLimits(void);
  207. void testAngLimits(void);
  208. // access for PE Solver
  209. Vector3 getAncorInA(void);
  210. Vector3 getAncorInB(void);
  211. void set_param(PhysicsServer::SliderJointParam p_param, real_t p_value);
  212. real_t get_param(PhysicsServer::SliderJointParam p_param) const;
  213. bool setup(real_t p_step);
  214. void solve(real_t p_step);
  215. virtual PhysicsServer::JointType get_type() const { return PhysicsServer::JOINT_SLIDER; }
  216. };
  217. #endif // SLIDER_JOINT_SW_H