1 //-----------------------------------------------------------------------------
2 // Copyright (c) 2012 GarageGames, LLC
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:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
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
21 //-----------------------------------------------------------------------------
23 #ifndef PLAYER_CONTORLLER_COMPONENT_H
24 #define PLAYER_CONTORLLER_COMPONENT_H
26 #ifndef PHYSICSBEHAVIOR_H
27 #include "T3D/components/physics/physicsBehavior.h"
29 #ifndef __RESOURCE_H__
30 #include "core/resource.h"
33 #include "ts/tsShape.h"
35 #ifndef _SCENERENDERSTATE_H_
36 #include "scene/sceneRenderState.h"
39 #include "math/mBox.h"
42 #include "T3D/entity.h"
45 #include "collision/convex.h"
48 #include "collision/boxConvex.h"
50 #ifndef _T3D_PHYSICSCOMMON_H_
51 #include "T3D/physics/physicsCommon.h"
53 #ifndef _T3D_PHYSICS_PHYSICSWORLD_H_
54 #include "T3D/physics/physicsWorld.h"
56 #ifndef PHYSICS_COMPONENT_INTERFACE_H
57 #include "T3D/components/physics/physicsComponentInterface.h"
59 #ifndef COLLISION_INTERFACES_H
60 #include "T3D/components/collision/collisionInterfaces.h"
63 class SceneRenderState
;
66 class SimplePhysicsBehaviorInstance
;
67 class CollisionInterface
;
69 //////////////////////////////////////////////////////////////////////////
72 //////////////////////////////////////////////////////////////////////////
73 class PlayerControllerComponent
: public Component
,
74 public PhysicsComponentInterface
76 typedef Component Parent
;
79 VelocityMask
= Parent::NextFreeMask
<< 0,
80 PositionMask
= Parent::NextFreeMask
<< 1,
81 NextFreeMask
= Parent::NextFreeMask
<< 2
86 Move move
; ///< Last move from server
87 F32 dt
; ///< Last interpolation time
93 S32 warpTicks
; ///< Number of ticks to warp
94 S32 warpCount
; ///< Current pos in warp
101 PhysicsPlayer
*mPhysicsRep
;
102 PhysicsWorld
*mPhysicsWorld
;
104 CollisionInterface
* mOwnerCollisionInterface
;
108 bool contacted
, jump
, run
;
109 SceneObject
*contactObject
;
110 VectorF contactNormal
;
115 contacted
= jump
= run
= false;
116 contactObject
= NULL
;
117 contactNormal
.set(1, 1, 1);
120 ContactInfo() { clear(); }
136 S32 mContactTimer
; ///< Ticks since last contact
138 U32 mIntegrationCount
;
140 Point3F mJumpSurfaceNormal
; ///< Normal of the surface the player last jumped on
142 F32 maxStepHeight
; ///< Maximum height the player can step up
143 F32 moveSurfaceAngle
; ///< Maximum angle from vertical in degrees the player can run up
144 F32 contactSurfaceAngle
; ///< Maximum angle from vertical in degrees we consider having real 'contact'
146 F32 horizMaxSpeed
; ///< Max speed attainable in the horizontal
148 F32 horizResistSpeed
; ///< Speed at which resistance will take place
149 F32 horizResistFactor
; ///< Factor of resistance once horizResistSpeed has been reached
151 F32 upMaxSpeed
; ///< Max vertical speed attainable
153 F32 upResistSpeed
; ///< Speed at which resistance will take place
154 F32 upResistFactor
; ///< Factor of resistance once upResistSpeed has been reached
156 F32 fallingSpeedThreshold
; ///< Downward speed at which we consider the player falling
161 Point3F mInputVelocity
;
163 bool mUseDirectMoveInput
;
166 PlayerControllerComponent();
167 virtual ~PlayerControllerComponent();
168 DECLARE_CONOBJECT(PlayerControllerComponent
);
170 virtual bool onAdd();
171 virtual void onRemove();
172 static void initPersistFields();
174 virtual void onComponentAdd();
176 virtual void componentAddedToOwner(Component
*comp
);
177 virtual void componentRemovedFromOwner(Component
*comp
);
179 virtual void ownerTransformSet(MatrixF
*mat
);
181 virtual U32
packUpdate(NetConnection
*con
, U32 mask
, BitStream
*stream
);
182 virtual void unpackUpdate(NetConnection
*con
, BitStream
*stream
);
184 void updatePhysics(PhysicsCollision
*collision
= NULL
);
186 virtual void processTick();
187 virtual void interpolateTick(F32 dt
);
188 virtual void updatePos(const F32 dt
);
191 virtual VectorF
getVelocity() { return mVelocity
; }
192 virtual void setVelocity(const VectorF
& vel
);
193 virtual void setTransform(const MatrixF
& mat
);
195 void findContact(bool *run
, bool *jump
, VectorF
*contactNormal
);
196 Point3F
getContactNormal() { return mContactInfo
.contactNormal
; }
197 SceneObject
* getContactObject() { return mContactInfo
.contactObject
; }
198 bool isContacted() { return mContactInfo
.contacted
; }
201 void applyImpulse(const Point3F
&pos
, const VectorF
&vec
);
203 //This is a weird artifact of the PhysicsReps. We want the collision component to be privvy to any events that happen
204 //so when the physics components do a findContact test during their update, they'll have a signal collision components
205 //can be listening to to update themselves with that info
206 Signal
< void(SceneObject
*) > PlayerControllerComponent::onContactSignal
;
209 DECLARE_CALLBACK(void, updateMove
, (PlayerControllerComponent
* obj
));
212 #endif // _COMPONENT_H_