rigid.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #ifndef _RIGID_H_
  23. #define _RIGID_H_
  24. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. #ifndef _MPOINT3_H_
  28. #include "math/mPoint3.h"
  29. #endif
  30. #ifndef _MMATRIX_H_
  31. #include "math/mMatrix.h"
  32. #endif
  33. #ifndef _MQUAT_H_
  34. #include "math/mQuat.h"
  35. #endif
  36. //----------------------------------------------------------------------------
  37. class Rigid
  38. {
  39. public:
  40. MatrixF objectInertia; ///< Moment of inertia
  41. MatrixF invObjectInertia; ///< Inverse moment of inertia
  42. MatrixF invWorldInertia; ///< Inverse moment of inertia in world space
  43. Point3F force;
  44. Point3F torque;
  45. Point3F linVelocity; ///< Linear velocity
  46. Point3F linPosition; ///< Current position
  47. Point3F linMomentum; ///< Linear momentum
  48. Point3F angVelocity; ///< Angular velocity
  49. QuatF angPosition; ///< Current rotation
  50. Point3F angMomentum; ///< Angular momentum
  51. Point3F centerOfMass; ///< Center of mass in object space
  52. Point3F worldCenterOfMass; ///< CofM in world space
  53. F32 mass; ///< Rigid body mass
  54. F32 oneOverMass; ///< 1 / mass
  55. F32 restitution; ///< Collision restitution
  56. F32 friction; ///< Friction coefficient
  57. // sleep threshold parameters
  58. F32 sleepLinearThreshold; ///< M/S ^ 2
  59. F32 sleepAngThreshold; ///< R/S ^ 2
  60. F32 sleepTimeThreshold; ///< Seconds
  61. F32 sleepTimer;
  62. bool atRest;
  63. private:
  64. void translateCenterOfMass(const Point3F &oldPos,const Point3F &newPos);
  65. void trySleep(F32 dt);
  66. public:
  67. //
  68. Rigid();
  69. void clearForces();
  70. void integrate(F32 delta);
  71. void updateInertialTensor();
  72. void updateVelocity();
  73. void updateCenterOfMass();
  74. void applyImpulse(const Point3F &v,const Point3F &impulse);
  75. bool resolveCollision(const Point3F& p,const Point3F &normal,Rigid*);
  76. bool resolveCollision(const Point3F& p,const Point3F &normal);
  77. F32 getZeroImpulse(const Point3F& r,const Point3F& normal);
  78. F32 getKineticEnergy();
  79. void getOriginVector(const Point3F &r,Point3F* v);
  80. void setCenterOfMass(const Point3F &v);
  81. void getVelocity(const Point3F &p,Point3F* r);
  82. void getTransform(MatrixF* mat);
  83. void setTransform(const MatrixF& mat);
  84. void setObjectInertia(const Point3F& r);
  85. void setObjectInertia();
  86. void invertObjectInertia();
  87. bool checkRestCondition();
  88. void setAtRest();
  89. //
  90. void setSleepThresholds(F32 linVel2, F32 angVel2, F32 timeToSleep);
  91. void wake();
  92. TORQUE_FORCEINLINE void updateAngularVelocity() { invWorldInertia.mulV(angMomentum, &angVelocity); }
  93. };
  94. #endif