Torque3D Documentation / _generateds / playerControllerComponent.h

playerControllerComponent.h

Engine/source/T3D/components/physics/playerControllerComponent.h

More...

Classes:

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef PLAYER_CONTORLLER_COMPONENT_H
 25#define PLAYER_CONTORLLER_COMPONENT_H
 26
 27#ifndef PHYSICSBEHAVIOR_H
 28#include "T3D/components/physics/physicsBehavior.h"
 29#endif
 30#ifndef __RESOURCE_H__
 31#include "core/resource.h"
 32#endif
 33#ifndef _TSSHAPE_H_
 34#include "ts/tsShape.h"
 35#endif
 36#ifndef _SCENERENDERSTATE_H_
 37#include "scene/sceneRenderState.h"
 38#endif
 39#ifndef _MBOX_H_
 40#include "math/mBox.h"
 41#endif
 42#ifndef ENTITY_H
 43#include "T3D/entity.h"
 44#endif
 45#ifndef _CONVEX_H_
 46#include "collision/convex.h"
 47#endif
 48#ifndef _BOXCONVEX_H_
 49#include "collision/boxConvex.h"
 50#endif
 51#ifndef _T3D_PHYSICSCOMMON_H_
 52#include "T3D/physics/physicsCommon.h"
 53#endif
 54#ifndef _T3D_PHYSICS_PHYSICSWORLD_H_
 55#include "T3D/physics/physicsWorld.h"
 56#endif
 57#ifndef PHYSICS_COMPONENT_INTERFACE_H
 58#include "T3D/components/physics/physicsComponentInterface.h"
 59#endif
 60#ifndef COLLISION_INTERFACES_H
 61#include "T3D/components/collision/collisionInterfaces.h"
 62#endif
 63
 64class SceneRenderState;
 65class PhysicsWorld;
 66class PhysicsPlayer;
 67class SimplePhysicsBehaviorInstance;
 68class CollisionInterface;
 69
 70//////////////////////////////////////////////////////////////////////////
 71/// 
 72/// 
 73//////////////////////////////////////////////////////////////////////////
 74class PlayerControllerComponent : public Component,
 75   public PhysicsComponentInterface
 76{
 77   typedef Component Parent;
 78
 79   enum MaskBits {
 80      VelocityMask = Parent::NextFreeMask << 0,
 81      PositionMask = Parent::NextFreeMask << 1,
 82      NextFreeMask = Parent::NextFreeMask << 2
 83   };
 84
 85   struct StateDelta
 86   {
 87      Move move;                    ///< Last move from server
 88      F32 dt;                       ///< Last interpolation time
 89      // Interpolation data
 90      Point3F pos;
 91      Point3F posVec;
 92      QuatF rot[2];
 93      // Warp data
 94      S32 warpTicks;                ///< Number of ticks to warp
 95      S32 warpCount;                ///< Current pos in warp
 96      Point3F warpOffset;
 97      QuatF warpRot[2];
 98   };
 99
100   StateDelta mDelta;
101
102   PhysicsPlayer *mPhysicsRep;
103   PhysicsWorld  *mPhysicsWorld;
104
105   CollisionInterface* mOwnerCollisionInterface;
106
107   struct ContactInfo
108   {
109      bool contacted, jump, run;
110      SceneObject *contactObject;
111      VectorF  contactNormal;
112      F32 contactTime;
113
114      void clear()
115      {
116         contacted = jump = run = false;
117         contactObject = NULL;
118         contactNormal.set(1, 1, 1);
119      }
120
121      ContactInfo() { clear(); }
122
123   } mContactInfo;
124
125protected:
126   F32 mDrag;
127   F32 mBuoyancy;
128   F32 mFriction;
129   F32 mElasticity;
130   F32 mMaxVelocity;
131   bool mSticky;
132
133   bool mFalling;
134   bool mSwimming;
135   bool mInWater;
136
137   S32 mContactTimer;               ///< Ticks since last contact
138
139   U32 mIntegrationCount;
140
141   Point3F mJumpSurfaceNormal;      ///< Normal of the surface the player last jumped on
142
143   F32 maxStepHeight;         ///< Maximum height the player can step up
144   F32 moveSurfaceAngle;      ///< Maximum angle from vertical in degrees the player can run up
145   F32 contactSurfaceAngle;   ///< Maximum angle from vertical in degrees we consider having real 'contact'
146
147   F32 horizMaxSpeed;         ///< Max speed attainable in the horizontal
148   F32 horizMaxAccel;
149   F32 horizResistSpeed;      ///< Speed at which resistance will take place
150   F32 horizResistFactor;     ///< Factor of resistance once horizResistSpeed has been reached
151
152   F32 upMaxSpeed;            ///< Max vertical speed attainable
153   F32 upMaxAccel;
154   F32 upResistSpeed;         ///< Speed at which resistance will take place
155   F32 upResistFactor;        ///< Factor of resistance once upResistSpeed has been reached
156
157   F32 fallingSpeedThreshold; ///< Downward speed at which we consider the player falling
158
159   // Air control
160   F32 airControl;
161
162   Point3F mInputVelocity;
163
164   bool mUseDirectMoveInput;
165
166public:
167   PlayerControllerComponent();
168   virtual ~PlayerControllerComponent();
169   DECLARE_CONOBJECT(PlayerControllerComponent);
170
171   virtual bool onAdd();
172   virtual void onRemove();
173   static void initPersistFields();
174
175   virtual void onComponentAdd();
176
177   virtual void componentAddedToOwner(Component *comp);
178   virtual void componentRemovedFromOwner(Component *comp);
179
180   virtual void ownerTransformSet(MatrixF *mat);
181
182   virtual U32 packUpdate(NetConnection *con, U32 mask, BitStream *stream);
183   virtual void unpackUpdate(NetConnection *con, BitStream *stream);
184
185   void updatePhysics(PhysicsCollision *collision = NULL);
186
187   virtual void processTick();
188   virtual void interpolateTick(F32 dt);
189   virtual void updatePos(const F32 dt);
190   void updateMove();
191
192   virtual VectorF getVelocity() { return mVelocity; }
193   virtual void setVelocity(const VectorF& vel);
194   virtual void setTransform(const MatrixF& mat);
195
196   void findContact(bool *run, bool *jump, VectorF *contactNormal);
197   Point3F getContactNormal() { return mContactInfo.contactNormal; }
198   SceneObject* getContactObject() { return mContactInfo.contactObject; }
199   bool isContacted() { return mContactInfo.contacted; }
200
201   //
202   void applyImpulse(const Point3F &pos, const VectorF &vec);
203
204   //This is a weird artifact of the PhysicsReps. We want the collision component to be privvy to any events that happen
205   //so when the physics components do a findContact test during their update, they'll have a signal collision components
206   //can be listening to to update themselves with that info
207   Signal< void(SceneObject*) > onContactSignal;
208
209   //
210   DECLARE_CALLBACK(void, updateMove, (PlayerControllerComponent* obj));
211};
212
213#endif // _COMPONENT_H_
214