Planeshift

linmove.h

Go to the documentation of this file.
00001 /*
00002   Crystal Space Entity Layer
00003   Copyright (C) 2001 PlaneShift Team ([email protected],
00004   Copyright (C) 2001-2003 by Jorrit Tyberghein
00005 
00006   This library is free software; you can redistribute it and/or
00007   modify it under the terms of the GNU Library General Public
00008   License as published by the Free Software Foundation; either
00009   version 2 of the License, or (at your option) any later version.
00010 
00011   This library is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   Library General Public License for more details.
00015 
00016   You should have received a copy of the GNU Library General Public
00017   License along with this library; if not, write to the Free
00018   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019 */
00020 
00021 /*
00022  * This code is heavily based on pslinmove from the PlaneShift project.
00023  * Thanks a lot for making this!
00024  */
00025 
00026 #ifndef __CEL_PF_LINEAR_MOVE_FACT__
00027 #define __CEL_PF_LINEAR_MOVE_FACT__
00028 
00029 //CS Includes
00030 #include "cstypes.h"
00031 #include "iutil/comp.h"
00032 #include "iutil/eventh.h"
00033 #include "csutil/scf.h"
00034 #include "csutil/stringarray.h"
00035 #include "cstool/collider.h"
00036 #include "ivaria/collider.h"
00037 #include "csutil/sysfunc.h"
00038 #include "colldet.h"
00039 
00040 #define PS_LINMOVE_FOLLOW_ONLY_PORTALS true
00041 
00042 #define PS_MOVE_FAIL 0
00043 #define PS_MOVE_SUCCEED 1
00044 #define PS_MOVE_PARTIAL 2
00045 #define PS_MOVE_DONTMOVE 3
00046 
00047 struct iObjectRegistry;
00048 struct iVirtualClock;
00049 struct iCollider;
00050 struct iCollideSystem;
00051 struct iSector;
00052 class csReversibleTransform;
00053 class csObject;
00054 class csColliderWrapper;
00055 struct iPath;
00056 class psCollisionDetection;
00057 
00058 /* Max deviation before we need to send
00059    a new DR packet */
00060 #define PSLINEARMOVEMENT_DRDELTA 0.2
00061 
00062 
00068 class psLinearMovement
00069 {
00070 public:
00071     psLinearMovement(iObjectRegistry* object_reg);
00072     virtual ~psLinearMovement();
00073 
00074     virtual void SetAngularVelocity(const csVector3 &angle);
00075     virtual void SetAngularVelocity(const csVector3 &angle, const csVector3 &angle_to_reach);
00076 
00078     virtual void SetVelocity(const csVector3 &vel);
00079 
00081     virtual void AddVelocity(const csVector3 &vel);
00082 
00084     virtual void ClearWorldVelocity();
00085 
00086     virtual void GetVelocity(csVector3 &v) const;
00087 
00088     virtual const csVector3 GetVelocity() const;
00089 
00090 
00091     virtual bool RotateV(float delta);
00092 
00096     virtual const csVector3 GetAngularVelocity() const;
00097     virtual void GetAngularVelocity(csVector3 &v) const;
00098 
00099     virtual bool InitCD(const csVector3 &body, const csVector3 &legs, const csVector3 &shift, iMeshWrapper* mesh);
00100     virtual void GetCDDimensions(csVector3 &body, csVector3 &legs, csVector3 &shift);
00101 
00102     virtual float GetYRotation() const;
00103     virtual void SetYRotation(float yrot);
00104     const csVector3 GetPosition() const;
00105     const csVector3 GetFullPosition() const;
00106 
00107     void SetScale(float set_scale)
00108     {
00109         scale = set_scale;
00110     }
00111 
00112     virtual void GetLastPosition(csVector3 &pos, float &yrot, iSector* &sector) const;
00113     virtual void GetLastFullPosition(csVector3 &pos, float &yrot, iSector* &sector);
00114     virtual void SetPosition(const csVector3 &pos, float yrot, const iSector* sector);
00115     virtual void SetFullPosition(const csVector3 &pos, float yrot, const iSector* sector);
00116     virtual void SetPosition(const char* center_name, float yrot, iSector* sector);
00117     virtual void SetFullPosition(const char* center_name, float yrot, iSector* sector);
00118     virtual void GetLastClientPosition(csVector3 &pos, float &yrot, iSector* &sector);
00119 
00120     virtual bool IsOnGround() const;
00121 
00122     virtual void SetOnGround(bool onground);
00123 
00124 
00126     virtual void SetHugGround(bool hugGround);
00127 
00128     virtual void SetGravity(float grav);
00129 
00130     virtual float GetGravity();
00131 
00132     virtual void ResetGravity();
00133 
00134     virtual bool IsPath() const;
00135 
00136 
00141     virtual csTicks TimeDiff(void);
00142 
00143     virtual csTicks ClientTimeDiff()
00144     {
00145         return csGetTicks() - lastClientDRUpdate;
00146     }
00147 
00149     virtual void GetDRData(bool &on_ground, csVector3 &pos,
00150                            float &yrot, iSector* &sector, csVector3 &vel, csVector3 &worldVel,
00151                            float &ang_vel);
00152 
00154     virtual void SetDRData(bool on_ground, csVector3 &pos,
00155                            float yrot, iSector* sector, csVector3 &vel, csVector3 &worldVel,
00156                            float ang_vel);
00157 
00166     virtual void SetSoftDRData(bool on_ground, csVector3 &pos,
00167                                float yrot, iSector* sector, csVector3 &vel, csVector3 &worldVel,
00168                                float ang_vel);
00169 
00170     virtual void TickEveryFrame();
00171 
00172     virtual iSector* GetSector() const;
00173 
00178     virtual int ExtrapolatePosition(float delta);
00179 
00186     virtual void UpdateDRDelta(csTicks ticksdelta);
00194     virtual void UpdateDR(csTicks delta);
00195     virtual void UpdateDR();
00196 
00202     virtual void SetPath(iPath* newpath);
00203 
00208     virtual void SetPathTime(float timeval);
00209 
00217     virtual void SetPathSpeed(float speed);
00218 
00227     virtual void SetPathAction(int which, const char* action);
00228 
00233     virtual void SetPathSector(const char* sectorname);
00234 
00235     virtual void SetDeltaLimit(float deltaLimit);
00236 
00238     virtual csVector3 GetPortalDisplacement();
00239 
00241     virtual void ClearPortalDisplacement();
00242 
00243 
00244     void UseCD(bool cd);
00245 
00248     void StackTrace(const char* error);
00249 
00250 protected:
00251 
00252     // Move local entity
00253     // both MoveV and MoveSprite return CEL_MOVE_* constants defined above
00254     int MoveV(float delta);
00255     int MoveSprite(float delta);
00256     void OffsetSprite(float delta);
00257 
00262     void HugGround(const csVector3 &pos, iSector* sector);
00263 
00264 
00265     // Returns a list of sectors near a position.
00266     int FindSectors(const csVector3 &pos, float size, iSector** sectors);
00267 
00268 
00269     static csCollisionPair our_cd_contact[1000];
00270     static int num_our_cd;
00271 
00272     iObjectRegistry* object_reg;
00273 
00274     csRef<iMeshWrapper> mesh;
00275     psCollisionDetection* colldet;
00276 
00277     csRef<iEngine> engine;
00278     csRef<iVirtualClock> vc;
00279     csRef<iCollideSystem> cdsys;
00280 
00281     // Linear vars
00282     float angDelta;
00283     bool stationary;
00286     float gravity;
00287 
00288     csVector3 angularVelocity;
00289     csVector3 angleToReach;
00290     bool angleToReachFlag;
00291     csVector3 velBody;
00292 
00293     csVector3 velWorld;
00294 
00296     bool hugGround;
00297 
00298     float xRot;
00299     float zRot;
00300 
00301     float scale;
00302 
00303     // Path vars
00304     csRef<iPath> path;
00305     float path_time, path_speed;
00306     csStringArray path_actions;
00307     bool path_sent;
00308     csString path_sector;
00309 
00310     //Collision vars
00311     csVector3 shift;
00312     csVector3 topSize;
00313 
00314     csVector3 bottomSize;
00315 
00316     csBox3 boundingBox;
00317 
00319     csVector3 portalDisplaced;
00320 
00322     csVector3 intervalSize;
00323 
00324     csTicks lastDRUpdate;
00325     csTicks lastClientDRUpdate;
00326     csVector3 lastClientPosition;
00327     float lastClientYrot;
00328     iSector* lastClientSector;
00329     float deltaLimit;
00330 
00331     // Variables for 'Soft Update' of position
00332     csVector3 offset_err;    
00333     csVector3 offset_rate;   
00334 };
00335 
00336 #endif