generic_6dof_joint_sw.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /**************************************************************************/
  2. /* generic_6dof_joint_sw.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. /*
  31. Adapted to Godot from the Bullet library.
  32. */
  33. /*
  34. Bullet Continuous Collision Detection and Physics Library
  35. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  36. This software is provided 'as-is', without any express or implied warranty.
  37. In no event will the authors be held liable for any damages arising from the use of this software.
  38. Permission is granted to anyone to use this software for any purpose,
  39. including commercial applications, and to alter it and redistribute it freely,
  40. subject to the following restrictions:
  41. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  42. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  43. 3. This notice may not be removed or altered from any source distribution.
  44. */
  45. /*
  46. 2007-09-09
  47. Generic6DOFJointSW Refactored by Francisco Le?n
  48. email: projectileman@yahoo.com
  49. http://gimpact.sf.net
  50. */
  51. #include "generic_6dof_joint_sw.h"
  52. #define GENERIC_D6_DISABLE_WARMSTARTING 1
  53. //////////////////////////// G6DOFRotationalLimitMotorSW ////////////////////////////////////
  54. int G6DOFRotationalLimitMotorSW::testLimitValue(real_t test_value) {
  55. if (m_loLimit > m_hiLimit) {
  56. m_currentLimit = 0; //Free from violation
  57. return 0;
  58. }
  59. if (test_value < m_loLimit) {
  60. m_currentLimit = 1; //low limit violation
  61. m_currentLimitError = test_value - m_loLimit;
  62. return 1;
  63. } else if (test_value > m_hiLimit) {
  64. m_currentLimit = 2; //High limit violation
  65. m_currentLimitError = test_value - m_hiLimit;
  66. return 2;
  67. };
  68. m_currentLimit = 0; //Free from violation
  69. return 0;
  70. }
  71. real_t G6DOFRotationalLimitMotorSW::solveAngularLimits(
  72. real_t timeStep, Vector3 &axis, real_t jacDiagABInv,
  73. BodySW *body0, BodySW *body1) {
  74. if (!needApplyTorques()) {
  75. return 0.0f;
  76. }
  77. real_t target_velocity = m_targetVelocity;
  78. real_t maxMotorForce = m_maxMotorForce;
  79. //current error correction
  80. if (m_currentLimit != 0) {
  81. target_velocity = -m_ERP * m_currentLimitError / (timeStep);
  82. maxMotorForce = m_maxLimitForce;
  83. }
  84. maxMotorForce *= timeStep;
  85. // current velocity difference
  86. Vector3 vel_diff = body0->get_angular_velocity();
  87. if (body1) {
  88. vel_diff -= body1->get_angular_velocity();
  89. }
  90. real_t rel_vel = axis.dot(vel_diff);
  91. // correction velocity
  92. real_t motor_relvel = m_limitSoftness * (target_velocity - m_damping * rel_vel);
  93. if (Math::is_zero_approx(motor_relvel)) {
  94. return 0.0f; //no need for applying force
  95. }
  96. // correction impulse
  97. real_t unclippedMotorImpulse = (1 + m_bounce) * motor_relvel * jacDiagABInv;
  98. // clip correction impulse
  99. real_t clippedMotorImpulse;
  100. ///@todo: should clip against accumulated impulse
  101. if (unclippedMotorImpulse > 0.0f) {
  102. clippedMotorImpulse = unclippedMotorImpulse > maxMotorForce ? maxMotorForce : unclippedMotorImpulse;
  103. } else {
  104. clippedMotorImpulse = unclippedMotorImpulse < -maxMotorForce ? -maxMotorForce : unclippedMotorImpulse;
  105. }
  106. // sort with accumulated impulses
  107. real_t lo = real_t(-1e30);
  108. real_t hi = real_t(1e30);
  109. real_t oldaccumImpulse = m_accumulatedImpulse;
  110. real_t sum = oldaccumImpulse + clippedMotorImpulse;
  111. m_accumulatedImpulse = sum > hi ? real_t(0.) : (sum < lo ? real_t(0.) : sum);
  112. clippedMotorImpulse = m_accumulatedImpulse - oldaccumImpulse;
  113. Vector3 motorImp = clippedMotorImpulse * axis;
  114. body0->apply_torque_impulse(motorImp);
  115. if (body1) {
  116. body1->apply_torque_impulse(-motorImp);
  117. }
  118. return clippedMotorImpulse;
  119. }
  120. //////////////////////////// End G6DOFRotationalLimitMotorSW ////////////////////////////////////
  121. //////////////////////////// G6DOFTranslationalLimitMotorSW ////////////////////////////////////
  122. real_t G6DOFTranslationalLimitMotorSW::solveLinearAxis(
  123. real_t timeStep,
  124. real_t jacDiagABInv,
  125. BodySW *body1, const Vector3 &pointInA,
  126. BodySW *body2, const Vector3 &pointInB,
  127. int limit_index,
  128. const Vector3 &axis_normal_on_a,
  129. const Vector3 &anchorPos) {
  130. ///find relative velocity
  131. // Vector3 rel_pos1 = pointInA - body1->get_transform().origin;
  132. // Vector3 rel_pos2 = pointInB - body2->get_transform().origin;
  133. Vector3 rel_pos1 = anchorPos - body1->get_transform().origin;
  134. Vector3 rel_pos2 = anchorPos - body2->get_transform().origin;
  135. Vector3 vel1 = body1->get_velocity_in_local_point(rel_pos1);
  136. Vector3 vel2 = body2->get_velocity_in_local_point(rel_pos2);
  137. Vector3 vel = vel1 - vel2;
  138. real_t rel_vel = axis_normal_on_a.dot(vel);
  139. /// apply displacement correction
  140. //positional error (zeroth order error)
  141. real_t depth = -(pointInA - pointInB).dot(axis_normal_on_a);
  142. real_t lo = real_t(-1e30);
  143. real_t hi = real_t(1e30);
  144. real_t minLimit = m_lowerLimit[limit_index];
  145. real_t maxLimit = m_upperLimit[limit_index];
  146. //handle the limits
  147. if (minLimit < maxLimit) {
  148. if (depth > maxLimit) {
  149. depth -= maxLimit;
  150. lo = real_t(0.);
  151. } else {
  152. if (depth < minLimit) {
  153. depth -= minLimit;
  154. hi = real_t(0.);
  155. } else {
  156. return 0.0f;
  157. }
  158. }
  159. }
  160. real_t normalImpulse = m_limitSoftness[limit_index] * (m_restitution[limit_index] * depth / timeStep - m_damping[limit_index] * rel_vel) * jacDiagABInv;
  161. real_t oldNormalImpulse = m_accumulatedImpulse[limit_index];
  162. real_t sum = oldNormalImpulse + normalImpulse;
  163. m_accumulatedImpulse[limit_index] = sum > hi ? real_t(0.) : (sum < lo ? real_t(0.) : sum);
  164. normalImpulse = m_accumulatedImpulse[limit_index] - oldNormalImpulse;
  165. Vector3 impulse_vector = axis_normal_on_a * normalImpulse;
  166. body1->apply_impulse(rel_pos1, impulse_vector);
  167. body2->apply_impulse(rel_pos2, -impulse_vector);
  168. return normalImpulse;
  169. }
  170. //////////////////////////// G6DOFTranslationalLimitMotorSW ////////////////////////////////////
  171. Generic6DOFJointSW::Generic6DOFJointSW(BodySW *rbA, BodySW *rbB, const Transform &frameInA, const Transform &frameInB, bool useLinearReferenceFrameA) :
  172. JointSW(_arr, 2),
  173. m_frameInA(frameInA),
  174. m_frameInB(frameInB),
  175. m_useLinearReferenceFrameA(useLinearReferenceFrameA) {
  176. A = rbA;
  177. B = rbB;
  178. A->add_constraint(this, 0);
  179. B->add_constraint(this, 1);
  180. }
  181. void Generic6DOFJointSW::calculateAngleInfo() {
  182. Basis relative_frame = m_calculatedTransformB.basis.inverse() * m_calculatedTransformA.basis;
  183. m_calculatedAxisAngleDiff = relative_frame.get_euler_xyz();
  184. // in euler angle mode we do not actually constrain the angular velocity
  185. // along the axes axis[0] and axis[2] (although we do use axis[1]) :
  186. //
  187. // to get constrain w2-w1 along ...not
  188. // ------ --------------------- ------
  189. // d(angle[0])/dt = 0 ax[1] x ax[2] ax[0]
  190. // d(angle[1])/dt = 0 ax[1]
  191. // d(angle[2])/dt = 0 ax[0] x ax[1] ax[2]
  192. //
  193. // constraining w2-w1 along an axis 'a' means that a'*(w2-w1)=0.
  194. // to prove the result for angle[0], write the expression for angle[0] from
  195. // GetInfo1 then take the derivative. to prove this for angle[2] it is
  196. // easier to take the euler rate expression for d(angle[2])/dt with respect
  197. // to the components of w and set that to 0.
  198. Vector3 axis0 = m_calculatedTransformB.basis.get_axis(0);
  199. Vector3 axis2 = m_calculatedTransformA.basis.get_axis(2);
  200. m_calculatedAxis[1] = axis2.cross(axis0);
  201. m_calculatedAxis[0] = m_calculatedAxis[1].cross(axis2);
  202. m_calculatedAxis[2] = axis0.cross(m_calculatedAxis[1]);
  203. /*
  204. if(m_debugDrawer)
  205. {
  206. char buff[300];
  207. sprintf(buff,"\n X: %.2f ; Y: %.2f ; Z: %.2f ",
  208. m_calculatedAxisAngleDiff[0],
  209. m_calculatedAxisAngleDiff[1],
  210. m_calculatedAxisAngleDiff[2]);
  211. m_debugDrawer->reportErrorWarning(buff);
  212. }
  213. */
  214. }
  215. void Generic6DOFJointSW::calculateTransforms() {
  216. m_calculatedTransformA = A->get_transform() * m_frameInA;
  217. m_calculatedTransformB = B->get_transform() * m_frameInB;
  218. calculateAngleInfo();
  219. }
  220. void Generic6DOFJointSW::buildLinearJacobian(
  221. JacobianEntrySW &jacLinear, const Vector3 &normalWorld,
  222. const Vector3 &pivotAInW, const Vector3 &pivotBInW) {
  223. memnew_placement(
  224. &jacLinear,
  225. JacobianEntrySW(
  226. A->get_principal_inertia_axes().transposed(),
  227. B->get_principal_inertia_axes().transposed(),
  228. pivotAInW - A->get_transform().origin - A->get_center_of_mass(),
  229. pivotBInW - B->get_transform().origin - B->get_center_of_mass(),
  230. normalWorld,
  231. A->get_inv_inertia(),
  232. A->get_inv_mass(),
  233. B->get_inv_inertia(),
  234. B->get_inv_mass()));
  235. }
  236. void Generic6DOFJointSW::buildAngularJacobian(
  237. JacobianEntrySW &jacAngular, const Vector3 &jointAxisW) {
  238. memnew_placement(
  239. &jacAngular,
  240. JacobianEntrySW(
  241. jointAxisW,
  242. A->get_principal_inertia_axes().transposed(),
  243. B->get_principal_inertia_axes().transposed(),
  244. A->get_inv_inertia(),
  245. B->get_inv_inertia()));
  246. }
  247. bool Generic6DOFJointSW::testAngularLimitMotor(int axis_index) {
  248. real_t angle = m_calculatedAxisAngleDiff[axis_index];
  249. //test limits
  250. m_angularLimits[axis_index].testLimitValue(angle);
  251. return m_angularLimits[axis_index].needApplyTorques();
  252. }
  253. bool Generic6DOFJointSW::setup(real_t p_timestep) {
  254. if ((A->get_mode() <= PhysicsServer::BODY_MODE_KINEMATIC) && (B->get_mode() <= PhysicsServer::BODY_MODE_KINEMATIC)) {
  255. return false;
  256. }
  257. // Clear accumulated impulses for the next simulation step
  258. m_linearLimits.m_accumulatedImpulse = Vector3(real_t(0.), real_t(0.), real_t(0.));
  259. int i;
  260. for (i = 0; i < 3; i++) {
  261. m_angularLimits[i].m_accumulatedImpulse = real_t(0.);
  262. }
  263. //calculates transform
  264. calculateTransforms();
  265. // const Vector3& pivotAInW = m_calculatedTransformA.origin;
  266. // const Vector3& pivotBInW = m_calculatedTransformB.origin;
  267. calcAnchorPos();
  268. Vector3 pivotAInW = m_AnchorPos;
  269. Vector3 pivotBInW = m_AnchorPos;
  270. // not used here
  271. // Vector3 rel_pos1 = pivotAInW - A->get_transform().origin;
  272. // Vector3 rel_pos2 = pivotBInW - B->get_transform().origin;
  273. Vector3 normalWorld;
  274. //linear part
  275. for (i = 0; i < 3; i++) {
  276. if (m_linearLimits.enable_limit[i] && m_linearLimits.isLimited(i)) {
  277. if (m_useLinearReferenceFrameA) {
  278. normalWorld = m_calculatedTransformA.basis.get_axis(i);
  279. } else {
  280. normalWorld = m_calculatedTransformB.basis.get_axis(i);
  281. }
  282. buildLinearJacobian(
  283. m_jacLinear[i], normalWorld,
  284. pivotAInW, pivotBInW);
  285. }
  286. }
  287. // angular part
  288. for (i = 0; i < 3; i++) {
  289. //calculates error angle
  290. if (m_angularLimits[i].m_enableLimit && testAngularLimitMotor(i)) {
  291. normalWorld = this->getAxis(i);
  292. // Create angular atom
  293. buildAngularJacobian(m_jacAng[i], normalWorld);
  294. }
  295. }
  296. return true;
  297. }
  298. void Generic6DOFJointSW::solve(real_t p_timestep) {
  299. m_timeStep = p_timestep;
  300. //calculateTransforms();
  301. int i;
  302. // linear
  303. Vector3 pointInA = m_calculatedTransformA.origin;
  304. Vector3 pointInB = m_calculatedTransformB.origin;
  305. real_t jacDiagABInv;
  306. Vector3 linear_axis;
  307. for (i = 0; i < 3; i++) {
  308. if (m_linearLimits.enable_limit[i] && m_linearLimits.isLimited(i)) {
  309. jacDiagABInv = real_t(1.) / m_jacLinear[i].getDiagonal();
  310. if (m_useLinearReferenceFrameA) {
  311. linear_axis = m_calculatedTransformA.basis.get_axis(i);
  312. } else {
  313. linear_axis = m_calculatedTransformB.basis.get_axis(i);
  314. }
  315. m_linearLimits.solveLinearAxis(
  316. m_timeStep,
  317. jacDiagABInv,
  318. A, pointInA,
  319. B, pointInB,
  320. i, linear_axis, m_AnchorPos);
  321. }
  322. }
  323. // angular
  324. Vector3 angular_axis;
  325. real_t angularJacDiagABInv;
  326. for (i = 0; i < 3; i++) {
  327. if (m_angularLimits[i].m_enableLimit && m_angularLimits[i].needApplyTorques()) {
  328. // get axis
  329. angular_axis = getAxis(i);
  330. angularJacDiagABInv = real_t(1.) / m_jacAng[i].getDiagonal();
  331. m_angularLimits[i].solveAngularLimits(m_timeStep, angular_axis, angularJacDiagABInv, A, B);
  332. }
  333. }
  334. }
  335. void Generic6DOFJointSW::updateRHS(real_t timeStep) {
  336. (void)timeStep;
  337. }
  338. Vector3 Generic6DOFJointSW::getAxis(int axis_index) const {
  339. return m_calculatedAxis[axis_index];
  340. }
  341. real_t Generic6DOFJointSW::getAngle(int axis_index) const {
  342. return m_calculatedAxisAngleDiff[axis_index];
  343. }
  344. void Generic6DOFJointSW::calcAnchorPos() {
  345. real_t imA = A->get_inv_mass();
  346. real_t imB = B->get_inv_mass();
  347. real_t weight;
  348. if (imB == real_t(0.0)) {
  349. weight = real_t(1.0);
  350. } else {
  351. weight = imA / (imA + imB);
  352. }
  353. const Vector3 &pA = m_calculatedTransformA.origin;
  354. const Vector3 &pB = m_calculatedTransformB.origin;
  355. m_AnchorPos = pA * weight + pB * (real_t(1.0) - weight);
  356. } // Generic6DOFJointSW::calcAnchorPos()
  357. void Generic6DOFJointSW::set_param(Vector3::Axis p_axis, PhysicsServer::G6DOFJointAxisParam p_param, real_t p_value) {
  358. ERR_FAIL_INDEX(p_axis, 3);
  359. switch (p_param) {
  360. case PhysicsServer::G6DOF_JOINT_LINEAR_LOWER_LIMIT: {
  361. m_linearLimits.m_lowerLimit[p_axis] = p_value;
  362. } break;
  363. case PhysicsServer::G6DOF_JOINT_LINEAR_UPPER_LIMIT: {
  364. m_linearLimits.m_upperLimit[p_axis] = p_value;
  365. } break;
  366. case PhysicsServer::G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS: {
  367. m_linearLimits.m_limitSoftness[p_axis] = p_value;
  368. } break;
  369. case PhysicsServer::G6DOF_JOINT_LINEAR_RESTITUTION: {
  370. m_linearLimits.m_restitution[p_axis] = p_value;
  371. } break;
  372. case PhysicsServer::G6DOF_JOINT_LINEAR_DAMPING: {
  373. m_linearLimits.m_damping[p_axis] = p_value;
  374. } break;
  375. case PhysicsServer::G6DOF_JOINT_ANGULAR_LOWER_LIMIT: {
  376. m_angularLimits[p_axis].m_loLimit = p_value;
  377. } break;
  378. case PhysicsServer::G6DOF_JOINT_ANGULAR_UPPER_LIMIT: {
  379. m_angularLimits[p_axis].m_hiLimit = p_value;
  380. } break;
  381. case PhysicsServer::G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS: {
  382. m_angularLimits[p_axis].m_limitSoftness = p_value;
  383. } break;
  384. case PhysicsServer::G6DOF_JOINT_ANGULAR_DAMPING: {
  385. m_angularLimits[p_axis].m_damping = p_value;
  386. } break;
  387. case PhysicsServer::G6DOF_JOINT_ANGULAR_RESTITUTION: {
  388. m_angularLimits[p_axis].m_bounce = p_value;
  389. } break;
  390. case PhysicsServer::G6DOF_JOINT_ANGULAR_FORCE_LIMIT: {
  391. m_angularLimits[p_axis].m_maxLimitForce = p_value;
  392. } break;
  393. case PhysicsServer::G6DOF_JOINT_ANGULAR_ERP: {
  394. m_angularLimits[p_axis].m_ERP = p_value;
  395. } break;
  396. case PhysicsServer::G6DOF_JOINT_ANGULAR_MOTOR_TARGET_VELOCITY: {
  397. m_angularLimits[p_axis].m_targetVelocity = p_value;
  398. } break;
  399. case PhysicsServer::G6DOF_JOINT_ANGULAR_MOTOR_FORCE_LIMIT: {
  400. m_angularLimits[p_axis].m_maxLimitForce = p_value;
  401. } break;
  402. case PhysicsServer::G6DOF_JOINT_LINEAR_MOTOR_TARGET_VELOCITY: {
  403. // Not implemented in GodotPhysics backend
  404. } break;
  405. case PhysicsServer::G6DOF_JOINT_LINEAR_MOTOR_FORCE_LIMIT: {
  406. // Not implemented in GodotPhysics backend
  407. } break;
  408. case PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_STIFFNESS: {
  409. // Not implemented in GodotPhysics backend
  410. } break;
  411. case PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_DAMPING: {
  412. // Not implemented in GodotPhysics backend
  413. } break;
  414. case PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_EQUILIBRIUM_POINT: {
  415. // Not implemented in GodotPhysics backend
  416. } break;
  417. case PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_STIFFNESS: {
  418. // Not implemented in GodotPhysics backend
  419. } break;
  420. case PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_DAMPING: {
  421. // Not implemented in GodotPhysics backend
  422. } break;
  423. case PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_EQUILIBRIUM_POINT: {
  424. // Not implemented in GodotPhysics backend
  425. } break;
  426. case PhysicsServer::G6DOF_JOINT_MAX:
  427. break; // Can't happen, but silences warning
  428. }
  429. }
  430. real_t Generic6DOFJointSW::get_param(Vector3::Axis p_axis, PhysicsServer::G6DOFJointAxisParam p_param) const {
  431. ERR_FAIL_INDEX_V(p_axis, 3, 0);
  432. switch (p_param) {
  433. case PhysicsServer::G6DOF_JOINT_LINEAR_LOWER_LIMIT: {
  434. return m_linearLimits.m_lowerLimit[p_axis];
  435. } break;
  436. case PhysicsServer::G6DOF_JOINT_LINEAR_UPPER_LIMIT: {
  437. return m_linearLimits.m_upperLimit[p_axis];
  438. } break;
  439. case PhysicsServer::G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS: {
  440. return m_linearLimits.m_limitSoftness[p_axis];
  441. } break;
  442. case PhysicsServer::G6DOF_JOINT_LINEAR_RESTITUTION: {
  443. return m_linearLimits.m_restitution[p_axis];
  444. } break;
  445. case PhysicsServer::G6DOF_JOINT_LINEAR_DAMPING: {
  446. return m_linearLimits.m_damping[p_axis];
  447. } break;
  448. case PhysicsServer::G6DOF_JOINT_ANGULAR_LOWER_LIMIT: {
  449. return m_angularLimits[p_axis].m_loLimit;
  450. } break;
  451. case PhysicsServer::G6DOF_JOINT_ANGULAR_UPPER_LIMIT: {
  452. return m_angularLimits[p_axis].m_hiLimit;
  453. } break;
  454. case PhysicsServer::G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS: {
  455. return m_angularLimits[p_axis].m_limitSoftness;
  456. } break;
  457. case PhysicsServer::G6DOF_JOINT_ANGULAR_DAMPING: {
  458. return m_angularLimits[p_axis].m_damping;
  459. } break;
  460. case PhysicsServer::G6DOF_JOINT_ANGULAR_RESTITUTION: {
  461. return m_angularLimits[p_axis].m_bounce;
  462. } break;
  463. case PhysicsServer::G6DOF_JOINT_ANGULAR_FORCE_LIMIT: {
  464. return m_angularLimits[p_axis].m_maxLimitForce;
  465. } break;
  466. case PhysicsServer::G6DOF_JOINT_ANGULAR_ERP: {
  467. return m_angularLimits[p_axis].m_ERP;
  468. } break;
  469. case PhysicsServer::G6DOF_JOINT_ANGULAR_MOTOR_TARGET_VELOCITY: {
  470. return m_angularLimits[p_axis].m_targetVelocity;
  471. } break;
  472. case PhysicsServer::G6DOF_JOINT_ANGULAR_MOTOR_FORCE_LIMIT: {
  473. return m_angularLimits[p_axis].m_maxMotorForce;
  474. } break;
  475. case PhysicsServer::G6DOF_JOINT_LINEAR_MOTOR_TARGET_VELOCITY: {
  476. // Not implemented in GodotPhysics backend
  477. } break;
  478. case PhysicsServer::G6DOF_JOINT_LINEAR_MOTOR_FORCE_LIMIT: {
  479. // Not implemented in GodotPhysics backend
  480. } break;
  481. case PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_STIFFNESS: {
  482. // Not implemented in GodotPhysics backend
  483. } break;
  484. case PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_DAMPING: {
  485. // Not implemented in GodotPhysics backend
  486. } break;
  487. case PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_EQUILIBRIUM_POINT: {
  488. // Not implemented in GodotPhysics backend
  489. } break;
  490. case PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_STIFFNESS: {
  491. // Not implemented in GodotPhysics backend
  492. } break;
  493. case PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_DAMPING: {
  494. // Not implemented in GodotPhysics backend
  495. } break;
  496. case PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_EQUILIBRIUM_POINT: {
  497. // Not implemented in GodotPhysics backend
  498. } break;
  499. case PhysicsServer::G6DOF_JOINT_MAX:
  500. break; // Can't happen, but silences warning
  501. }
  502. return 0;
  503. }
  504. void Generic6DOFJointSW::set_flag(Vector3::Axis p_axis, PhysicsServer::G6DOFJointAxisFlag p_flag, bool p_value) {
  505. ERR_FAIL_INDEX(p_axis, 3);
  506. switch (p_flag) {
  507. case PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT: {
  508. m_linearLimits.enable_limit[p_axis] = p_value;
  509. } break;
  510. case PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT: {
  511. m_angularLimits[p_axis].m_enableLimit = p_value;
  512. } break;
  513. case PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_MOTOR: {
  514. m_angularLimits[p_axis].m_enableMotor = p_value;
  515. } break;
  516. case PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_MOTOR: {
  517. // Not implemented in GodotPhysics backend
  518. } break;
  519. case PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_SPRING: {
  520. // Not implemented in GodotPhysics backend
  521. } break;
  522. case PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_SPRING: {
  523. // Not implemented in GodotPhysics backend
  524. } break;
  525. case PhysicsServer::G6DOF_JOINT_FLAG_MAX:
  526. break; // Can't happen, but silences warning
  527. }
  528. }
  529. bool Generic6DOFJointSW::get_flag(Vector3::Axis p_axis, PhysicsServer::G6DOFJointAxisFlag p_flag) const {
  530. ERR_FAIL_INDEX_V(p_axis, 3, 0);
  531. switch (p_flag) {
  532. case PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT: {
  533. return m_linearLimits.enable_limit[p_axis];
  534. } break;
  535. case PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT: {
  536. return m_angularLimits[p_axis].m_enableLimit;
  537. } break;
  538. case PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_MOTOR: {
  539. return m_angularLimits[p_axis].m_enableMotor;
  540. } break;
  541. case PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_MOTOR: {
  542. // Not implemented in GodotPhysics backend
  543. } break;
  544. case PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_SPRING: {
  545. // Not implemented in GodotPhysics backend
  546. } break;
  547. case PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_SPRING: {
  548. // Not implemented in GodotPhysics backend
  549. } break;
  550. case PhysicsServer::G6DOF_JOINT_FLAG_MAX:
  551. break; // Can't happen, but silences warning
  552. }
  553. return false;
  554. }