hinge_joint_bullet.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**************************************************************************/
  2. /* hinge_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 "hinge_joint_bullet.h"
  31. #include "bullet_types_converter.h"
  32. #include "bullet_utilities.h"
  33. #include "rigid_body_bullet.h"
  34. #include <BulletDynamics/ConstraintSolver/btHingeConstraint.h>
  35. /**
  36. @author AndreaCatania
  37. */
  38. HingeJointBullet::HingeJointBullet(RigidBodyBullet *rbA, RigidBodyBullet *rbB, const Transform &frameA, const Transform &frameB) :
  39. JointBullet() {
  40. Transform scaled_AFrame(frameA.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(frameB.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. hingeConstraint = bulletnew(btHingeConstraint(*rbA->get_bt_rigid_body(), *rbB->get_bt_rigid_body(), btFrameA, btFrameB));
  50. } else {
  51. hingeConstraint = bulletnew(btHingeConstraint(*rbA->get_bt_rigid_body(), btFrameA));
  52. }
  53. setup(hingeConstraint);
  54. }
  55. HingeJointBullet::HingeJointBullet(RigidBodyBullet *rbA, RigidBodyBullet *rbB, const Vector3 &pivotInA, const Vector3 &pivotInB, const Vector3 &axisInA, const Vector3 &axisInB) :
  56. JointBullet() {
  57. btVector3 btPivotA;
  58. btVector3 btAxisA;
  59. G_TO_B(pivotInA * rbA->get_body_scale(), btPivotA);
  60. G_TO_B(axisInA * rbA->get_body_scale(), btAxisA);
  61. if (rbB) {
  62. btVector3 btPivotB;
  63. btVector3 btAxisB;
  64. G_TO_B(pivotInB * rbB->get_body_scale(), btPivotB);
  65. G_TO_B(axisInB * rbB->get_body_scale(), btAxisB);
  66. hingeConstraint = bulletnew(btHingeConstraint(*rbA->get_bt_rigid_body(), *rbB->get_bt_rigid_body(), btPivotA, btPivotB, btAxisA, btAxisB));
  67. } else {
  68. hingeConstraint = bulletnew(btHingeConstraint(*rbA->get_bt_rigid_body(), btPivotA, btAxisA));
  69. }
  70. setup(hingeConstraint);
  71. }
  72. real_t HingeJointBullet::get_hinge_angle() {
  73. return hingeConstraint->getHingeAngle();
  74. }
  75. void HingeJointBullet::set_param(PhysicsServer::HingeJointParam p_param, real_t p_value) {
  76. switch (p_param) {
  77. case PhysicsServer::HINGE_JOINT_LIMIT_UPPER:
  78. hingeConstraint->setLimit(hingeConstraint->getLowerLimit(), p_value, hingeConstraint->getLimitSoftness(), hingeConstraint->getLimitBiasFactor(), hingeConstraint->getLimitRelaxationFactor());
  79. break;
  80. case PhysicsServer::HINGE_JOINT_LIMIT_LOWER:
  81. hingeConstraint->setLimit(p_value, hingeConstraint->getUpperLimit(), hingeConstraint->getLimitSoftness(), hingeConstraint->getLimitBiasFactor(), hingeConstraint->getLimitRelaxationFactor());
  82. break;
  83. case PhysicsServer::HINGE_JOINT_LIMIT_BIAS:
  84. hingeConstraint->setLimit(hingeConstraint->getLowerLimit(), hingeConstraint->getUpperLimit(), hingeConstraint->getLimitSoftness(), p_value, hingeConstraint->getLimitRelaxationFactor());
  85. break;
  86. case PhysicsServer::HINGE_JOINT_LIMIT_SOFTNESS:
  87. hingeConstraint->setLimit(hingeConstraint->getLowerLimit(), hingeConstraint->getUpperLimit(), p_value, hingeConstraint->getLimitBiasFactor(), hingeConstraint->getLimitRelaxationFactor());
  88. break;
  89. case PhysicsServer::HINGE_JOINT_LIMIT_RELAXATION:
  90. hingeConstraint->setLimit(hingeConstraint->getLowerLimit(), hingeConstraint->getUpperLimit(), hingeConstraint->getLimitSoftness(), hingeConstraint->getLimitBiasFactor(), p_value);
  91. break;
  92. case PhysicsServer::HINGE_JOINT_MOTOR_TARGET_VELOCITY:
  93. hingeConstraint->setMotorTargetVelocity(p_value);
  94. break;
  95. case PhysicsServer::HINGE_JOINT_MOTOR_MAX_IMPULSE:
  96. hingeConstraint->setMaxMotorImpulse(p_value);
  97. break;
  98. default:
  99. WARN_DEPRECATED_MSG("The HingeJoint parameter " + itos(p_param) + " is deprecated.");
  100. break;
  101. }
  102. }
  103. real_t HingeJointBullet::get_param(PhysicsServer::HingeJointParam p_param) const {
  104. switch (p_param) {
  105. case PhysicsServer::HINGE_JOINT_BIAS:
  106. return 0;
  107. break;
  108. case PhysicsServer::HINGE_JOINT_LIMIT_UPPER:
  109. return hingeConstraint->getUpperLimit();
  110. case PhysicsServer::HINGE_JOINT_LIMIT_LOWER:
  111. return hingeConstraint->getLowerLimit();
  112. case PhysicsServer::HINGE_JOINT_LIMIT_BIAS:
  113. return hingeConstraint->getLimitBiasFactor();
  114. case PhysicsServer::HINGE_JOINT_LIMIT_SOFTNESS:
  115. return hingeConstraint->getLimitSoftness();
  116. case PhysicsServer::HINGE_JOINT_LIMIT_RELAXATION:
  117. return hingeConstraint->getLimitRelaxationFactor();
  118. case PhysicsServer::HINGE_JOINT_MOTOR_TARGET_VELOCITY:
  119. return hingeConstraint->getMotorTargetVelocity();
  120. case PhysicsServer::HINGE_JOINT_MOTOR_MAX_IMPULSE:
  121. return hingeConstraint->getMaxMotorImpulse();
  122. default:
  123. WARN_DEPRECATED_MSG("The HingeJoint parameter " + itos(p_param) + " is deprecated.");
  124. return 0;
  125. }
  126. }
  127. void HingeJointBullet::set_flag(PhysicsServer::HingeJointFlag p_flag, bool p_value) {
  128. switch (p_flag) {
  129. case PhysicsServer::HINGE_JOINT_FLAG_USE_LIMIT:
  130. if (!p_value) {
  131. hingeConstraint->setLimit(-Math_PI, Math_PI);
  132. }
  133. break;
  134. case PhysicsServer::HINGE_JOINT_FLAG_ENABLE_MOTOR:
  135. hingeConstraint->enableMotor(p_value);
  136. break;
  137. case PhysicsServer::HINGE_JOINT_FLAG_MAX:
  138. break; // Can't happen, but silences warning
  139. }
  140. }
  141. bool HingeJointBullet::get_flag(PhysicsServer::HingeJointFlag p_flag) const {
  142. switch (p_flag) {
  143. case PhysicsServer::HINGE_JOINT_FLAG_USE_LIMIT:
  144. return true;
  145. case PhysicsServer::HINGE_JOINT_FLAG_ENABLE_MOTOR:
  146. return hingeConstraint->getEnableAngularMotor();
  147. default:
  148. return false;
  149. }
  150. }