slider_joint_bullet.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /**************************************************************************/
  2. /* slider_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 "slider_joint_bullet.h"
  31. #include "bullet_types_converter.h"
  32. #include "bullet_utilities.h"
  33. #include "rigid_body_bullet.h"
  34. #include <BulletDynamics/ConstraintSolver/btSliderConstraint.h>
  35. /**
  36. @author AndreaCatania
  37. */
  38. SliderJointBullet::SliderJointBullet(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. sliderConstraint = bulletnew(btSliderConstraint(*rbA->get_bt_rigid_body(), *rbB->get_bt_rigid_body(), btFrameA, btFrameB, true));
  50. } else {
  51. sliderConstraint = bulletnew(btSliderConstraint(*rbA->get_bt_rigid_body(), btFrameA, true));
  52. }
  53. setup(sliderConstraint);
  54. }
  55. const RigidBodyBullet *SliderJointBullet::getRigidBodyA() const {
  56. return static_cast<RigidBodyBullet *>(sliderConstraint->getRigidBodyA().getUserPointer());
  57. }
  58. const RigidBodyBullet *SliderJointBullet::getRigidBodyB() const {
  59. return static_cast<RigidBodyBullet *>(sliderConstraint->getRigidBodyB().getUserPointer());
  60. }
  61. const Transform SliderJointBullet::getCalculatedTransformA() const {
  62. btTransform btTransform = sliderConstraint->getCalculatedTransformA();
  63. Transform gTrans;
  64. B_TO_G(btTransform, gTrans);
  65. return gTrans;
  66. }
  67. const Transform SliderJointBullet::getCalculatedTransformB() const {
  68. btTransform btTransform = sliderConstraint->getCalculatedTransformB();
  69. Transform gTrans;
  70. B_TO_G(btTransform, gTrans);
  71. return gTrans;
  72. }
  73. const Transform SliderJointBullet::getFrameOffsetA() const {
  74. btTransform btTransform = sliderConstraint->getFrameOffsetA();
  75. Transform gTrans;
  76. B_TO_G(btTransform, gTrans);
  77. return gTrans;
  78. }
  79. const Transform SliderJointBullet::getFrameOffsetB() const {
  80. btTransform btTransform = sliderConstraint->getFrameOffsetB();
  81. Transform gTrans;
  82. B_TO_G(btTransform, gTrans);
  83. return gTrans;
  84. }
  85. Transform SliderJointBullet::getFrameOffsetA() {
  86. btTransform btTransform = sliderConstraint->getFrameOffsetA();
  87. Transform gTrans;
  88. B_TO_G(btTransform, gTrans);
  89. return gTrans;
  90. }
  91. Transform SliderJointBullet::getFrameOffsetB() {
  92. btTransform btTransform = sliderConstraint->getFrameOffsetB();
  93. Transform gTrans;
  94. B_TO_G(btTransform, gTrans);
  95. return gTrans;
  96. }
  97. real_t SliderJointBullet::getLowerLinLimit() const {
  98. return sliderConstraint->getLowerLinLimit();
  99. }
  100. void SliderJointBullet::setLowerLinLimit(real_t lowerLimit) {
  101. sliderConstraint->setLowerLinLimit(lowerLimit);
  102. }
  103. real_t SliderJointBullet::getUpperLinLimit() const {
  104. return sliderConstraint->getUpperLinLimit();
  105. }
  106. void SliderJointBullet::setUpperLinLimit(real_t upperLimit) {
  107. sliderConstraint->setUpperLinLimit(upperLimit);
  108. }
  109. real_t SliderJointBullet::getLowerAngLimit() const {
  110. return sliderConstraint->getLowerAngLimit();
  111. }
  112. void SliderJointBullet::setLowerAngLimit(real_t lowerLimit) {
  113. sliderConstraint->setLowerAngLimit(lowerLimit);
  114. }
  115. real_t SliderJointBullet::getUpperAngLimit() const {
  116. return sliderConstraint->getUpperAngLimit();
  117. }
  118. void SliderJointBullet::setUpperAngLimit(real_t upperLimit) {
  119. sliderConstraint->setUpperAngLimit(upperLimit);
  120. }
  121. real_t SliderJointBullet::getSoftnessDirLin() const {
  122. return sliderConstraint->getSoftnessDirLin();
  123. }
  124. real_t SliderJointBullet::getRestitutionDirLin() const {
  125. return sliderConstraint->getRestitutionDirLin();
  126. }
  127. real_t SliderJointBullet::getDampingDirLin() const {
  128. return sliderConstraint->getDampingDirLin();
  129. }
  130. real_t SliderJointBullet::getSoftnessDirAng() const {
  131. return sliderConstraint->getSoftnessDirAng();
  132. }
  133. real_t SliderJointBullet::getRestitutionDirAng() const {
  134. return sliderConstraint->getRestitutionDirAng();
  135. }
  136. real_t SliderJointBullet::getDampingDirAng() const {
  137. return sliderConstraint->getDampingDirAng();
  138. }
  139. real_t SliderJointBullet::getSoftnessLimLin() const {
  140. return sliderConstraint->getSoftnessLimLin();
  141. }
  142. real_t SliderJointBullet::getRestitutionLimLin() const {
  143. return sliderConstraint->getRestitutionLimLin();
  144. }
  145. real_t SliderJointBullet::getDampingLimLin() const {
  146. return sliderConstraint->getDampingLimLin();
  147. }
  148. real_t SliderJointBullet::getSoftnessLimAng() const {
  149. return sliderConstraint->getSoftnessLimAng();
  150. }
  151. real_t SliderJointBullet::getRestitutionLimAng() const {
  152. return sliderConstraint->getRestitutionLimAng();
  153. }
  154. real_t SliderJointBullet::getDampingLimAng() const {
  155. return sliderConstraint->getDampingLimAng();
  156. }
  157. real_t SliderJointBullet::getSoftnessOrthoLin() const {
  158. return sliderConstraint->getSoftnessOrthoLin();
  159. }
  160. real_t SliderJointBullet::getRestitutionOrthoLin() const {
  161. return sliderConstraint->getRestitutionOrthoLin();
  162. }
  163. real_t SliderJointBullet::getDampingOrthoLin() const {
  164. return sliderConstraint->getDampingOrthoLin();
  165. }
  166. real_t SliderJointBullet::getSoftnessOrthoAng() const {
  167. return sliderConstraint->getSoftnessOrthoAng();
  168. }
  169. real_t SliderJointBullet::getRestitutionOrthoAng() const {
  170. return sliderConstraint->getRestitutionOrthoAng();
  171. }
  172. real_t SliderJointBullet::getDampingOrthoAng() const {
  173. return sliderConstraint->getDampingOrthoAng();
  174. }
  175. void SliderJointBullet::setSoftnessDirLin(real_t softnessDirLin) {
  176. sliderConstraint->setSoftnessDirLin(softnessDirLin);
  177. }
  178. void SliderJointBullet::setRestitutionDirLin(real_t restitutionDirLin) {
  179. sliderConstraint->setRestitutionDirLin(restitutionDirLin);
  180. }
  181. void SliderJointBullet::setDampingDirLin(real_t dampingDirLin) {
  182. sliderConstraint->setDampingDirLin(dampingDirLin);
  183. }
  184. void SliderJointBullet::setSoftnessDirAng(real_t softnessDirAng) {
  185. sliderConstraint->setSoftnessDirAng(softnessDirAng);
  186. }
  187. void SliderJointBullet::setRestitutionDirAng(real_t restitutionDirAng) {
  188. sliderConstraint->setRestitutionDirAng(restitutionDirAng);
  189. }
  190. void SliderJointBullet::setDampingDirAng(real_t dampingDirAng) {
  191. sliderConstraint->setDampingDirAng(dampingDirAng);
  192. }
  193. void SliderJointBullet::setSoftnessLimLin(real_t softnessLimLin) {
  194. sliderConstraint->setSoftnessLimLin(softnessLimLin);
  195. }
  196. void SliderJointBullet::setRestitutionLimLin(real_t restitutionLimLin) {
  197. sliderConstraint->setRestitutionLimLin(restitutionLimLin);
  198. }
  199. void SliderJointBullet::setDampingLimLin(real_t dampingLimLin) {
  200. sliderConstraint->setDampingLimLin(dampingLimLin);
  201. }
  202. void SliderJointBullet::setSoftnessLimAng(real_t softnessLimAng) {
  203. sliderConstraint->setSoftnessLimAng(softnessLimAng);
  204. }
  205. void SliderJointBullet::setRestitutionLimAng(real_t restitutionLimAng) {
  206. sliderConstraint->setRestitutionLimAng(restitutionLimAng);
  207. }
  208. void SliderJointBullet::setDampingLimAng(real_t dampingLimAng) {
  209. sliderConstraint->setDampingLimAng(dampingLimAng);
  210. }
  211. void SliderJointBullet::setSoftnessOrthoLin(real_t softnessOrthoLin) {
  212. sliderConstraint->setSoftnessOrthoLin(softnessOrthoLin);
  213. }
  214. void SliderJointBullet::setRestitutionOrthoLin(real_t restitutionOrthoLin) {
  215. sliderConstraint->setRestitutionOrthoLin(restitutionOrthoLin);
  216. }
  217. void SliderJointBullet::setDampingOrthoLin(real_t dampingOrthoLin) {
  218. sliderConstraint->setDampingOrthoLin(dampingOrthoLin);
  219. }
  220. void SliderJointBullet::setSoftnessOrthoAng(real_t softnessOrthoAng) {
  221. sliderConstraint->setSoftnessOrthoAng(softnessOrthoAng);
  222. }
  223. void SliderJointBullet::setRestitutionOrthoAng(real_t restitutionOrthoAng) {
  224. sliderConstraint->setRestitutionOrthoAng(restitutionOrthoAng);
  225. }
  226. void SliderJointBullet::setDampingOrthoAng(real_t dampingOrthoAng) {
  227. sliderConstraint->setDampingOrthoAng(dampingOrthoAng);
  228. }
  229. void SliderJointBullet::setPoweredLinMotor(bool onOff) {
  230. sliderConstraint->setPoweredLinMotor(onOff);
  231. }
  232. bool SliderJointBullet::getPoweredLinMotor() {
  233. return sliderConstraint->getPoweredLinMotor();
  234. }
  235. void SliderJointBullet::setTargetLinMotorVelocity(real_t targetLinMotorVelocity) {
  236. sliderConstraint->setTargetLinMotorVelocity(targetLinMotorVelocity);
  237. }
  238. real_t SliderJointBullet::getTargetLinMotorVelocity() {
  239. return sliderConstraint->getTargetLinMotorVelocity();
  240. }
  241. void SliderJointBullet::setMaxLinMotorForce(real_t maxLinMotorForce) {
  242. sliderConstraint->setMaxLinMotorForce(maxLinMotorForce);
  243. }
  244. real_t SliderJointBullet::getMaxLinMotorForce() {
  245. return sliderConstraint->getMaxLinMotorForce();
  246. }
  247. void SliderJointBullet::setPoweredAngMotor(bool onOff) {
  248. sliderConstraint->setPoweredAngMotor(onOff);
  249. }
  250. bool SliderJointBullet::getPoweredAngMotor() {
  251. return sliderConstraint->getPoweredAngMotor();
  252. }
  253. void SliderJointBullet::setTargetAngMotorVelocity(real_t targetAngMotorVelocity) {
  254. sliderConstraint->setTargetAngMotorVelocity(targetAngMotorVelocity);
  255. }
  256. real_t SliderJointBullet::getTargetAngMotorVelocity() {
  257. return sliderConstraint->getTargetAngMotorVelocity();
  258. }
  259. void SliderJointBullet::setMaxAngMotorForce(real_t maxAngMotorForce) {
  260. sliderConstraint->setMaxAngMotorForce(maxAngMotorForce);
  261. }
  262. real_t SliderJointBullet::getMaxAngMotorForce() {
  263. return sliderConstraint->getMaxAngMotorForce();
  264. }
  265. real_t SliderJointBullet::getLinearPos() {
  266. return sliderConstraint->getLinearPos();
  267. ;
  268. }
  269. void SliderJointBullet::set_param(PhysicsServer::SliderJointParam p_param, real_t p_value) {
  270. switch (p_param) {
  271. case PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_UPPER:
  272. setUpperLinLimit(p_value);
  273. break;
  274. case PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_LOWER:
  275. setLowerLinLimit(p_value);
  276. break;
  277. case PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS:
  278. setSoftnessLimLin(p_value);
  279. break;
  280. case PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION:
  281. setRestitutionLimLin(p_value);
  282. break;
  283. case PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_DAMPING:
  284. setDampingLimLin(p_value);
  285. break;
  286. case PhysicsServer::SLIDER_JOINT_LINEAR_MOTION_SOFTNESS:
  287. setSoftnessDirLin(p_value);
  288. break;
  289. case PhysicsServer::SLIDER_JOINT_LINEAR_MOTION_RESTITUTION:
  290. setRestitutionDirLin(p_value);
  291. break;
  292. case PhysicsServer::SLIDER_JOINT_LINEAR_MOTION_DAMPING:
  293. setDampingDirLin(p_value);
  294. break;
  295. case PhysicsServer::SLIDER_JOINT_LINEAR_ORTHOGONAL_SOFTNESS:
  296. setSoftnessOrthoLin(p_value);
  297. break;
  298. case PhysicsServer::SLIDER_JOINT_LINEAR_ORTHOGONAL_RESTITUTION:
  299. setRestitutionOrthoLin(p_value);
  300. break;
  301. case PhysicsServer::SLIDER_JOINT_LINEAR_ORTHOGONAL_DAMPING:
  302. setDampingOrthoLin(p_value);
  303. break;
  304. case PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_UPPER:
  305. setUpperAngLimit(p_value);
  306. break;
  307. case PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_LOWER:
  308. setLowerAngLimit(p_value);
  309. break;
  310. case PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS:
  311. setSoftnessLimAng(p_value);
  312. break;
  313. case PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_RESTITUTION:
  314. setRestitutionLimAng(p_value);
  315. break;
  316. case PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_DAMPING:
  317. setDampingLimAng(p_value);
  318. break;
  319. case PhysicsServer::SLIDER_JOINT_ANGULAR_MOTION_SOFTNESS:
  320. setSoftnessDirAng(p_value);
  321. break;
  322. case PhysicsServer::SLIDER_JOINT_ANGULAR_MOTION_RESTITUTION:
  323. setRestitutionDirAng(p_value);
  324. break;
  325. case PhysicsServer::SLIDER_JOINT_ANGULAR_MOTION_DAMPING:
  326. setDampingDirAng(p_value);
  327. break;
  328. case PhysicsServer::SLIDER_JOINT_ANGULAR_ORTHOGONAL_SOFTNESS:
  329. setSoftnessOrthoAng(p_value);
  330. break;
  331. case PhysicsServer::SLIDER_JOINT_ANGULAR_ORTHOGONAL_RESTITUTION:
  332. setRestitutionOrthoAng(p_value);
  333. break;
  334. case PhysicsServer::SLIDER_JOINT_ANGULAR_ORTHOGONAL_DAMPING:
  335. setDampingOrthoAng(p_value);
  336. break;
  337. case PhysicsServer::SLIDER_JOINT_MAX:
  338. break; // Can't happen, but silences warning
  339. }
  340. }
  341. real_t SliderJointBullet::get_param(PhysicsServer::SliderJointParam p_param) const {
  342. switch (p_param) {
  343. case PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_UPPER:
  344. return getUpperLinLimit();
  345. case PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_LOWER:
  346. return getLowerLinLimit();
  347. case PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS:
  348. return getSoftnessLimLin();
  349. case PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION:
  350. return getRestitutionLimLin();
  351. case PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_DAMPING:
  352. return getDampingLimLin();
  353. case PhysicsServer::SLIDER_JOINT_LINEAR_MOTION_SOFTNESS:
  354. return getSoftnessDirLin();
  355. case PhysicsServer::SLIDER_JOINT_LINEAR_MOTION_RESTITUTION:
  356. return getRestitutionDirLin();
  357. case PhysicsServer::SLIDER_JOINT_LINEAR_MOTION_DAMPING:
  358. return getDampingDirLin();
  359. case PhysicsServer::SLIDER_JOINT_LINEAR_ORTHOGONAL_SOFTNESS:
  360. return getSoftnessOrthoLin();
  361. case PhysicsServer::SLIDER_JOINT_LINEAR_ORTHOGONAL_RESTITUTION:
  362. return getRestitutionOrthoLin();
  363. case PhysicsServer::SLIDER_JOINT_LINEAR_ORTHOGONAL_DAMPING:
  364. return getDampingOrthoLin();
  365. case PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_UPPER:
  366. return getUpperAngLimit();
  367. case PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_LOWER:
  368. return getLowerAngLimit();
  369. case PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS:
  370. return getSoftnessLimAng();
  371. case PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_RESTITUTION:
  372. return getRestitutionLimAng();
  373. case PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_DAMPING:
  374. return getDampingLimAng();
  375. case PhysicsServer::SLIDER_JOINT_ANGULAR_MOTION_SOFTNESS:
  376. return getSoftnessDirAng();
  377. case PhysicsServer::SLIDER_JOINT_ANGULAR_MOTION_RESTITUTION:
  378. return getRestitutionDirAng();
  379. case PhysicsServer::SLIDER_JOINT_ANGULAR_MOTION_DAMPING:
  380. return getDampingDirAng();
  381. case PhysicsServer::SLIDER_JOINT_ANGULAR_ORTHOGONAL_SOFTNESS:
  382. return getSoftnessOrthoAng();
  383. case PhysicsServer::SLIDER_JOINT_ANGULAR_ORTHOGONAL_RESTITUTION:
  384. return getRestitutionOrthoAng();
  385. case PhysicsServer::SLIDER_JOINT_ANGULAR_ORTHOGONAL_DAMPING:
  386. return getDampingOrthoAng();
  387. default:
  388. return 0;
  389. }
  390. }