playerControllerComponent.h 6.6 KB

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