generic_6dof_joint_bullet.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**************************************************************************/
  2. /* generic_6dof_joint_bullet.cpp */
  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. #include "generic_6dof_joint_bullet.h"
  31. #include "bullet_types_converter.h"
  32. #include "bullet_utilities.h"
  33. #include "rigid_body_bullet.h"
  34. #include <BulletDynamics/ConstraintSolver/btGeneric6DofSpring2Constraint.h>
  35. /**
  36. @author AndreaCatania
  37. */
  38. Generic6DOFJointBullet::Generic6DOFJointBullet(RigidBodyBullet *rbA, RigidBodyBullet *rbB, const Transform &frameInA, const Transform &frameInB) :
  39. JointBullet() {
  40. Transform scaled_AFrame(frameInA.scaled(rbA->get_body_scale()));
  41. _ALLOW_DISCARD_ scaled_AFrame.basis.rotref_posscale_decomposition(scaled_AFrame.basis);
  42. btTransform btFrameA;
  43. G_TO_B(scaled_AFrame, btFrameA);
  44. if (rbB) {
  45. Transform scaled_BFrame(frameInB.scaled(rbB->get_body_scale()));
  46. _ALLOW_DISCARD_ scaled_BFrame.basis.rotref_posscale_decomposition(scaled_BFrame.basis);
  47. btTransform btFrameB;
  48. G_TO_B(scaled_BFrame, btFrameB);
  49. sixDOFConstraint = bulletnew(btGeneric6DofSpring2Constraint(*rbA->get_bt_rigid_body(), *rbB->get_bt_rigid_body(), btFrameA, btFrameB));
  50. } else {
  51. sixDOFConstraint = bulletnew(btGeneric6DofSpring2Constraint(*rbA->get_bt_rigid_body(), btFrameA));
  52. }
  53. setup(sixDOFConstraint);
  54. }
  55. Transform Generic6DOFJointBullet::getFrameOffsetA() const {
  56. btTransform btTrs = sixDOFConstraint->getFrameOffsetA();
  57. Transform gTrs;
  58. B_TO_G(btTrs, gTrs);
  59. return gTrs;
  60. }
  61. Transform Generic6DOFJointBullet::getFrameOffsetB() const {
  62. btTransform btTrs = sixDOFConstraint->getFrameOffsetB();
  63. Transform gTrs;
  64. B_TO_G(btTrs, gTrs);
  65. return gTrs;
  66. }
  67. Transform Generic6DOFJointBullet::getFrameOffsetA() {
  68. btTransform btTrs = sixDOFConstraint->getFrameOffsetA();
  69. Transform gTrs;
  70. B_TO_G(btTrs, gTrs);
  71. return gTrs;
  72. }
  73. Transform Generic6DOFJointBullet::getFrameOffsetB() {
  74. btTransform btTrs = sixDOFConstraint->getFrameOffsetB();
  75. Transform gTrs;
  76. B_TO_G(btTrs, gTrs);
  77. return gTrs;
  78. }
  79. void Generic6DOFJointBullet::set_linear_lower_limit(const Vector3 &linearLower) {
  80. btVector3 btVec;
  81. G_TO_B(linearLower, btVec);
  82. sixDOFConstraint->setLinearLowerLimit(btVec);
  83. }
  84. void Generic6DOFJointBullet::set_linear_upper_limit(const Vector3 &linearUpper) {
  85. btVector3 btVec;
  86. G_TO_B(linearUpper, btVec);
  87. sixDOFConstraint->setLinearUpperLimit(btVec);
  88. }
  89. void Generic6DOFJointBullet::set_angular_lower_limit(const Vector3 &angularLower) {
  90. btVector3 btVec;
  91. G_TO_B(angularLower, btVec);
  92. sixDOFConstraint->setAngularLowerLimit(btVec);
  93. }
  94. void Generic6DOFJointBullet::set_angular_upper_limit(const Vector3 &angularUpper) {
  95. btVector3 btVec;
  96. G_TO_B(angularUpper, btVec);
  97. sixDOFConstraint->setAngularUpperLimit(btVec);
  98. }
  99. void Generic6DOFJointBullet::set_param(Vector3::Axis p_axis, PhysicsServer::G6DOFJointAxisParam p_param, real_t p_value) {
  100. ERR_FAIL_INDEX(p_axis, 3);
  101. switch (p_param) {
  102. case PhysicsServer::G6DOF_JOINT_LINEAR_LOWER_LIMIT:
  103. limits_lower[0][p_axis] = p_value;
  104. set_flag(p_axis, PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT, flags[p_axis][PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT]); // Reload bullet parameter
  105. break;
  106. case PhysicsServer::G6DOF_JOINT_LINEAR_UPPER_LIMIT:
  107. limits_upper[0][p_axis] = p_value;
  108. set_flag(p_axis, PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT, flags[p_axis][PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT]); // Reload bullet parameter
  109. break;
  110. case PhysicsServer::G6DOF_JOINT_LINEAR_MOTOR_TARGET_VELOCITY:
  111. sixDOFConstraint->getTranslationalLimitMotor()->m_targetVelocity.m_floats[p_axis] = p_value;
  112. break;
  113. case PhysicsServer::G6DOF_JOINT_LINEAR_MOTOR_FORCE_LIMIT:
  114. sixDOFConstraint->getTranslationalLimitMotor()->m_maxMotorForce.m_floats[p_axis] = p_value;
  115. break;
  116. case PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_DAMPING:
  117. sixDOFConstraint->getTranslationalLimitMotor()->m_springDamping.m_floats[p_axis] = p_value;
  118. break;
  119. case PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_STIFFNESS:
  120. sixDOFConstraint->getTranslationalLimitMotor()->m_springStiffness.m_floats[p_axis] = p_value;
  121. break;
  122. case PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_EQUILIBRIUM_POINT:
  123. sixDOFConstraint->getTranslationalLimitMotor()->m_equilibriumPoint.m_floats[p_axis] = p_value;
  124. break;
  125. case PhysicsServer::G6DOF_JOINT_ANGULAR_LOWER_LIMIT:
  126. limits_lower[1][p_axis] = p_value;
  127. set_flag(p_axis, PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT, flags[p_axis][PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT]); // Reload bullet parameter
  128. break;
  129. case PhysicsServer::G6DOF_JOINT_ANGULAR_UPPER_LIMIT:
  130. limits_upper[1][p_axis] = p_value;
  131. set_flag(p_axis, PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT, flags[p_axis][PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT]); // Reload bullet parameter
  132. break;
  133. case PhysicsServer::G6DOF_JOINT_ANGULAR_RESTITUTION:
  134. sixDOFConstraint->getRotationalLimitMotor(p_axis)->m_bounce = p_value;
  135. break;
  136. case PhysicsServer::G6DOF_JOINT_ANGULAR_ERP:
  137. sixDOFConstraint->getRotationalLimitMotor(p_axis)->m_stopERP = p_value;
  138. break;
  139. case PhysicsServer::G6DOF_JOINT_ANGULAR_MOTOR_TARGET_VELOCITY:
  140. sixDOFConstraint->getRotationalLimitMotor(p_axis)->m_targetVelocity = p_value;
  141. break;
  142. case PhysicsServer::G6DOF_JOINT_ANGULAR_MOTOR_FORCE_LIMIT:
  143. sixDOFConstraint->getRotationalLimitMotor(p_axis)->m_maxMotorForce = p_value;
  144. break;
  145. case PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_STIFFNESS:
  146. sixDOFConstraint->getRotationalLimitMotor(p_axis)->m_springStiffness = p_value;
  147. break;
  148. case PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_DAMPING:
  149. sixDOFConstraint->getRotationalLimitMotor(p_axis)->m_springDamping = p_value;
  150. break;
  151. case PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_EQUILIBRIUM_POINT:
  152. sixDOFConstraint->getRotationalLimitMotor(p_axis)->m_equilibriumPoint = p_value;
  153. break;
  154. default:
  155. WARN_DEPRECATED_MSG("The parameter " + itos(p_param) + " is deprecated.");
  156. break;
  157. }
  158. }
  159. real_t Generic6DOFJointBullet::get_param(Vector3::Axis p_axis, PhysicsServer::G6DOFJointAxisParam p_param) const {
  160. ERR_FAIL_INDEX_V(p_axis, 3, 0.);
  161. switch (p_param) {
  162. case PhysicsServer::G6DOF_JOINT_LINEAR_LOWER_LIMIT:
  163. return limits_lower[0][p_axis];
  164. case PhysicsServer::G6DOF_JOINT_LINEAR_UPPER_LIMIT:
  165. return limits_upper[0][p_axis];
  166. case PhysicsServer::G6DOF_JOINT_LINEAR_MOTOR_TARGET_VELOCITY:
  167. return sixDOFConstraint->getTranslationalLimitMotor()->m_targetVelocity.m_floats[p_axis];
  168. case PhysicsServer::G6DOF_JOINT_LINEAR_MOTOR_FORCE_LIMIT:
  169. return sixDOFConstraint->getTranslationalLimitMotor()->m_maxMotorForce.m_floats[p_axis];
  170. case PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_DAMPING:
  171. return sixDOFConstraint->getTranslationalLimitMotor()->m_springDamping.m_floats[p_axis];
  172. case PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_STIFFNESS:
  173. return sixDOFConstraint->getTranslationalLimitMotor()->m_springStiffness.m_floats[p_axis];
  174. case PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_EQUILIBRIUM_POINT:
  175. return sixDOFConstraint->getTranslationalLimitMotor()->m_equilibriumPoint.m_floats[p_axis];
  176. case PhysicsServer::G6DOF_JOINT_ANGULAR_LOWER_LIMIT:
  177. return limits_lower[1][p_axis];
  178. case PhysicsServer::G6DOF_JOINT_ANGULAR_UPPER_LIMIT:
  179. return limits_upper[1][p_axis];
  180. case PhysicsServer::G6DOF_JOINT_ANGULAR_RESTITUTION:
  181. return sixDOFConstraint->getRotationalLimitMotor(p_axis)->m_bounce;
  182. case PhysicsServer::G6DOF_JOINT_ANGULAR_ERP:
  183. return sixDOFConstraint->getRotationalLimitMotor(p_axis)->m_stopERP;
  184. case PhysicsServer::G6DOF_JOINT_ANGULAR_MOTOR_TARGET_VELOCITY:
  185. return sixDOFConstraint->getRotationalLimitMotor(p_axis)->m_targetVelocity;
  186. case PhysicsServer::G6DOF_JOINT_ANGULAR_MOTOR_FORCE_LIMIT:
  187. return sixDOFConstraint->getRotationalLimitMotor(p_axis)->m_maxMotorForce;
  188. case PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_STIFFNESS:
  189. return sixDOFConstraint->getRotationalLimitMotor(p_axis)->m_springStiffness;
  190. case PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_DAMPING:
  191. return sixDOFConstraint->getRotationalLimitMotor(p_axis)->m_springDamping;
  192. case PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_EQUILIBRIUM_POINT:
  193. return sixDOFConstraint->getRotationalLimitMotor(p_axis)->m_equilibriumPoint;
  194. default:
  195. WARN_DEPRECATED_MSG("The parameter " + itos(p_param) + " is deprecated.");
  196. return 0;
  197. }
  198. }
  199. void Generic6DOFJointBullet::set_flag(Vector3::Axis p_axis, PhysicsServer::G6DOFJointAxisFlag p_flag, bool p_value) {
  200. ERR_FAIL_INDEX(p_axis, 3);
  201. flags[p_axis][p_flag] = p_value;
  202. switch (p_flag) {
  203. case PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT:
  204. if (flags[p_axis][p_flag]) {
  205. sixDOFConstraint->setLimit(p_axis, limits_lower[0][p_axis], limits_upper[0][p_axis]);
  206. } else {
  207. sixDOFConstraint->setLimit(p_axis, 0, -1); // Free
  208. }
  209. break;
  210. case PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT:
  211. if (flags[p_axis][p_flag]) {
  212. sixDOFConstraint->setLimit(p_axis + 3, limits_lower[1][p_axis], limits_upper[1][p_axis]);
  213. } else {
  214. sixDOFConstraint->setLimit(p_axis + 3, 0, -1); // Free
  215. }
  216. break;
  217. case PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_MOTOR:
  218. sixDOFConstraint->getRotationalLimitMotor(p_axis)->m_enableMotor = flags[p_axis][p_flag];
  219. break;
  220. case PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_MOTOR:
  221. sixDOFConstraint->getTranslationalLimitMotor()->m_enableMotor[p_axis] = flags[p_axis][p_flag];
  222. break;
  223. case PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_SPRING:
  224. sixDOFConstraint->getTranslationalLimitMotor()->m_enableSpring[p_axis] = p_value;
  225. break;
  226. case PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_SPRING:
  227. sixDOFConstraint->getRotationalLimitMotor(p_axis)->m_enableSpring = p_value;
  228. break;
  229. default:
  230. WARN_DEPRECATED_MSG("The flag " + itos(p_flag) + " is deprecated.");
  231. break;
  232. }
  233. }
  234. bool Generic6DOFJointBullet::get_flag(Vector3::Axis p_axis, PhysicsServer::G6DOFJointAxisFlag p_flag) const {
  235. ERR_FAIL_INDEX_V(p_axis, 3, false);
  236. return flags[p_axis][p_flag];
  237. }