rigid.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "T3D/rigid.h"
  23. #include "console/console.h"
  24. //----------------------------------------------------------------------------
  25. Rigid::Rigid()
  26. {
  27. force.set(0.0f,0.0f,0.0f);
  28. torque.set(0.0f,0.0f,0.0f);
  29. linVelocity.set(0.0f,0.0f,0.0f);
  30. linPosition.set(0.0f,0.0f,0.0f);
  31. linMomentum.set(0.0f,0.0f,0.0f);
  32. angVelocity.set(0.0f,0.0f,0.0f);
  33. angMomentum.set(0.0f,0.0f,0.0f);
  34. angPosition.identity();
  35. invWorldInertia.identity();
  36. centerOfMass.set(0.0f,0.0f,0.0f);
  37. worldCenterOfMass = linPosition;
  38. mass = oneOverMass = 1.0f;
  39. invObjectInertia.identity();
  40. restitution = 0.3f;
  41. friction = 0.5f;
  42. atRest = false;
  43. }
  44. void Rigid::clearForces()
  45. {
  46. force.set(0.0f,0.0f,0.0f);
  47. torque.set(0.0f,0.0f,0.0f);
  48. }
  49. //-----------------------------------------------------------------------------
  50. void Rigid::integrate(F32 delta)
  51. {
  52. // Update Angular position
  53. F32 angle = angVelocity.len();
  54. if (angle != 0.0f) {
  55. QuatF dq;
  56. F32 sinHalfAngle;
  57. mSinCos(angle * delta * -0.5f, sinHalfAngle, dq.w);
  58. sinHalfAngle *= 1.0f / angle;
  59. dq.x = angVelocity.x * sinHalfAngle;
  60. dq.y = angVelocity.y * sinHalfAngle;
  61. dq.z = angVelocity.z * sinHalfAngle;
  62. QuatF tmp = angPosition;
  63. angPosition.mul(tmp, dq);
  64. angPosition.normalize();
  65. // Rotate the position around the center of mass
  66. Point3F lp = linPosition - worldCenterOfMass;
  67. dq.mulP(lp,&linPosition);
  68. linPosition += worldCenterOfMass;
  69. }
  70. // Update angular momentum
  71. angMomentum = angMomentum + torque * delta;
  72. // Update linear position, momentum
  73. linPosition = linPosition + linVelocity * delta;
  74. linMomentum = linMomentum + force * delta;
  75. linVelocity = linMomentum * oneOverMass;
  76. // Update dependent state variables
  77. updateInertialTensor();
  78. updateVelocity();
  79. updateCenterOfMass();
  80. }
  81. void Rigid::updateVelocity()
  82. {
  83. linVelocity.x = linMomentum.x * oneOverMass;
  84. linVelocity.y = linMomentum.y * oneOverMass;
  85. linVelocity.z = linMomentum.z * oneOverMass;
  86. invWorldInertia.mulV(angMomentum,&angVelocity);
  87. }
  88. void Rigid::updateInertialTensor()
  89. {
  90. MatrixF iv,qmat;
  91. angPosition.setMatrix(&qmat);
  92. iv.mul(qmat,invObjectInertia);
  93. qmat.transpose();
  94. invWorldInertia.mul(iv,qmat);
  95. }
  96. void Rigid::updateCenterOfMass()
  97. {
  98. // Move the center of mass into world space
  99. angPosition.mulP(centerOfMass,&worldCenterOfMass);
  100. worldCenterOfMass += linPosition;
  101. }
  102. void Rigid::applyImpulse(const Point3F &r, const Point3F &impulse)
  103. {
  104. atRest = false;
  105. // Linear momentum and velocity
  106. linMomentum += impulse;
  107. linVelocity.x = linMomentum.x * oneOverMass;
  108. linVelocity.y = linMomentum.y * oneOverMass;
  109. linVelocity.z = linMomentum.z * oneOverMass;
  110. // Rotational momentum and velocity
  111. Point3F tv;
  112. mCross(r,impulse,&tv);
  113. angMomentum += tv;
  114. invWorldInertia.mulV(angMomentum, &angVelocity);
  115. }
  116. //-----------------------------------------------------------------------------
  117. /** Resolve collision with another rigid body
  118. Computes & applies the collision impulses needed to keep the bodies
  119. from interpenetrating.
  120. tg: This function was commented out... I uncommented it, but haven't
  121. double checked the math.
  122. */
  123. bool Rigid::resolveCollision(const Point3F& p, const Point3F &normal, Rigid* rigid)
  124. {
  125. atRest = false;
  126. Point3F v1,v2,r1,r2;
  127. getOriginVector(p,&r1);
  128. getVelocity(r1,&v1);
  129. rigid->getOriginVector(p,&r2);
  130. rigid->getVelocity(r2,&v2);
  131. // Make sure they are converging
  132. F32 nv = mDot(v1,normal);
  133. nv -= mDot(v2,normal);
  134. if (nv > 0.0f)
  135. return false;
  136. // Compute impulse
  137. F32 d, n = -nv * (2.0f + restitution * rigid->restitution);
  138. Point3F a1,b1,c1;
  139. mCross(r1,normal,&a1);
  140. invWorldInertia.mulV(a1,&b1);
  141. mCross(b1,r1,&c1);
  142. Point3F a2,b2,c2;
  143. mCross(r2,normal,&a2);
  144. rigid->invWorldInertia.mulV(a2,&b2);
  145. mCross(b2,r2,&c2);
  146. Point3F c3 = c1 + c2;
  147. d = oneOverMass + rigid->oneOverMass + mDot(c3,normal);
  148. Point3F impulse = normal * (n / d);
  149. applyImpulse(r1,impulse);
  150. impulse.neg();
  151. rigid->applyImpulse(r2, impulse);
  152. return true;
  153. }
  154. //-----------------------------------------------------------------------------
  155. /** Resolve collision with an immovable object
  156. Computes & applies the collision impulse needed to keep the body
  157. from penetrating the given surface.
  158. */
  159. bool Rigid::resolveCollision(const Point3F& p, const Point3F &normal)
  160. {
  161. atRest = false;
  162. Point3F v,r;
  163. getOriginVector(p,&r);
  164. getVelocity(r,&v);
  165. F32 n = -mDot(v,normal);
  166. if (n >= 0.0f) {
  167. // Collision impulse, straight forward force stuff.
  168. F32 d = getZeroImpulse(r,normal);
  169. F32 j = n * (1.0f + restitution) * d;
  170. Point3F impulse = normal * j;
  171. // Friction impulse, calculated as a function of the
  172. // amount of force it would take to stop the motion
  173. // perpendicular to the normal.
  174. Point3F uv = v + (normal * n);
  175. F32 ul = uv.len();
  176. if (ul) {
  177. uv /= -ul;
  178. F32 u = ul * getZeroImpulse(r,uv);
  179. j *= friction;
  180. if (u > j)
  181. u = j;
  182. impulse += uv * u;
  183. }
  184. //
  185. applyImpulse(r,impulse);
  186. }
  187. return true;
  188. }
  189. //-----------------------------------------------------------------------------
  190. /** Calculate the inertia along the given vector
  191. This function can be used to calculate the amount of force needed to
  192. affect a change in velocity along the specified normal applied at
  193. the given point.
  194. */
  195. F32 Rigid::getZeroImpulse(const Point3F& r,const Point3F& normal)
  196. {
  197. Point3F a,b,c;
  198. mCross(r,normal,&a);
  199. invWorldInertia.mulV(a,&b);
  200. mCross(b,r,&c);
  201. return 1 / (oneOverMass + mDot(c,normal));
  202. }
  203. F32 Rigid::getKineticEnergy()
  204. {
  205. Point3F w;
  206. QuatF qmat = angPosition;
  207. qmat.inverse();
  208. qmat.mulP(angVelocity,&w);
  209. const F32* f = invObjectInertia;
  210. return 0.5f * ((mass * mDot(linVelocity,linVelocity)) +
  211. w.x * w.x / f[0] +
  212. w.y * w.y / f[5] +
  213. w.z * w.z / f[10]);
  214. }
  215. void Rigid::getOriginVector(const Point3F &p,Point3F* r)
  216. {
  217. *r = p - worldCenterOfMass;
  218. }
  219. void Rigid::setCenterOfMass(const Point3F &newCenter)
  220. {
  221. // Sets the center of mass relative to the origin.
  222. centerOfMass = newCenter;
  223. // Update world center of mass
  224. angPosition.mulP(centerOfMass,&worldCenterOfMass);
  225. worldCenterOfMass += linPosition;
  226. }
  227. void Rigid::translateCenterOfMass(const Point3F &oldPos,const Point3F &newPos)
  228. {
  229. // I + mass * (crossmatrix(centerOfMass)^2 - crossmatrix(newCenter)^2)
  230. MatrixF oldx,newx;
  231. oldx.setCrossProduct(oldPos);
  232. newx.setCrossProduct(newPos);
  233. for (S32 row = 0; row < 3; row++)
  234. for (S32 col = 0; col < 3; col++) {
  235. F32 n = newx(row,col), o = oldx(row,col);
  236. objectInertia(row,col) += mass * ((o * o) - (n * n));
  237. }
  238. // Make sure the matrix is symetrical
  239. objectInertia(1,0) = objectInertia(0,1);
  240. objectInertia(2,0) = objectInertia(0,2);
  241. objectInertia(2,1) = objectInertia(1,2);
  242. }
  243. void Rigid::getVelocity(const Point3F& r, Point3F* v)
  244. {
  245. mCross(angVelocity, r, v);
  246. *v += linVelocity;
  247. }
  248. void Rigid::getTransform(MatrixF* mat)
  249. {
  250. angPosition.setMatrix(mat);
  251. mat->setColumn(3,linPosition);
  252. }
  253. void Rigid::setTransform(const MatrixF& mat)
  254. {
  255. angPosition.set(mat);
  256. mat.getColumn(3,&linPosition);
  257. // Update center of mass
  258. angPosition.mulP(centerOfMass,&worldCenterOfMass);
  259. worldCenterOfMass += linPosition;
  260. }
  261. //----------------------------------------------------------------------------
  262. /** Set the rigid body moment of inertia
  263. The moment is calculated as a box with the given dimensions.
  264. */
  265. void Rigid::setObjectInertia(const Point3F& r)
  266. {
  267. // Rotational moment of inertia of a box
  268. F32 ot = mass / 12.0f;
  269. F32 a = r.x * r.x;
  270. F32 b = r.y * r.y;
  271. F32 c = r.z * r.z;
  272. objectInertia.identity();
  273. F32* f = objectInertia;
  274. f[0] = ot * (b + c);
  275. f[5] = ot * (c + a);
  276. f[10] = ot * (a + b);
  277. invertObjectInertia();
  278. updateInertialTensor();
  279. }
  280. //----------------------------------------------------------------------------
  281. /** Set the rigid body moment of inertia
  282. The moment is calculated as a unit sphere.
  283. */
  284. void Rigid::setObjectInertia()
  285. {
  286. objectInertia.identity();
  287. F32 radius = 1.0f;
  288. F32* f = objectInertia;
  289. f[0] = f[5] = f[10] = (0.4f * mass * radius * radius);
  290. invertObjectInertia();
  291. updateInertialTensor();
  292. }
  293. void Rigid::invertObjectInertia()
  294. {
  295. invObjectInertia = objectInertia;
  296. invObjectInertia.fullInverse();
  297. }
  298. //----------------------------------------------------------------------------
  299. bool Rigid::checkRestCondition()
  300. {
  301. // F32 k = getKineticEnergy(mWorldToObj);
  302. // F32 G = -force.z * oneOverMass * 0.032;
  303. // F32 Kg = 0.5 * mRigid.mass * G * G;
  304. // if (k < Kg * restTol)
  305. // mRigid.setAtRest();
  306. return atRest;
  307. }
  308. void Rigid::setAtRest()
  309. {
  310. atRest = true;
  311. linVelocity.set(0.0f,0.0f,0.0f);
  312. linMomentum.set(0.0f,0.0f,0.0f);
  313. angVelocity.set(0.0f,0.0f,0.0f);
  314. angMomentum.set(0.0f,0.0f,0.0f);
  315. }