physicsCommon.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 _T3D_PHYSICSCOMMON_H_
  23. #define _T3D_PHYSICSCOMMON_H_
  24. #ifndef _MPOINT3_H_
  25. #include "math/mPoint3.h"
  26. #endif
  27. #ifndef _MQUAT_H_
  28. #include "math/mQuat.h"
  29. #endif
  30. #ifndef _MMATRIX_H_
  31. #include "math/mMatrix.h"
  32. #endif
  33. #ifndef _REFBASE_H_
  34. #include "core/util/refBase.h"
  35. #endif
  36. /// Helper structure which defines the state of a single physics body.
  37. struct PhysicsState
  38. {
  39. /// Constructor.
  40. PhysicsState()
  41. : position( Point3F::Zero ),
  42. momentum( Point3F::Zero ),
  43. orientation( QuatF::Identity ),
  44. angularMomentum( Point3F::Zero ),
  45. sleeping( false ),
  46. linVelocity( Point3F::Zero ),
  47. angVelocity( Point3F::Zero )
  48. {
  49. }
  50. /// The primary physics state.
  51. // @{
  52. /// The position of the body.
  53. Point3F position;
  54. /// The momentum in kilogram meters per second.
  55. Point3F momentum;
  56. /// The orientation of the body.
  57. QuatF orientation;
  58. /// The angular momentum.
  59. Point3F angularMomentum;
  60. /// Is true if the shape is asleep.
  61. bool sleeping;
  62. // @}
  63. /// The secondary physics state derived from the primary state.
  64. /// @{
  65. /// The linear velocity derived from the momentum.
  66. Point3F linVelocity;
  67. ///
  68. Point3F angVelocity;
  69. /*
  70. Vector velocity; ///< velocity in meters per second (calculated from momentum).
  71. Quaternion spin; ///< quaternion rate of change in orientation.
  72. Vector angularVelocity; ///< angular velocity (calculated from angularMomentum).
  73. Matrix bodyToWorld; ///< body to world coordinates matrix.
  74. Matrix worldToBody; ///< world to body coordinates matrix.
  75. */
  76. /// @}
  77. /// Interpolates between two physics states leaving the
  78. /// result in this physics state.
  79. inline PhysicsState& interpolate( const PhysicsState &a, const PhysicsState &b, F32 t )
  80. {
  81. F32 inverseT = 1.0f - t;
  82. position = a.position*inverseT + b.position*t;
  83. momentum = a.momentum*inverseT + b.momentum*t;
  84. orientation.interpolate( a.orientation, b.orientation, t );
  85. angularMomentum = a.angularMomentum*inverseT + b.angularMomentum*t;
  86. // Recalculate the velocities
  87. //linVelocity =
  88. //angVelocity
  89. return *this;
  90. }
  91. /// Helper builds the transform from the state.
  92. inline MatrixF getTransform() const
  93. {
  94. MatrixF xfm;
  95. orientation.setMatrix( &xfm );
  96. xfm.setPosition( position );
  97. return xfm;
  98. }
  99. };
  100. /// The event type passed to the physics reset signal.
  101. /// @see PhysicsPlugin::getPhysicsResetSignal().
  102. enum PhysicsResetEvent
  103. {
  104. PhysicsResetEvent_Store,
  105. PhysicsResetEvent_Restore
  106. };
  107. /// The signal for system wide physics events.
  108. /// @see PhysicsPlugin
  109. typedef Signal<void(PhysicsResetEvent reset)> PhysicsResetSignal;
  110. class PhysicsCollision;
  111. /// A strong reference to a physics collision shape.
  112. typedef StrongRefPtr<PhysicsCollision> PhysicsCollisionRef;
  113. #endif // _T3D_PHYSICSCOMMON_H_