hinge_joint_bullet.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*************************************************************************/
  2. /* hinge_joint_bullet.cpp */
  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. #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. 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. 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_BIAS:
  78. if (0 < p_value) {
  79. print_line("The Bullet Hinge Joint doesn't support bias, So it's always 0");
  80. }
  81. break;
  82. case PhysicsServer::HINGE_JOINT_LIMIT_UPPER:
  83. hingeConstraint->setLimit(hingeConstraint->getLowerLimit(), p_value, hingeConstraint->getLimitSoftness(), hingeConstraint->getLimitBiasFactor(), hingeConstraint->getLimitRelaxationFactor());
  84. break;
  85. case PhysicsServer::HINGE_JOINT_LIMIT_LOWER:
  86. hingeConstraint->setLimit(p_value, hingeConstraint->getUpperLimit(), hingeConstraint->getLimitSoftness(), hingeConstraint->getLimitBiasFactor(), hingeConstraint->getLimitRelaxationFactor());
  87. break;
  88. case PhysicsServer::HINGE_JOINT_LIMIT_BIAS:
  89. hingeConstraint->setLimit(hingeConstraint->getLowerLimit(), hingeConstraint->getUpperLimit(), hingeConstraint->getLimitSoftness(), p_value, hingeConstraint->getLimitRelaxationFactor());
  90. break;
  91. case PhysicsServer::HINGE_JOINT_LIMIT_SOFTNESS:
  92. hingeConstraint->setLimit(hingeConstraint->getLowerLimit(), hingeConstraint->getUpperLimit(), p_value, hingeConstraint->getLimitBiasFactor(), hingeConstraint->getLimitRelaxationFactor());
  93. break;
  94. case PhysicsServer::HINGE_JOINT_LIMIT_RELAXATION:
  95. hingeConstraint->setLimit(hingeConstraint->getLowerLimit(), hingeConstraint->getUpperLimit(), hingeConstraint->getLimitSoftness(), hingeConstraint->getLimitBiasFactor(), p_value);
  96. break;
  97. case PhysicsServer::HINGE_JOINT_MOTOR_TARGET_VELOCITY:
  98. hingeConstraint->setMotorTargetVelocity(p_value);
  99. break;
  100. case PhysicsServer::HINGE_JOINT_MOTOR_MAX_IMPULSE:
  101. hingeConstraint->setMaxMotorImpulse(p_value);
  102. break;
  103. default:
  104. WARN_PRINTS("The Bullet Hinge Joint doesn't support this parameter: " + itos(p_param) + ", value: " + itos(p_value));
  105. }
  106. }
  107. real_t HingeJointBullet::get_param(PhysicsServer::HingeJointParam p_param) const {
  108. switch (p_param) {
  109. case PhysicsServer::HINGE_JOINT_BIAS:
  110. return 0;
  111. break;
  112. case PhysicsServer::HINGE_JOINT_LIMIT_UPPER:
  113. return hingeConstraint->getUpperLimit();
  114. case PhysicsServer::HINGE_JOINT_LIMIT_LOWER:
  115. return hingeConstraint->getLowerLimit();
  116. case PhysicsServer::HINGE_JOINT_LIMIT_BIAS:
  117. return hingeConstraint->getLimitBiasFactor();
  118. case PhysicsServer::HINGE_JOINT_LIMIT_SOFTNESS:
  119. return hingeConstraint->getLimitSoftness();
  120. case PhysicsServer::HINGE_JOINT_LIMIT_RELAXATION:
  121. return hingeConstraint->getLimitRelaxationFactor();
  122. case PhysicsServer::HINGE_JOINT_MOTOR_TARGET_VELOCITY:
  123. return hingeConstraint->getMotorTargetVelocity();
  124. case PhysicsServer::HINGE_JOINT_MOTOR_MAX_IMPULSE:
  125. return hingeConstraint->getMaxMotorImpulse();
  126. default:
  127. WARN_PRINTS("The Bullet Hinge Joint doesn't support this parameter: " + itos(p_param));
  128. return 0;
  129. }
  130. }
  131. void HingeJointBullet::set_flag(PhysicsServer::HingeJointFlag p_flag, bool p_value) {
  132. switch (p_flag) {
  133. case PhysicsServer::HINGE_JOINT_FLAG_USE_LIMIT:
  134. if (!p_value) {
  135. hingeConstraint->setLimit(-Math_PI, Math_PI);
  136. }
  137. break;
  138. case PhysicsServer::HINGE_JOINT_FLAG_ENABLE_MOTOR:
  139. hingeConstraint->enableMotor(p_value);
  140. break;
  141. }
  142. }
  143. bool HingeJointBullet::get_flag(PhysicsServer::HingeJointFlag p_flag) const {
  144. switch (p_flag) {
  145. case PhysicsServer::HINGE_JOINT_FLAG_USE_LIMIT:
  146. return true;
  147. case PhysicsServer::HINGE_JOINT_FLAG_ENABLE_MOTOR:
  148. return hingeConstraint->getEnableAngularMotor();
  149. default:
  150. return false;
  151. }
  152. }