physicsComponent.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //-----------------------------------------------------------------------------
  2. // Torque Game Engine
  3. // Copyright (C) GarageGames.com, Inc.
  4. //-----------------------------------------------------------------------------
  5. #pragma once
  6. #ifndef PHYSICS_COMPONENT_H
  7. #define PHYSICS_COMPONENT_H
  8. #include "T3D/components/component.h"
  9. #ifndef __RESOURCE_H__
  10. #include "core/resource.h"
  11. #endif
  12. #ifndef _TSSHAPE_H_
  13. #include "ts/tsShape.h"
  14. #endif
  15. #ifndef _SCENERENDERSTATE_H_
  16. #include "scene/sceneRenderState.h"
  17. #endif
  18. #ifndef _MBOX_H_
  19. #include "math/mBox.h"
  20. #endif
  21. #ifndef _ENTITY_H_
  22. #include "T3D/entity.h"
  23. #endif
  24. #ifndef _CONVEX_H_
  25. #include "collision/convex.h"
  26. #endif
  27. #ifndef _BOXCONVEX_H_
  28. #include "collision/boxConvex.h"
  29. #endif
  30. #ifndef _RIGID_H_
  31. #include "T3D/rigid.h"
  32. #endif
  33. #ifndef _T3D_PHYSICS_PHYSICSBODY_H_
  34. #include "T3D/physics/physicsBody.h"
  35. #endif
  36. #ifndef _RENDER_COMPONENT_INTERFACE_H_
  37. #include "T3D/components/render/renderComponentInterface.h"
  38. #endif
  39. class TSShapeInstance;
  40. class SceneRenderState;
  41. class PhysicsBody;
  42. class PhysicsBehaviorInstance;
  43. //////////////////////////////////////////////////////////////////////////
  44. ///
  45. ///
  46. //////////////////////////////////////////////////////////////////////////
  47. class PhysicsComponent : public Component
  48. {
  49. typedef Component Parent;
  50. protected:
  51. bool mStatic;
  52. bool mAtRest;
  53. S32 mAtRestCounter;
  54. VectorF mGravity;
  55. VectorF mVelocity;
  56. F32 mDrag;
  57. F32 mMass;
  58. F32 mGravityMod;
  59. S32 csmAtRestTimer;
  60. F32 sAtRestVelocity; // Min speed after collisio
  61. PhysicsBody* mPhysicsRep;
  62. PhysicsWorld* mPhysicsWorld;
  63. Convex* mConvexList;
  64. public:
  65. enum MaskBits {
  66. PositionMask = Parent::NextFreeMask << 0,
  67. FreezeMask = Parent::NextFreeMask << 1,
  68. ForceMoveMask = Parent::NextFreeMask << 2,
  69. VelocityMask = Parent::NextFreeMask << 3,
  70. NextFreeMask = Parent::NextFreeMask << 4
  71. };
  72. struct StateDelta
  73. {
  74. Move move; ///< Last move from server
  75. F32 dt; ///< Last interpolation time
  76. // Interpolation data
  77. Point3F pos;
  78. Point3F posVec;
  79. QuatF rot[2];
  80. // Warp data
  81. S32 warpTicks; ///< Number of ticks to warp
  82. S32 warpCount; ///< Current pos in warp
  83. Point3F warpOffset;
  84. QuatF warpRot[2];
  85. };
  86. StateDelta mDelta;
  87. S32 mPredictionCount; ///< Number of ticks to predict
  88. ContainerQueryInfo mLastContainerInfo;
  89. public:
  90. PhysicsComponent();
  91. virtual ~PhysicsComponent();
  92. DECLARE_CONOBJECT(PhysicsComponent);
  93. static void initPersistFields();
  94. //Components
  95. virtual void onComponentAdd();
  96. virtual void onComponentRemove();
  97. //Setup
  98. void prepCollision();
  99. virtual void _updatePhysics();
  100. virtual void buildConvex(const Box3F& box, Convex* convex);
  101. //Update
  102. virtual void interpolateTick(F32 dt);
  103. virtual void updatePos(const F32 dt);
  104. virtual void updateForces();
  105. void updateContainer();
  106. //Physics Collision Conveinence Hooks
  107. virtual bool updateCollision(F32 dt, Rigid& ns, CollisionList &cList) { return false; }
  108. virtual bool resolveContacts(Rigid& ns, CollisionList& cList, F32 dt) { return false; }
  109. virtual bool resolveCollision(const Point3F& p, const Point3F &normal) { return false; }
  110. //Networking
  111. virtual U32 packUpdate(NetConnection *con, U32 mask, BitStream *stream);
  112. virtual void unpackUpdate(NetConnection *con, BitStream *stream);
  113. //Events
  114. virtual void updateVelocity(const F32 dt);
  115. virtual void setVelocity(const VectorF& vel);
  116. virtual void setTransform(const MatrixF& mat);
  117. virtual void setPosition(const Point3F& pos);
  118. void setRenderPosition(const Point3F& pos, F32 dt);
  119. virtual void applyImpulse(const Point3F&, const VectorF& vec);
  120. //Gets
  121. F32 getMass() { return mMass; }
  122. virtual PhysicsBody *getPhysicsRep();
  123. virtual Point3F getVelocity() { return mVelocity; }
  124. virtual void getOriginVector(const Point3F &p, Point3F* r);
  125. virtual void getVelocity(const Point3F& r, Point3F* v);
  126. virtual F32 getZeroImpulse(const Point3F& r, const Point3F& normal);
  127. };
  128. #endif // _COMPONENT_H_