btConeTwistConstraint.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. btConeTwistConstraint is Copyright (c) 2007 Starbreeze Studios
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 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.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. Written by: Marcus Hennix
  13. */
  14. #include "btConeTwistConstraint.h"
  15. #include "bullet/BulletDynamics/Dynamics/btRigidBody.h"
  16. #include "bullet/LinearMath/btTransformUtil.h"
  17. #include "bullet/LinearMath/btMinMax.h"
  18. #include <new>
  19. //#define CONETWIST_USE_OBSOLETE_SOLVER true
  20. #define CONETWIST_USE_OBSOLETE_SOLVER false
  21. #define CONETWIST_DEF_FIX_THRESH btScalar(.05f)
  22. SIMD_FORCE_INLINE btScalar computeAngularImpulseDenominator(const btVector3& axis, const btMatrix3x3& invInertiaWorld)
  23. {
  24. btVector3 vec = axis * invInertiaWorld;
  25. return axis.dot(vec);
  26. }
  27. btConeTwistConstraint::btConeTwistConstraint(btRigidBody& rbA,btRigidBody& rbB,
  28. const btTransform& rbAFrame,const btTransform& rbBFrame)
  29. :btTypedConstraint(CONETWIST_CONSTRAINT_TYPE, rbA,rbB),m_rbAFrame(rbAFrame),m_rbBFrame(rbBFrame),
  30. m_angularOnly(false),
  31. m_useSolveConstraintObsolete(CONETWIST_USE_OBSOLETE_SOLVER)
  32. {
  33. init();
  34. }
  35. btConeTwistConstraint::btConeTwistConstraint(btRigidBody& rbA,const btTransform& rbAFrame)
  36. :btTypedConstraint(CONETWIST_CONSTRAINT_TYPE,rbA),m_rbAFrame(rbAFrame),
  37. m_angularOnly(false),
  38. m_useSolveConstraintObsolete(CONETWIST_USE_OBSOLETE_SOLVER)
  39. {
  40. m_rbBFrame = m_rbAFrame;
  41. m_rbBFrame.setOrigin(btVector3(0., 0., 0.));
  42. init();
  43. }
  44. void btConeTwistConstraint::init()
  45. {
  46. m_angularOnly = false;
  47. m_solveTwistLimit = false;
  48. m_solveSwingLimit = false;
  49. m_bMotorEnabled = false;
  50. m_maxMotorImpulse = btScalar(-1);
  51. setLimit(btScalar(BT_LARGE_FLOAT), btScalar(BT_LARGE_FLOAT), btScalar(BT_LARGE_FLOAT));
  52. m_damping = btScalar(0.01);
  53. m_fixThresh = CONETWIST_DEF_FIX_THRESH;
  54. m_flags = 0;
  55. m_linCFM = btScalar(0.f);
  56. m_linERP = btScalar(0.7f);
  57. m_angCFM = btScalar(0.f);
  58. }
  59. void btConeTwistConstraint::getInfo1 (btConstraintInfo1* info)
  60. {
  61. if (m_useSolveConstraintObsolete)
  62. {
  63. info->m_numConstraintRows = 0;
  64. info->nub = 0;
  65. }
  66. else
  67. {
  68. info->m_numConstraintRows = 3;
  69. info->nub = 3;
  70. calcAngleInfo2(m_rbA.getCenterOfMassTransform(),m_rbB.getCenterOfMassTransform(),m_rbA.getInvInertiaTensorWorld(),m_rbB.getInvInertiaTensorWorld());
  71. if(m_solveSwingLimit)
  72. {
  73. info->m_numConstraintRows++;
  74. info->nub--;
  75. if((m_swingSpan1 < m_fixThresh) && (m_swingSpan2 < m_fixThresh))
  76. {
  77. info->m_numConstraintRows++;
  78. info->nub--;
  79. }
  80. }
  81. if(m_solveTwistLimit)
  82. {
  83. info->m_numConstraintRows++;
  84. info->nub--;
  85. }
  86. }
  87. }
  88. void btConeTwistConstraint::getInfo1NonVirtual (btConstraintInfo1* info)
  89. {
  90. //always reserve 6 rows: object transform is not available on SPU
  91. info->m_numConstraintRows = 6;
  92. info->nub = 0;
  93. }
  94. void btConeTwistConstraint::getInfo2 (btConstraintInfo2* info)
  95. {
  96. getInfo2NonVirtual(info,m_rbA.getCenterOfMassTransform(),m_rbB.getCenterOfMassTransform(),m_rbA.getInvInertiaTensorWorld(),m_rbB.getInvInertiaTensorWorld());
  97. }
  98. void btConeTwistConstraint::getInfo2NonVirtual (btConstraintInfo2* info,const btTransform& transA,const btTransform& transB,const btMatrix3x3& invInertiaWorldA,const btMatrix3x3& invInertiaWorldB)
  99. {
  100. calcAngleInfo2(transA,transB,invInertiaWorldA,invInertiaWorldB);
  101. btAssert(!m_useSolveConstraintObsolete);
  102. // set jacobian
  103. info->m_J1linearAxis[0] = 1;
  104. info->m_J1linearAxis[info->rowskip+1] = 1;
  105. info->m_J1linearAxis[2*info->rowskip+2] = 1;
  106. btVector3 a1 = transA.getBasis() * m_rbAFrame.getOrigin();
  107. {
  108. btVector3* angular0 = (btVector3*)(info->m_J1angularAxis);
  109. btVector3* angular1 = (btVector3*)(info->m_J1angularAxis+info->rowskip);
  110. btVector3* angular2 = (btVector3*)(info->m_J1angularAxis+2*info->rowskip);
  111. btVector3 a1neg = -a1;
  112. a1neg.getSkewSymmetricMatrix(angular0,angular1,angular2);
  113. }
  114. info->m_J2linearAxis[0] = -1;
  115. info->m_J2linearAxis[info->rowskip+1] = -1;
  116. info->m_J2linearAxis[2*info->rowskip+2] = -1;
  117. btVector3 a2 = transB.getBasis() * m_rbBFrame.getOrigin();
  118. {
  119. btVector3* angular0 = (btVector3*)(info->m_J2angularAxis);
  120. btVector3* angular1 = (btVector3*)(info->m_J2angularAxis+info->rowskip);
  121. btVector3* angular2 = (btVector3*)(info->m_J2angularAxis+2*info->rowskip);
  122. a2.getSkewSymmetricMatrix(angular0,angular1,angular2);
  123. }
  124. // set right hand side
  125. btScalar linERP = (m_flags & BT_CONETWIST_FLAGS_LIN_ERP) ? m_linERP : info->erp;
  126. btScalar k = info->fps * linERP;
  127. int j;
  128. for (j=0; j<3; j++)
  129. {
  130. info->m_constraintError[j*info->rowskip] = k * (a2[j] + transB.getOrigin()[j] - a1[j] - transA.getOrigin()[j]);
  131. info->m_lowerLimit[j*info->rowskip] = -SIMD_INFINITY;
  132. info->m_upperLimit[j*info->rowskip] = SIMD_INFINITY;
  133. if(m_flags & BT_CONETWIST_FLAGS_LIN_CFM)
  134. {
  135. info->cfm[j*info->rowskip] = m_linCFM;
  136. }
  137. }
  138. int row = 3;
  139. int srow = row * info->rowskip;
  140. btVector3 ax1;
  141. // angular limits
  142. if(m_solveSwingLimit)
  143. {
  144. btScalar *J1 = info->m_J1angularAxis;
  145. btScalar *J2 = info->m_J2angularAxis;
  146. if((m_swingSpan1 < m_fixThresh) && (m_swingSpan2 < m_fixThresh))
  147. {
  148. btTransform trA = transA*m_rbAFrame;
  149. btVector3 p = trA.getBasis().getColumn(1);
  150. btVector3 q = trA.getBasis().getColumn(2);
  151. int srow1 = srow + info->rowskip;
  152. J1[srow+0] = p[0];
  153. J1[srow+1] = p[1];
  154. J1[srow+2] = p[2];
  155. J1[srow1+0] = q[0];
  156. J1[srow1+1] = q[1];
  157. J1[srow1+2] = q[2];
  158. J2[srow+0] = -p[0];
  159. J2[srow+1] = -p[1];
  160. J2[srow+2] = -p[2];
  161. J2[srow1+0] = -q[0];
  162. J2[srow1+1] = -q[1];
  163. J2[srow1+2] = -q[2];
  164. btScalar fact = info->fps * m_relaxationFactor;
  165. info->m_constraintError[srow] = fact * m_swingAxis.dot(p);
  166. info->m_constraintError[srow1] = fact * m_swingAxis.dot(q);
  167. info->m_lowerLimit[srow] = -SIMD_INFINITY;
  168. info->m_upperLimit[srow] = SIMD_INFINITY;
  169. info->m_lowerLimit[srow1] = -SIMD_INFINITY;
  170. info->m_upperLimit[srow1] = SIMD_INFINITY;
  171. srow = srow1 + info->rowskip;
  172. }
  173. else
  174. {
  175. ax1 = m_swingAxis * m_relaxationFactor * m_relaxationFactor;
  176. J1[srow+0] = ax1[0];
  177. J1[srow+1] = ax1[1];
  178. J1[srow+2] = ax1[2];
  179. J2[srow+0] = -ax1[0];
  180. J2[srow+1] = -ax1[1];
  181. J2[srow+2] = -ax1[2];
  182. btScalar k = info->fps * m_biasFactor;
  183. info->m_constraintError[srow] = k * m_swingCorrection;
  184. if(m_flags & BT_CONETWIST_FLAGS_ANG_CFM)
  185. {
  186. info->cfm[srow] = m_angCFM;
  187. }
  188. // m_swingCorrection is always positive or 0
  189. info->m_lowerLimit[srow] = 0;
  190. info->m_upperLimit[srow] = SIMD_INFINITY;
  191. srow += info->rowskip;
  192. }
  193. }
  194. if(m_solveTwistLimit)
  195. {
  196. ax1 = m_twistAxis * m_relaxationFactor * m_relaxationFactor;
  197. btScalar *J1 = info->m_J1angularAxis;
  198. btScalar *J2 = info->m_J2angularAxis;
  199. J1[srow+0] = ax1[0];
  200. J1[srow+1] = ax1[1];
  201. J1[srow+2] = ax1[2];
  202. J2[srow+0] = -ax1[0];
  203. J2[srow+1] = -ax1[1];
  204. J2[srow+2] = -ax1[2];
  205. btScalar k = info->fps * m_biasFactor;
  206. info->m_constraintError[srow] = k * m_twistCorrection;
  207. if(m_flags & BT_CONETWIST_FLAGS_ANG_CFM)
  208. {
  209. info->cfm[srow] = m_angCFM;
  210. }
  211. if(m_twistSpan > 0.0f)
  212. {
  213. if(m_twistCorrection > 0.0f)
  214. {
  215. info->m_lowerLimit[srow] = 0;
  216. info->m_upperLimit[srow] = SIMD_INFINITY;
  217. }
  218. else
  219. {
  220. info->m_lowerLimit[srow] = -SIMD_INFINITY;
  221. info->m_upperLimit[srow] = 0;
  222. }
  223. }
  224. else
  225. {
  226. info->m_lowerLimit[srow] = -SIMD_INFINITY;
  227. info->m_upperLimit[srow] = SIMD_INFINITY;
  228. }
  229. srow += info->rowskip;
  230. }
  231. }
  232. void btConeTwistConstraint::buildJacobian()
  233. {
  234. if (m_useSolveConstraintObsolete)
  235. {
  236. m_appliedImpulse = btScalar(0.);
  237. m_accTwistLimitImpulse = btScalar(0.);
  238. m_accSwingLimitImpulse = btScalar(0.);
  239. m_accMotorImpulse = btVector3(0.,0.,0.);
  240. if (!m_angularOnly)
  241. {
  242. btVector3 pivotAInW = m_rbA.getCenterOfMassTransform()*m_rbAFrame.getOrigin();
  243. btVector3 pivotBInW = m_rbB.getCenterOfMassTransform()*m_rbBFrame.getOrigin();
  244. btVector3 relPos = pivotBInW - pivotAInW;
  245. btVector3 normal[3];
  246. if (relPos.length2() > SIMD_EPSILON)
  247. {
  248. normal[0] = relPos.normalized();
  249. }
  250. else
  251. {
  252. normal[0].setValue(btScalar(1.0),0,0);
  253. }
  254. btPlaneSpace1(normal[0], normal[1], normal[2]);
  255. for (int i=0;i<3;i++)
  256. {
  257. new (&m_jac[i]) btJacobianEntry(
  258. m_rbA.getCenterOfMassTransform().getBasis().transpose(),
  259. m_rbB.getCenterOfMassTransform().getBasis().transpose(),
  260. pivotAInW - m_rbA.getCenterOfMassPosition(),
  261. pivotBInW - m_rbB.getCenterOfMassPosition(),
  262. normal[i],
  263. m_rbA.getInvInertiaDiagLocal(),
  264. m_rbA.getInvMass(),
  265. m_rbB.getInvInertiaDiagLocal(),
  266. m_rbB.getInvMass());
  267. }
  268. }
  269. calcAngleInfo2(m_rbA.getCenterOfMassTransform(),m_rbB.getCenterOfMassTransform(),m_rbA.getInvInertiaTensorWorld(),m_rbB.getInvInertiaTensorWorld());
  270. }
  271. }
  272. void btConeTwistConstraint::solveConstraintObsolete(btSolverBody& bodyA,btSolverBody& bodyB,btScalar timeStep)
  273. {
  274. #ifndef __SPU__
  275. if (m_useSolveConstraintObsolete)
  276. {
  277. btVector3 pivotAInW = m_rbA.getCenterOfMassTransform()*m_rbAFrame.getOrigin();
  278. btVector3 pivotBInW = m_rbB.getCenterOfMassTransform()*m_rbBFrame.getOrigin();
  279. btScalar tau = btScalar(0.3);
  280. //linear part
  281. if (!m_angularOnly)
  282. {
  283. btVector3 rel_pos1 = pivotAInW - m_rbA.getCenterOfMassPosition();
  284. btVector3 rel_pos2 = pivotBInW - m_rbB.getCenterOfMassPosition();
  285. btVector3 vel1;
  286. bodyA.internalGetVelocityInLocalPointObsolete(rel_pos1,vel1);
  287. btVector3 vel2;
  288. bodyB.internalGetVelocityInLocalPointObsolete(rel_pos2,vel2);
  289. btVector3 vel = vel1 - vel2;
  290. for (int i=0;i<3;i++)
  291. {
  292. const btVector3& normal = m_jac[i].m_linearJointAxis;
  293. btScalar jacDiagABInv = btScalar(1.) / m_jac[i].getDiagonal();
  294. btScalar rel_vel;
  295. rel_vel = normal.dot(vel);
  296. //positional error (zeroth order error)
  297. btScalar depth = -(pivotAInW - pivotBInW).dot(normal); //this is the error projected on the normal
  298. btScalar impulse = depth*tau/timeStep * jacDiagABInv - rel_vel * jacDiagABInv;
  299. m_appliedImpulse += impulse;
  300. btVector3 ftorqueAxis1 = rel_pos1.cross(normal);
  301. btVector3 ftorqueAxis2 = rel_pos2.cross(normal);
  302. bodyA.internalApplyImpulse(normal*m_rbA.getInvMass(), m_rbA.getInvInertiaTensorWorld()*ftorqueAxis1,impulse);
  303. bodyB.internalApplyImpulse(normal*m_rbB.getInvMass(), m_rbB.getInvInertiaTensorWorld()*ftorqueAxis2,-impulse);
  304. }
  305. }
  306. // apply motor
  307. if (m_bMotorEnabled)
  308. {
  309. // compute current and predicted transforms
  310. btTransform trACur = m_rbA.getCenterOfMassTransform();
  311. btTransform trBCur = m_rbB.getCenterOfMassTransform();
  312. btVector3 omegaA; bodyA.internalGetAngularVelocity(omegaA);
  313. btVector3 omegaB; bodyB.internalGetAngularVelocity(omegaB);
  314. btTransform trAPred; trAPred.setIdentity();
  315. btVector3 zerovec(0,0,0);
  316. btTransformUtil::integrateTransform(
  317. trACur, zerovec, omegaA, timeStep, trAPred);
  318. btTransform trBPred; trBPred.setIdentity();
  319. btTransformUtil::integrateTransform(
  320. trBCur, zerovec, omegaB, timeStep, trBPred);
  321. // compute desired transforms in world
  322. btTransform trPose(m_qTarget);
  323. btTransform trABDes = m_rbBFrame * trPose * m_rbAFrame.inverse();
  324. btTransform trADes = trBPred * trABDes;
  325. btTransform trBDes = trAPred * trABDes.inverse();
  326. // compute desired omegas in world
  327. btVector3 omegaADes, omegaBDes;
  328. btTransformUtil::calculateVelocity(trACur, trADes, timeStep, zerovec, omegaADes);
  329. btTransformUtil::calculateVelocity(trBCur, trBDes, timeStep, zerovec, omegaBDes);
  330. // compute delta omegas
  331. btVector3 dOmegaA = omegaADes - omegaA;
  332. btVector3 dOmegaB = omegaBDes - omegaB;
  333. // compute weighted avg axis of dOmega (weighting based on inertias)
  334. btVector3 axisA, axisB;
  335. btScalar kAxisAInv = 0, kAxisBInv = 0;
  336. if (dOmegaA.length2() > SIMD_EPSILON)
  337. {
  338. axisA = dOmegaA.normalized();
  339. kAxisAInv = getRigidBodyA().computeAngularImpulseDenominator(axisA);
  340. }
  341. if (dOmegaB.length2() > SIMD_EPSILON)
  342. {
  343. axisB = dOmegaB.normalized();
  344. kAxisBInv = getRigidBodyB().computeAngularImpulseDenominator(axisB);
  345. }
  346. btVector3 avgAxis = kAxisAInv * axisA + kAxisBInv * axisB;
  347. static bool bDoTorque = true;
  348. if (bDoTorque && avgAxis.length2() > SIMD_EPSILON)
  349. {
  350. avgAxis.normalize();
  351. kAxisAInv = getRigidBodyA().computeAngularImpulseDenominator(avgAxis);
  352. kAxisBInv = getRigidBodyB().computeAngularImpulseDenominator(avgAxis);
  353. btScalar kInvCombined = kAxisAInv + kAxisBInv;
  354. btVector3 impulse = (kAxisAInv * dOmegaA - kAxisBInv * dOmegaB) /
  355. (kInvCombined * kInvCombined);
  356. if (m_maxMotorImpulse >= 0)
  357. {
  358. btScalar fMaxImpulse = m_maxMotorImpulse;
  359. if (m_bNormalizedMotorStrength)
  360. fMaxImpulse = fMaxImpulse/kAxisAInv;
  361. btVector3 newUnclampedAccImpulse = m_accMotorImpulse + impulse;
  362. btScalar newUnclampedMag = newUnclampedAccImpulse.length();
  363. if (newUnclampedMag > fMaxImpulse)
  364. {
  365. newUnclampedAccImpulse.normalize();
  366. newUnclampedAccImpulse *= fMaxImpulse;
  367. impulse = newUnclampedAccImpulse - m_accMotorImpulse;
  368. }
  369. m_accMotorImpulse += impulse;
  370. }
  371. btScalar impulseMag = impulse.length();
  372. btVector3 impulseAxis = impulse / impulseMag;
  373. bodyA.internalApplyImpulse(btVector3(0,0,0), m_rbA.getInvInertiaTensorWorld()*impulseAxis, impulseMag);
  374. bodyB.internalApplyImpulse(btVector3(0,0,0), m_rbB.getInvInertiaTensorWorld()*impulseAxis, -impulseMag);
  375. }
  376. }
  377. else if (m_damping > SIMD_EPSILON) // no motor: do a little damping
  378. {
  379. btVector3 angVelA; bodyA.internalGetAngularVelocity(angVelA);
  380. btVector3 angVelB; bodyB.internalGetAngularVelocity(angVelB);
  381. btVector3 relVel = angVelB - angVelA;
  382. if (relVel.length2() > SIMD_EPSILON)
  383. {
  384. btVector3 relVelAxis = relVel.normalized();
  385. btScalar m_kDamping = btScalar(1.) /
  386. (getRigidBodyA().computeAngularImpulseDenominator(relVelAxis) +
  387. getRigidBodyB().computeAngularImpulseDenominator(relVelAxis));
  388. btVector3 impulse = m_damping * m_kDamping * relVel;
  389. btScalar impulseMag = impulse.length();
  390. btVector3 impulseAxis = impulse / impulseMag;
  391. bodyA.internalApplyImpulse(btVector3(0,0,0), m_rbA.getInvInertiaTensorWorld()*impulseAxis, impulseMag);
  392. bodyB.internalApplyImpulse(btVector3(0,0,0), m_rbB.getInvInertiaTensorWorld()*impulseAxis, -impulseMag);
  393. }
  394. }
  395. // joint limits
  396. {
  397. ///solve angular part
  398. btVector3 angVelA;
  399. bodyA.internalGetAngularVelocity(angVelA);
  400. btVector3 angVelB;
  401. bodyB.internalGetAngularVelocity(angVelB);
  402. // solve swing limit
  403. if (m_solveSwingLimit)
  404. {
  405. btScalar amplitude = m_swingLimitRatio * m_swingCorrection*m_biasFactor/timeStep;
  406. btScalar relSwingVel = (angVelB - angVelA).dot(m_swingAxis);
  407. if (relSwingVel > 0)
  408. amplitude += m_swingLimitRatio * relSwingVel * m_relaxationFactor;
  409. btScalar impulseMag = amplitude * m_kSwing;
  410. // Clamp the accumulated impulse
  411. btScalar temp = m_accSwingLimitImpulse;
  412. m_accSwingLimitImpulse = btMax(m_accSwingLimitImpulse + impulseMag, btScalar(0.0) );
  413. impulseMag = m_accSwingLimitImpulse - temp;
  414. btVector3 impulse = m_swingAxis * impulseMag;
  415. // don't let cone response affect twist
  416. // (this can happen since body A's twist doesn't match body B's AND we use an elliptical cone limit)
  417. {
  418. btVector3 impulseTwistCouple = impulse.dot(m_twistAxisA) * m_twistAxisA;
  419. btVector3 impulseNoTwistCouple = impulse - impulseTwistCouple;
  420. impulse = impulseNoTwistCouple;
  421. }
  422. impulseMag = impulse.length();
  423. btVector3 noTwistSwingAxis = impulse / impulseMag;
  424. bodyA.internalApplyImpulse(btVector3(0,0,0), m_rbA.getInvInertiaTensorWorld()*noTwistSwingAxis, impulseMag);
  425. bodyB.internalApplyImpulse(btVector3(0,0,0), m_rbB.getInvInertiaTensorWorld()*noTwistSwingAxis, -impulseMag);
  426. }
  427. // solve twist limit
  428. if (m_solveTwistLimit)
  429. {
  430. btScalar amplitude = m_twistLimitRatio * m_twistCorrection*m_biasFactor/timeStep;
  431. btScalar relTwistVel = (angVelB - angVelA).dot( m_twistAxis );
  432. if (relTwistVel > 0) // only damp when moving towards limit (m_twistAxis flipping is important)
  433. amplitude += m_twistLimitRatio * relTwistVel * m_relaxationFactor;
  434. btScalar impulseMag = amplitude * m_kTwist;
  435. // Clamp the accumulated impulse
  436. btScalar temp = m_accTwistLimitImpulse;
  437. m_accTwistLimitImpulse = btMax(m_accTwistLimitImpulse + impulseMag, btScalar(0.0) );
  438. impulseMag = m_accTwistLimitImpulse - temp;
  439. // btVector3 impulse = m_twistAxis * impulseMag;
  440. bodyA.internalApplyImpulse(btVector3(0,0,0), m_rbA.getInvInertiaTensorWorld()*m_twistAxis,impulseMag);
  441. bodyB.internalApplyImpulse(btVector3(0,0,0), m_rbB.getInvInertiaTensorWorld()*m_twistAxis,-impulseMag);
  442. }
  443. }
  444. }
  445. #else
  446. btAssert(0);
  447. #endif //__SPU__
  448. }
  449. void btConeTwistConstraint::updateRHS(btScalar timeStep)
  450. {
  451. (void)timeStep;
  452. }
  453. #ifndef __SPU__
  454. void btConeTwistConstraint::calcAngleInfo()
  455. {
  456. m_swingCorrection = btScalar(0.);
  457. m_twistLimitSign = btScalar(0.);
  458. m_solveTwistLimit = false;
  459. m_solveSwingLimit = false;
  460. btVector3 b1Axis1,b1Axis2,b1Axis3;
  461. btVector3 b2Axis1,b2Axis2;
  462. b1Axis1 = getRigidBodyA().getCenterOfMassTransform().getBasis() * this->m_rbAFrame.getBasis().getColumn(0);
  463. b2Axis1 = getRigidBodyB().getCenterOfMassTransform().getBasis() * this->m_rbBFrame.getBasis().getColumn(0);
  464. btScalar swing1=btScalar(0.),swing2 = btScalar(0.);
  465. btScalar swx=btScalar(0.),swy = btScalar(0.);
  466. btScalar thresh = btScalar(10.);
  467. btScalar fact;
  468. // Get Frame into world space
  469. if (m_swingSpan1 >= btScalar(0.05f))
  470. {
  471. b1Axis2 = getRigidBodyA().getCenterOfMassTransform().getBasis() * this->m_rbAFrame.getBasis().getColumn(1);
  472. swx = b2Axis1.dot(b1Axis1);
  473. swy = b2Axis1.dot(b1Axis2);
  474. swing1 = btAtan2Fast(swy, swx);
  475. fact = (swy*swy + swx*swx) * thresh * thresh;
  476. fact = fact / (fact + btScalar(1.0));
  477. swing1 *= fact;
  478. }
  479. if (m_swingSpan2 >= btScalar(0.05f))
  480. {
  481. b1Axis3 = getRigidBodyA().getCenterOfMassTransform().getBasis() * this->m_rbAFrame.getBasis().getColumn(2);
  482. swx = b2Axis1.dot(b1Axis1);
  483. swy = b2Axis1.dot(b1Axis3);
  484. swing2 = btAtan2Fast(swy, swx);
  485. fact = (swy*swy + swx*swx) * thresh * thresh;
  486. fact = fact / (fact + btScalar(1.0));
  487. swing2 *= fact;
  488. }
  489. btScalar RMaxAngle1Sq = 1.0f / (m_swingSpan1*m_swingSpan1);
  490. btScalar RMaxAngle2Sq = 1.0f / (m_swingSpan2*m_swingSpan2);
  491. btScalar EllipseAngle = btFabs(swing1*swing1)* RMaxAngle1Sq + btFabs(swing2*swing2) * RMaxAngle2Sq;
  492. if (EllipseAngle > 1.0f)
  493. {
  494. m_swingCorrection = EllipseAngle-1.0f;
  495. m_solveSwingLimit = true;
  496. // Calculate necessary axis & factors
  497. m_swingAxis = b2Axis1.cross(b1Axis2* b2Axis1.dot(b1Axis2) + b1Axis3* b2Axis1.dot(b1Axis3));
  498. m_swingAxis.normalize();
  499. btScalar swingAxisSign = (b2Axis1.dot(b1Axis1) >= 0.0f) ? 1.0f : -1.0f;
  500. m_swingAxis *= swingAxisSign;
  501. }
  502. // Twist limits
  503. if (m_twistSpan >= btScalar(0.))
  504. {
  505. btVector3 b2Axis2 = getRigidBodyB().getCenterOfMassTransform().getBasis() * this->m_rbBFrame.getBasis().getColumn(1);
  506. btQuaternion rotationArc = shortestArcQuat(b2Axis1,b1Axis1);
  507. btVector3 TwistRef = quatRotate(rotationArc,b2Axis2);
  508. btScalar twist = btAtan2Fast( TwistRef.dot(b1Axis3), TwistRef.dot(b1Axis2) );
  509. m_twistAngle = twist;
  510. // btScalar lockedFreeFactor = (m_twistSpan > btScalar(0.05f)) ? m_limitSoftness : btScalar(0.);
  511. btScalar lockedFreeFactor = (m_twistSpan > btScalar(0.05f)) ? btScalar(1.0f) : btScalar(0.);
  512. if (twist <= -m_twistSpan*lockedFreeFactor)
  513. {
  514. m_twistCorrection = -(twist + m_twistSpan);
  515. m_solveTwistLimit = true;
  516. m_twistAxis = (b2Axis1 + b1Axis1) * 0.5f;
  517. m_twistAxis.normalize();
  518. m_twistAxis *= -1.0f;
  519. }
  520. else if (twist > m_twistSpan*lockedFreeFactor)
  521. {
  522. m_twistCorrection = (twist - m_twistSpan);
  523. m_solveTwistLimit = true;
  524. m_twistAxis = (b2Axis1 + b1Axis1) * 0.5f;
  525. m_twistAxis.normalize();
  526. }
  527. }
  528. }
  529. #endif //__SPU__
  530. static btVector3 vTwist(1,0,0); // twist axis in constraint's space
  531. void btConeTwistConstraint::calcAngleInfo2(const btTransform& transA, const btTransform& transB, const btMatrix3x3& invInertiaWorldA,const btMatrix3x3& invInertiaWorldB)
  532. {
  533. m_swingCorrection = btScalar(0.);
  534. m_twistLimitSign = btScalar(0.);
  535. m_solveTwistLimit = false;
  536. m_solveSwingLimit = false;
  537. // compute rotation of A wrt B (in constraint space)
  538. if (m_bMotorEnabled && (!m_useSolveConstraintObsolete))
  539. { // it is assumed that setMotorTarget() was alredy called
  540. // and motor target m_qTarget is within constraint limits
  541. // TODO : split rotation to pure swing and pure twist
  542. // compute desired transforms in world
  543. btTransform trPose(m_qTarget);
  544. btTransform trA = transA * m_rbAFrame;
  545. btTransform trB = transB * m_rbBFrame;
  546. btTransform trDeltaAB = trB * trPose * trA.inverse();
  547. btQuaternion qDeltaAB = trDeltaAB.getRotation();
  548. btVector3 swingAxis = btVector3(qDeltaAB.x(), qDeltaAB.y(), qDeltaAB.z());
  549. float swingAxisLen2 = swingAxis.length2();
  550. if(btFuzzyZero(swingAxisLen2))
  551. {
  552. return;
  553. }
  554. m_swingAxis = swingAxis;
  555. m_swingAxis.normalize();
  556. m_swingCorrection = qDeltaAB.getAngle();
  557. if(!btFuzzyZero(m_swingCorrection))
  558. {
  559. m_solveSwingLimit = true;
  560. }
  561. return;
  562. }
  563. {
  564. // compute rotation of A wrt B (in constraint space)
  565. btQuaternion qA = transA.getRotation() * m_rbAFrame.getRotation();
  566. btQuaternion qB = transB.getRotation() * m_rbBFrame.getRotation();
  567. btQuaternion qAB = qB.inverse() * qA;
  568. // split rotation into cone and twist
  569. // (all this is done from B's perspective. Maybe I should be averaging axes...)
  570. btVector3 vConeNoTwist = quatRotate(qAB, vTwist); vConeNoTwist.normalize();
  571. btQuaternion qABCone = shortestArcQuat(vTwist, vConeNoTwist); qABCone.normalize();
  572. btQuaternion qABTwist = qABCone.inverse() * qAB; qABTwist.normalize();
  573. if (m_swingSpan1 >= m_fixThresh && m_swingSpan2 >= m_fixThresh)
  574. {
  575. btScalar swingAngle, swingLimit = 0; btVector3 swingAxis;
  576. computeConeLimitInfo(qABCone, swingAngle, swingAxis, swingLimit);
  577. if (swingAngle > swingLimit * m_limitSoftness)
  578. {
  579. m_solveSwingLimit = true;
  580. // compute limit ratio: 0->1, where
  581. // 0 == beginning of soft limit
  582. // 1 == hard/real limit
  583. m_swingLimitRatio = 1.f;
  584. if (swingAngle < swingLimit && m_limitSoftness < 1.f - SIMD_EPSILON)
  585. {
  586. m_swingLimitRatio = (swingAngle - swingLimit * m_limitSoftness)/
  587. (swingLimit - swingLimit * m_limitSoftness);
  588. }
  589. // swing correction tries to get back to soft limit
  590. m_swingCorrection = swingAngle - (swingLimit * m_limitSoftness);
  591. // adjustment of swing axis (based on ellipse normal)
  592. adjustSwingAxisToUseEllipseNormal(swingAxis);
  593. // Calculate necessary axis & factors
  594. m_swingAxis = quatRotate(qB, -swingAxis);
  595. m_twistAxisA.setValue(0,0,0);
  596. m_kSwing = btScalar(1.) /
  597. (computeAngularImpulseDenominator(m_swingAxis,invInertiaWorldA) +
  598. computeAngularImpulseDenominator(m_swingAxis,invInertiaWorldB));
  599. }
  600. }
  601. else
  602. {
  603. // you haven't set any limits;
  604. // or you're trying to set at least one of the swing limits too small. (if so, do you really want a conetwist constraint?)
  605. // anyway, we have either hinge or fixed joint
  606. btVector3 ivA = transA.getBasis() * m_rbAFrame.getBasis().getColumn(0);
  607. btVector3 jvA = transA.getBasis() * m_rbAFrame.getBasis().getColumn(1);
  608. btVector3 kvA = transA.getBasis() * m_rbAFrame.getBasis().getColumn(2);
  609. btVector3 ivB = transB.getBasis() * m_rbBFrame.getBasis().getColumn(0);
  610. btVector3 target;
  611. btScalar x = ivB.dot(ivA);
  612. btScalar y = ivB.dot(jvA);
  613. btScalar z = ivB.dot(kvA);
  614. if((m_swingSpan1 < m_fixThresh) && (m_swingSpan2 < m_fixThresh))
  615. { // fixed. We'll need to add one more row to constraint
  616. if((!btFuzzyZero(y)) || (!(btFuzzyZero(z))))
  617. {
  618. m_solveSwingLimit = true;
  619. m_swingAxis = -ivB.cross(ivA);
  620. }
  621. }
  622. else
  623. {
  624. if(m_swingSpan1 < m_fixThresh)
  625. { // hinge around Y axis
  626. // if(!(btFuzzyZero(y)))
  627. if((!(btFuzzyZero(x))) || (!(btFuzzyZero(z))))
  628. {
  629. m_solveSwingLimit = true;
  630. if(m_swingSpan2 >= m_fixThresh)
  631. {
  632. y = btScalar(0.f);
  633. btScalar span2 = btAtan2(z, x);
  634. if(span2 > m_swingSpan2)
  635. {
  636. x = btCos(m_swingSpan2);
  637. z = btSin(m_swingSpan2);
  638. }
  639. else if(span2 < -m_swingSpan2)
  640. {
  641. x = btCos(m_swingSpan2);
  642. z = -btSin(m_swingSpan2);
  643. }
  644. }
  645. }
  646. }
  647. else
  648. { // hinge around Z axis
  649. // if(!btFuzzyZero(z))
  650. if((!(btFuzzyZero(x))) || (!(btFuzzyZero(y))))
  651. {
  652. m_solveSwingLimit = true;
  653. if(m_swingSpan1 >= m_fixThresh)
  654. {
  655. z = btScalar(0.f);
  656. btScalar span1 = btAtan2(y, x);
  657. if(span1 > m_swingSpan1)
  658. {
  659. x = btCos(m_swingSpan1);
  660. y = btSin(m_swingSpan1);
  661. }
  662. else if(span1 < -m_swingSpan1)
  663. {
  664. x = btCos(m_swingSpan1);
  665. y = -btSin(m_swingSpan1);
  666. }
  667. }
  668. }
  669. }
  670. target[0] = x * ivA[0] + y * jvA[0] + z * kvA[0];
  671. target[1] = x * ivA[1] + y * jvA[1] + z * kvA[1];
  672. target[2] = x * ivA[2] + y * jvA[2] + z * kvA[2];
  673. target.normalize();
  674. m_swingAxis = -ivB.cross(target);
  675. m_swingCorrection = m_swingAxis.length();
  676. m_swingAxis.normalize();
  677. }
  678. }
  679. if (m_twistSpan >= btScalar(0.f))
  680. {
  681. btVector3 twistAxis;
  682. computeTwistLimitInfo(qABTwist, m_twistAngle, twistAxis);
  683. if (m_twistAngle > m_twistSpan*m_limitSoftness)
  684. {
  685. m_solveTwistLimit = true;
  686. m_twistLimitRatio = 1.f;
  687. if (m_twistAngle < m_twistSpan && m_limitSoftness < 1.f - SIMD_EPSILON)
  688. {
  689. m_twistLimitRatio = (m_twistAngle - m_twistSpan * m_limitSoftness)/
  690. (m_twistSpan - m_twistSpan * m_limitSoftness);
  691. }
  692. // twist correction tries to get back to soft limit
  693. m_twistCorrection = m_twistAngle - (m_twistSpan * m_limitSoftness);
  694. m_twistAxis = quatRotate(qB, -twistAxis);
  695. m_kTwist = btScalar(1.) /
  696. (computeAngularImpulseDenominator(m_twistAxis,invInertiaWorldA) +
  697. computeAngularImpulseDenominator(m_twistAxis,invInertiaWorldB));
  698. }
  699. if (m_solveSwingLimit)
  700. m_twistAxisA = quatRotate(qA, -twistAxis);
  701. }
  702. else
  703. {
  704. m_twistAngle = btScalar(0.f);
  705. }
  706. }
  707. }
  708. // given a cone rotation in constraint space, (pre: twist must already be removed)
  709. // this method computes its corresponding swing angle and axis.
  710. // more interestingly, it computes the cone/swing limit (angle) for this cone "pose".
  711. void btConeTwistConstraint::computeConeLimitInfo(const btQuaternion& qCone,
  712. btScalar& swingAngle, // out
  713. btVector3& vSwingAxis, // out
  714. btScalar& swingLimit) // out
  715. {
  716. swingAngle = qCone.getAngle();
  717. if (swingAngle > SIMD_EPSILON)
  718. {
  719. vSwingAxis = btVector3(qCone.x(), qCone.y(), qCone.z());
  720. vSwingAxis.normalize();
  721. #if 0
  722. // non-zero twist?! this should never happen.
  723. btAssert(fabs(vSwingAxis.x()) <= SIMD_EPSILON));
  724. #endif
  725. // Compute limit for given swing. tricky:
  726. // Given a swing axis, we're looking for the intersection with the bounding cone ellipse.
  727. // (Since we're dealing with angles, this ellipse is embedded on the surface of a sphere.)
  728. // For starters, compute the direction from center to surface of ellipse.
  729. // This is just the perpendicular (ie. rotate 2D vector by PI/2) of the swing axis.
  730. // (vSwingAxis is the cone rotation (in z,y); change vars and rotate to (x,y) coords.)
  731. btScalar xEllipse = vSwingAxis.y();
  732. btScalar yEllipse = -vSwingAxis.z();
  733. // Now, we use the slope of the vector (using x/yEllipse) and find the length
  734. // of the line that intersects the ellipse:
  735. // x^2 y^2
  736. // --- + --- = 1, where a and b are semi-major axes 2 and 1 respectively (ie. the limits)
  737. // a^2 b^2
  738. // Do the math and it should be clear.
  739. swingLimit = m_swingSpan1; // if xEllipse == 0, we have a pure vSwingAxis.z rotation: just use swingspan1
  740. if (fabs(xEllipse) > SIMD_EPSILON)
  741. {
  742. btScalar surfaceSlope2 = (yEllipse*yEllipse)/(xEllipse*xEllipse);
  743. btScalar norm = 1 / (m_swingSpan2 * m_swingSpan2);
  744. norm += surfaceSlope2 / (m_swingSpan1 * m_swingSpan1);
  745. btScalar swingLimit2 = (1 + surfaceSlope2) / norm;
  746. swingLimit = sqrt(swingLimit2);
  747. }
  748. // test!
  749. /*swingLimit = m_swingSpan2;
  750. if (fabs(vSwingAxis.z()) > SIMD_EPSILON)
  751. {
  752. btScalar mag_2 = m_swingSpan1*m_swingSpan1 + m_swingSpan2*m_swingSpan2;
  753. btScalar sinphi = m_swingSpan2 / sqrt(mag_2);
  754. btScalar phi = asin(sinphi);
  755. btScalar theta = atan2(fabs(vSwingAxis.y()),fabs(vSwingAxis.z()));
  756. btScalar alpha = 3.14159f - theta - phi;
  757. btScalar sinalpha = sin(alpha);
  758. swingLimit = m_swingSpan1 * sinphi/sinalpha;
  759. }*/
  760. }
  761. else if (swingAngle < 0)
  762. {
  763. // this should never happen!
  764. #if 0
  765. btAssert(0);
  766. #endif
  767. }
  768. }
  769. btVector3 btConeTwistConstraint::GetPointForAngle(btScalar fAngleInRadians, btScalar fLength) const
  770. {
  771. // compute x/y in ellipse using cone angle (0 -> 2*PI along surface of cone)
  772. btScalar xEllipse = btCos(fAngleInRadians);
  773. btScalar yEllipse = btSin(fAngleInRadians);
  774. // Use the slope of the vector (using x/yEllipse) and find the length
  775. // of the line that intersects the ellipse:
  776. // x^2 y^2
  777. // --- + --- = 1, where a and b are semi-major axes 2 and 1 respectively (ie. the limits)
  778. // a^2 b^2
  779. // Do the math and it should be clear.
  780. float swingLimit = m_swingSpan1; // if xEllipse == 0, just use axis b (1)
  781. if (fabs(xEllipse) > SIMD_EPSILON)
  782. {
  783. btScalar surfaceSlope2 = (yEllipse*yEllipse)/(xEllipse*xEllipse);
  784. btScalar norm = 1 / (m_swingSpan2 * m_swingSpan2);
  785. norm += surfaceSlope2 / (m_swingSpan1 * m_swingSpan1);
  786. btScalar swingLimit2 = (1 + surfaceSlope2) / norm;
  787. swingLimit = sqrt(swingLimit2);
  788. }
  789. // convert into point in constraint space:
  790. // note: twist is x-axis, swing 1 and 2 are along the z and y axes respectively
  791. btVector3 vSwingAxis(0, xEllipse, -yEllipse);
  792. btQuaternion qSwing(vSwingAxis, swingLimit);
  793. btVector3 vPointInConstraintSpace(fLength,0,0);
  794. return quatRotate(qSwing, vPointInConstraintSpace);
  795. }
  796. // given a twist rotation in constraint space, (pre: cone must already be removed)
  797. // this method computes its corresponding angle and axis.
  798. void btConeTwistConstraint::computeTwistLimitInfo(const btQuaternion& qTwist,
  799. btScalar& twistAngle, // out
  800. btVector3& vTwistAxis) // out
  801. {
  802. btQuaternion qMinTwist = qTwist;
  803. twistAngle = qTwist.getAngle();
  804. if (twistAngle > SIMD_PI) // long way around. flip quat and recalculate.
  805. {
  806. qMinTwist = -(qTwist);
  807. twistAngle = qMinTwist.getAngle();
  808. }
  809. if (twistAngle < 0)
  810. {
  811. // this should never happen
  812. #if 0
  813. btAssert(0);
  814. #endif
  815. }
  816. vTwistAxis = btVector3(qMinTwist.x(), qMinTwist.y(), qMinTwist.z());
  817. if (twistAngle > SIMD_EPSILON)
  818. vTwistAxis.normalize();
  819. }
  820. void btConeTwistConstraint::adjustSwingAxisToUseEllipseNormal(btVector3& vSwingAxis) const
  821. {
  822. // the swing axis is computed as the "twist-free" cone rotation,
  823. // but the cone limit is not circular, but elliptical (if swingspan1 != swingspan2).
  824. // so, if we're outside the limits, the closest way back inside the cone isn't
  825. // along the vector back to the center. better (and more stable) to use the ellipse normal.
  826. // convert swing axis to direction from center to surface of ellipse
  827. // (ie. rotate 2D vector by PI/2)
  828. btScalar y = -vSwingAxis.z();
  829. btScalar z = vSwingAxis.y();
  830. // do the math...
  831. if (fabs(z) > SIMD_EPSILON) // avoid division by 0. and we don't need an update if z == 0.
  832. {
  833. // compute gradient/normal of ellipse surface at current "point"
  834. btScalar grad = y/z;
  835. grad *= m_swingSpan2 / m_swingSpan1;
  836. // adjust y/z to represent normal at point (instead of vector to point)
  837. if (y > 0)
  838. y = fabs(grad * z);
  839. else
  840. y = -fabs(grad * z);
  841. // convert ellipse direction back to swing axis
  842. vSwingAxis.setZ(-y);
  843. vSwingAxis.setY( z);
  844. vSwingAxis.normalize();
  845. }
  846. }
  847. void btConeTwistConstraint::setMotorTarget(const btQuaternion &q)
  848. {
  849. btTransform trACur = m_rbA.getCenterOfMassTransform();
  850. btTransform trBCur = m_rbB.getCenterOfMassTransform();
  851. // btTransform trABCur = trBCur.inverse() * trACur;
  852. // btQuaternion qABCur = trABCur.getRotation();
  853. // btTransform trConstraintCur = (trBCur * m_rbBFrame).inverse() * (trACur * m_rbAFrame);
  854. //btQuaternion qConstraintCur = trConstraintCur.getRotation();
  855. btQuaternion qConstraint = m_rbBFrame.getRotation().inverse() * q * m_rbAFrame.getRotation();
  856. setMotorTargetInConstraintSpace(qConstraint);
  857. }
  858. void btConeTwistConstraint::setMotorTargetInConstraintSpace(const btQuaternion &q)
  859. {
  860. m_qTarget = q;
  861. // clamp motor target to within limits
  862. {
  863. btScalar softness = 1.f;//m_limitSoftness;
  864. // split into twist and cone
  865. btVector3 vTwisted = quatRotate(m_qTarget, vTwist);
  866. btQuaternion qTargetCone = shortestArcQuat(vTwist, vTwisted); qTargetCone.normalize();
  867. btQuaternion qTargetTwist = qTargetCone.inverse() * m_qTarget; qTargetTwist.normalize();
  868. // clamp cone
  869. if (m_swingSpan1 >= btScalar(0.05f) && m_swingSpan2 >= btScalar(0.05f))
  870. {
  871. btScalar swingAngle, swingLimit; btVector3 swingAxis;
  872. computeConeLimitInfo(qTargetCone, swingAngle, swingAxis, swingLimit);
  873. if (fabs(swingAngle) > SIMD_EPSILON)
  874. {
  875. if (swingAngle > swingLimit*softness)
  876. swingAngle = swingLimit*softness;
  877. else if (swingAngle < -swingLimit*softness)
  878. swingAngle = -swingLimit*softness;
  879. qTargetCone = btQuaternion(swingAxis, swingAngle);
  880. }
  881. }
  882. // clamp twist
  883. if (m_twistSpan >= btScalar(0.05f))
  884. {
  885. btScalar twistAngle; btVector3 twistAxis;
  886. computeTwistLimitInfo(qTargetTwist, twistAngle, twistAxis);
  887. if (fabs(twistAngle) > SIMD_EPSILON)
  888. {
  889. // eddy todo: limitSoftness used here???
  890. if (twistAngle > m_twistSpan*softness)
  891. twistAngle = m_twistSpan*softness;
  892. else if (twistAngle < -m_twistSpan*softness)
  893. twistAngle = -m_twistSpan*softness;
  894. qTargetTwist = btQuaternion(twistAxis, twistAngle);
  895. }
  896. }
  897. m_qTarget = qTargetCone * qTargetTwist;
  898. }
  899. }
  900. ///override the default global value of a parameter (such as ERP or CFM), optionally provide the axis (0..5).
  901. ///If no axis is provided, it uses the default axis for this constraint.
  902. void btConeTwistConstraint::setParam(int num, btScalar value, int axis)
  903. {
  904. switch(num)
  905. {
  906. case BT_CONSTRAINT_ERP :
  907. case BT_CONSTRAINT_STOP_ERP :
  908. if((axis >= 0) && (axis < 3))
  909. {
  910. m_linERP = value;
  911. m_flags |= BT_CONETWIST_FLAGS_LIN_ERP;
  912. }
  913. else
  914. {
  915. m_biasFactor = value;
  916. }
  917. break;
  918. case BT_CONSTRAINT_CFM :
  919. case BT_CONSTRAINT_STOP_CFM :
  920. if((axis >= 0) && (axis < 3))
  921. {
  922. m_linCFM = value;
  923. m_flags |= BT_CONETWIST_FLAGS_LIN_CFM;
  924. }
  925. else
  926. {
  927. m_angCFM = value;
  928. m_flags |= BT_CONETWIST_FLAGS_ANG_CFM;
  929. }
  930. break;
  931. default:
  932. btAssertConstrParams(0);
  933. break;
  934. }
  935. }
  936. ///return the local value of parameter
  937. btScalar btConeTwistConstraint::getParam(int num, int axis) const
  938. {
  939. btScalar retVal = 0;
  940. switch(num)
  941. {
  942. case BT_CONSTRAINT_ERP :
  943. case BT_CONSTRAINT_STOP_ERP :
  944. if((axis >= 0) && (axis < 3))
  945. {
  946. btAssertConstrParams(m_flags & BT_CONETWIST_FLAGS_LIN_ERP);
  947. retVal = m_linERP;
  948. }
  949. else if((axis >= 3) && (axis < 6))
  950. {
  951. retVal = m_biasFactor;
  952. }
  953. else
  954. {
  955. btAssertConstrParams(0);
  956. }
  957. break;
  958. case BT_CONSTRAINT_CFM :
  959. case BT_CONSTRAINT_STOP_CFM :
  960. if((axis >= 0) && (axis < 3))
  961. {
  962. btAssertConstrParams(m_flags & BT_CONETWIST_FLAGS_LIN_CFM);
  963. retVal = m_linCFM;
  964. }
  965. else if((axis >= 3) && (axis < 6))
  966. {
  967. btAssertConstrParams(m_flags & BT_CONETWIST_FLAGS_ANG_CFM);
  968. retVal = m_angCFM;
  969. }
  970. else
  971. {
  972. btAssertConstrParams(0);
  973. }
  974. break;
  975. default :
  976. btAssertConstrParams(0);
  977. }
  978. return retVal;
  979. }
  980. void btConeTwistConstraint::setFrames(const btTransform & frameA, const btTransform & frameB)
  981. {
  982. m_rbAFrame = frameA;
  983. m_rbBFrame = frameB;
  984. buildJacobian();
  985. //calculateTransforms();
  986. }