cone_twist_joint_bullet.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*************************************************************************/
  2. /* cone_twist_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 "cone_twist_joint_bullet.h"
  31. #include "bullet_types_converter.h"
  32. #include "bullet_utilities.h"
  33. #include "rigid_body_bullet.h"
  34. #include <BulletDynamics/ConstraintSolver/btConeTwistConstraint.h>
  35. /**
  36. @author AndreaCatania
  37. */
  38. ConeTwistJointBullet::ConeTwistJointBullet(RigidBodyBullet *rbA, RigidBodyBullet *rbB, const Transform &rbAFrame, const Transform &rbBFrame) :
  39. JointBullet() {
  40. Transform scaled_AFrame(rbAFrame.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(rbBFrame.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. coneConstraint = bulletnew(btConeTwistConstraint(*rbA->get_bt_rigid_body(), *rbB->get_bt_rigid_body(), btFrameA, btFrameB));
  50. } else {
  51. coneConstraint = bulletnew(btConeTwistConstraint(*rbA->get_bt_rigid_body(), btFrameA));
  52. }
  53. setup(coneConstraint);
  54. }
  55. void ConeTwistJointBullet::set_angular_only(bool angularOnly) {
  56. coneConstraint->setAngularOnly(angularOnly);
  57. }
  58. void ConeTwistJointBullet::set_limit(real_t _swingSpan1, real_t _swingSpan2, real_t _twistSpan, real_t _softness, real_t _biasFactor, real_t _relaxationFactor) {
  59. coneConstraint->setLimit(_swingSpan1, _swingSpan2, _twistSpan, _softness, _biasFactor, _relaxationFactor);
  60. }
  61. int ConeTwistJointBullet::get_solve_twist_limit() {
  62. return coneConstraint->getSolveTwistLimit();
  63. }
  64. int ConeTwistJointBullet::get_solve_swing_limit() {
  65. return coneConstraint->getSolveSwingLimit();
  66. }
  67. real_t ConeTwistJointBullet::get_twist_limit_sign() {
  68. return coneConstraint->getTwistLimitSign();
  69. }
  70. void ConeTwistJointBullet::set_param(PhysicsServer::ConeTwistJointParam p_param, real_t p_value) {
  71. switch (p_param) {
  72. case PhysicsServer::CONE_TWIST_JOINT_SWING_SPAN:
  73. coneConstraint->setLimit(5, p_value);
  74. coneConstraint->setLimit(4, p_value);
  75. break;
  76. case PhysicsServer::CONE_TWIST_JOINT_TWIST_SPAN:
  77. coneConstraint->setLimit(3, p_value);
  78. break;
  79. case PhysicsServer::CONE_TWIST_JOINT_BIAS:
  80. coneConstraint->setLimit(coneConstraint->getSwingSpan1(), coneConstraint->getSwingSpan2(), coneConstraint->getTwistSpan(), coneConstraint->getLimitSoftness(), p_value, coneConstraint->getRelaxationFactor());
  81. break;
  82. case PhysicsServer::CONE_TWIST_JOINT_SOFTNESS:
  83. coneConstraint->setLimit(coneConstraint->getSwingSpan1(), coneConstraint->getSwingSpan2(), coneConstraint->getTwistSpan(), p_value, coneConstraint->getBiasFactor(), coneConstraint->getRelaxationFactor());
  84. break;
  85. case PhysicsServer::CONE_TWIST_JOINT_RELAXATION:
  86. coneConstraint->setLimit(coneConstraint->getSwingSpan1(), coneConstraint->getSwingSpan2(), coneConstraint->getTwistSpan(), coneConstraint->getLimitSoftness(), coneConstraint->getBiasFactor(), p_value);
  87. break;
  88. default:
  89. WARN_PRINT("This parameter is not supported by Bullet engine");
  90. }
  91. }
  92. real_t ConeTwistJointBullet::get_param(PhysicsServer::ConeTwistJointParam p_param) const {
  93. switch (p_param) {
  94. case PhysicsServer::CONE_TWIST_JOINT_SWING_SPAN:
  95. return coneConstraint->getSwingSpan1();
  96. case PhysicsServer::CONE_TWIST_JOINT_TWIST_SPAN:
  97. return coneConstraint->getTwistSpan();
  98. case PhysicsServer::CONE_TWIST_JOINT_BIAS:
  99. return coneConstraint->getBiasFactor();
  100. case PhysicsServer::CONE_TWIST_JOINT_SOFTNESS:
  101. return coneConstraint->getLimitSoftness();
  102. case PhysicsServer::CONE_TWIST_JOINT_RELAXATION:
  103. return coneConstraint->getRelaxationFactor();
  104. default:
  105. WARN_PRINT("This parameter is not supported by Bullet engine");
  106. return 0;
  107. }
  108. }