playerControllerComponent.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 PLAYER_CONTORLLER_COMPONENT_H
  23. #define PLAYER_CONTORLLER_COMPONENT_H
  24. #ifndef PHYSICS_COMPONENT_H
  25. #include "T3D/components/physics/physicsComponent.h"
  26. #endif
  27. #ifndef __RESOURCE_H__
  28. #include "core/resource.h"
  29. #endif
  30. #ifndef _TSSHAPE_H_
  31. #include "ts/tsShape.h"
  32. #endif
  33. #ifndef _SCENERENDERSTATE_H_
  34. #include "scene/sceneRenderState.h"
  35. #endif
  36. #ifndef _MBOX_H_
  37. #include "math/mBox.h"
  38. #endif
  39. #ifndef ENTITY_H
  40. #include "T3D/entity.h"
  41. #endif
  42. #ifndef _CONVEX_H_
  43. #include "collision/convex.h"
  44. #endif
  45. #ifndef _BOXCONVEX_H_
  46. #include "collision/boxConvex.h"
  47. #endif
  48. #ifndef _T3D_PHYSICSCOMMON_H_
  49. #include "T3D/physics/physicsCommon.h"
  50. #endif
  51. #ifndef _T3D_PHYSICS_PHYSICSWORLD_H_
  52. #include "T3D/physics/physicsWorld.h"
  53. #endif
  54. #ifndef COLLISION_COMPONENT_H
  55. #include "T3D/components/collision/collisionComponent.h"
  56. #endif
  57. class SceneRenderState;
  58. class PhysicsWorld;
  59. class PhysicsPlayer;
  60. class SimplePhysicsBehaviorInstance;
  61. //////////////////////////////////////////////////////////////////////////
  62. ///
  63. ///
  64. //////////////////////////////////////////////////////////////////////////
  65. class PlayerControllerComponent : public PhysicsComponent
  66. {
  67. typedef Component Parent;
  68. enum MaskBits {
  69. VelocityMask = Parent::NextFreeMask << 0,
  70. PositionMask = Parent::NextFreeMask << 1,
  71. NextFreeMask = Parent::NextFreeMask << 2
  72. };
  73. struct StateDelta
  74. {
  75. Move move; ///< Last move from server
  76. F32 dt; ///< Last interpolation time
  77. // Interpolation data
  78. Point3F pos;
  79. Point3F posVec;
  80. QuatF rot[2];
  81. // Warp data
  82. S32 warpTicks; ///< Number of ticks to warp
  83. S32 warpCount; ///< Current pos in warp
  84. Point3F warpOffset;
  85. QuatF warpRot[2];
  86. };
  87. StateDelta mDelta;
  88. PhysicsPlayer *mPhysicsRep;
  89. PhysicsWorld *mPhysicsWorld;
  90. CollisionComponent* mOwnerCollisionComp;
  91. struct ContactInfo
  92. {
  93. bool contacted, jump, run;
  94. SceneObject *contactObject;
  95. VectorF contactNormal;
  96. F32 contactTime;
  97. void clear()
  98. {
  99. contacted = jump = run = false;
  100. contactObject = nullptr;
  101. contactNormal.set(0,0,0);
  102. contactTime = 0;
  103. }
  104. ContactInfo() { clear(); }
  105. } mContactInfo;
  106. protected:
  107. F32 mDrag;
  108. F32 mBuoyancy;
  109. F32 mFriction;
  110. F32 mElasticity;
  111. F32 mMaxVelocity;
  112. bool mSticky;
  113. bool mFalling;
  114. bool mSwimming;
  115. bool mInWater;
  116. S32 mContactTimer; ///< Ticks since last contact
  117. U32 mIntegrationCount;
  118. Point3F mJumpSurfaceNormal; ///< Normal of the surface the player last jumped on
  119. F32 maxStepHeight; ///< Maximum height the player can step up
  120. F32 moveSurfaceAngle; ///< Maximum angle from vertical in degrees the player can run up
  121. F32 contactSurfaceAngle; ///< Maximum angle from vertical in degrees we consider having real 'contact'
  122. F32 horizMaxSpeed; ///< Max speed attainable in the horizontal
  123. F32 horizMaxAccel;
  124. F32 horizResistSpeed; ///< Speed at which resistance will take place
  125. F32 horizResistFactor; ///< Factor of resistance once horizResistSpeed has been reached
  126. F32 upMaxSpeed; ///< Max vertical speed attainable
  127. F32 upMaxAccel;
  128. F32 upResistSpeed; ///< Speed at which resistance will take place
  129. F32 upResistFactor; ///< Factor of resistance once upResistSpeed has been reached
  130. F32 fallingSpeedThreshold; ///< Downward speed at which we consider the player falling
  131. // Air control
  132. F32 airControl;
  133. Point3F mInputVelocity;
  134. bool mUseDirectMoveInput;
  135. public:
  136. PlayerControllerComponent();
  137. virtual ~PlayerControllerComponent();
  138. DECLARE_CONOBJECT(PlayerControllerComponent);
  139. virtual bool onAdd();
  140. virtual void onRemove();
  141. static void initPersistFields();
  142. virtual void onComponentAdd();
  143. virtual void componentAddedToOwner(Component *comp);
  144. virtual void componentRemovedFromOwner(Component *comp);
  145. virtual void ownerTransformSet(MatrixF *mat);
  146. virtual U32 packUpdate(NetConnection *con, U32 mask, BitStream *stream);
  147. virtual void unpackUpdate(NetConnection *con, BitStream *stream);
  148. void updatePhysics(PhysicsCollision *collision = NULL);
  149. virtual void processTick();
  150. virtual void interpolateTick(F32 dt);
  151. virtual void updatePos(const F32 dt);
  152. void updateMove();
  153. virtual VectorF getVelocity() { return mVelocity; }
  154. virtual void setVelocity(const VectorF& vel);
  155. virtual void setTransform(const MatrixF& mat);
  156. void findContact(bool *run, bool *jump, VectorF *contactNormal);
  157. Point3F getContactNormal() { return mContactInfo.contactNormal; }
  158. SceneObject* getContactObject() { return mContactInfo.contactObject; }
  159. bool isContacted() { return mContactInfo.contacted; }
  160. //
  161. void applyImpulse(const Point3F &pos, const VectorF &vec);
  162. //This is a weird artifact of the PhysicsReps. We want the collision component to be privvy to any events that happen
  163. //so when the physics components do a findContact test during their update, they'll have a signal collision components
  164. //can be listening to to update themselves with that info
  165. Signal< void(SceneObject*) > onContactSignal;
  166. //
  167. DECLARE_CALLBACK(void, updateMove, (PlayerControllerComponent* obj));
  168. };
  169. #endif // _COMPONENT_H_