| ## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# cstool/collider.h

Go to the documentation of this file.

    00001 /*
    00002     Copyright (C) 1998-2003 by Jorrit Tyberghein
    00003     Written by Alex Pfaffe.
    00004
    00005     This library is free software; you can redistribute it and/or
    00006     modify it under the terms of the GNU Library General Public
    00007     License as published by the Free Software Foundation; either
    00008     version 2 of the License, or (at your option) any later version.
    00009
    00010     This library is distributed in the hope that it will be useful,
    00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013     Library General Public License for more details.
    00014
    00015     You should have received a copy of the GNU Library General Public
    00016     License along with this library; if not, write to the Free
    00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00018 */
    00019 #ifndef __CS_COLLIDER_H__
    00020 #define __CS_COLLIDER_H__
    00021
    00026 #include "csextern.h"
    00027
    00028 #include "csgeom/box.h"
    00029 #include "csutil/csobject.h"
    00030 #include "csutil/leakguard.h"
    00031 #include "csutil/array.h"
    00032 #include "csutil/scf_implementation.h"
    00033 #include "csutil/set.h"
    00034
    00035 #include "ivaria/collider.h"
    00036
    00037 struct iCamera;
    00038 struct iCollider;
    00039 struct iCollideSystem;
    00040 struct iEngine;
    00041 struct iMeshWrapper;
    00042 struct iMovable;
    00043 struct iObject;
    00044 struct iPolygonMesh;
    00045 struct iRegion;
    00046 struct iSector;
    00047
    00048 struct csCollisionPair;
    00049 class csReversibleTransform;
    00050
    00051 struct csIntersectingTriangle;
    00052
    00053
    00067 class CS_CRYSTALSPACE_EXPORT csColliderWrapper :
    00068   public scfImplementationExt1<csColliderWrapper,
    00069                                csObject,
    00070                                scfFakeInterface<csColliderWrapper> >
    00071 {
    00072 private:
    00073   csRef<iCollideSystem> collide_system;
    00074   csRef<iCollider> collider;
    00075
    00076 public:
    00077   SCF_INTERFACE(csColliderWrapper, 2,0,0);
    00078
    00079   CS_LEAKGUARD_DECLARE (csColliderWrapper);
    00080
    00082   csColliderWrapper (csObject& parent, iCollideSystem* collide_system,
    00083         iPolygonMesh* mesh);
    00084
    00086   csColliderWrapper (iObject* parent, iCollideSystem* collide_system,
    00087         iPolygonMesh* mesh);
    00088
    00090   csColliderWrapper (iObject* parent, iCollideSystem* collide_system,
    00091         iTerraFormer* terrain);
    00092
    00098   csColliderWrapper (iObject* parent, iCollideSystem* collide_system,
    00099         iCollider* collider);
    00100
    00102   virtual ~csColliderWrapper ();
    00103
    00105   iCollider* GetCollider () { return collider; }
    00106
    00108   iCollideSystem* GetCollideSystem () { return collide_system; }
    00109
    00117   bool Collide (csColliderWrapper& pOtherCollider,
    00118                 csReversibleTransform* pThisTransform = 0,
    00119                 csReversibleTransform* pOtherTransform = 0);
    00124   bool Collide (csObject& otherObject,
    00125                 csReversibleTransform* pThisTransform = 0,
    00126                 csReversibleTransform* pOtherTransform = 0);
    00131   bool Collide (iObject* otherObject,
    00132                 csReversibleTransform* pThisTransform = 0,
    00133                 csReversibleTransform* pOtherTransform = 0);
    00134
    00139   static csColliderWrapper* GetColliderWrapper (csObject& object);
    00140
    00145   static csColliderWrapper* GetColliderWrapper (iObject* object);
    00146 };
    00147
    00151 struct CS_CRYSTALSPACE_EXPORT csTraceBeamResult
    00152 {
    00154
    00158   csIntersectingTriangle closest_tri;
    00163   csVector3 closest_isect;
    00167   iMeshWrapper* closest_mesh;
    00172   float sqdistance;
    00176   iSector* end_sector;
    00177 };
    00178
    00183 class CS_CRYSTALSPACE_EXPORT csColliderHelper
    00184 {
    00185 public:
    00197   static csColliderWrapper* InitializeCollisionWrapper (iCollideSystem* colsys,
    00198         iMeshWrapper* mesh);
    00199
    00205   static void InitializeCollisionWrappers (iCollideSystem* colsys,
    00206         iEngine* engine, iRegion* region = 0);
    00207
    00225   static bool CollideArray (
    00226         iCollideSystem* colsys,
    00227         iCollider* collider,
    00228         const csReversibleTransform* trans,
    00229         int num_colliders,
    00230         iCollider** colliders,
    00231         csReversibleTransform **transforms);
    00232
    00269   static int CollidePath (
    00270         iCollideSystem* colsys,
    00271         iCollider* collider, const csReversibleTransform* trans,
    00272         float nbrsteps,
    00273         csVector3& newpos,
    00274         int num_colliders,
    00275         iCollider** colliders,
    00276         csReversibleTransform** transforms);
    00277
    00301   static float TraceBeam (iCollideSystem* cdsys, iSector* sector,
    00302         const csVector3& start, const csVector3& end,
    00303         bool traverse_portals,
    00304         csIntersectingTriangle& closest_tri,
    00305         csVector3& closest_isect,
    00306         iMeshWrapper** closest_mesh = 0,
    00307         iSector** end_sector = 0);
    00308
    00325   static csTraceBeamResult TraceBeam (iCollideSystem* cdsys, iSector* sector,
    00326         const csVector3& start, const csVector3& end,
    00327         bool traverse_portals);
    00328 };
    00329
    00334 class CS_CRYSTALSPACE_EXPORT csColliderActor
    00335 {
    00336 private:
    00337   bool revertMove;
    00338   bool onground;
    00339   bool cd;
    00340   csArray<csCollisionPair> our_cd_contact;
    00341   float gravity;
    00342   iMeshWrapper* mesh;
    00343   iCamera* camera;
    00344   iMovable* movable;
    00345   iCollideSystem* cdsys;
    00346   iEngine* engine;
    00347   csVector3 velWorld;
    00349   csSet<csPtrKey<iMeshWrapper> > hit_meshes;
    00350   bool do_hit_meshes;
    00351
    00353   csVector3 rotation;
    00354
    00355   csRef<iCollider> topCollider;
    00356   csRef<iCollider> bottomCollider;
    00357   csBox3 boundingBox;
    00358   csVector3 shift;
    00359   csVector3 topSize;
    00360   csVector3 bottomSize;
    00361   csVector3 intervalSize;
    00362
    00363   int revertCount;
    00364
    00379   int CollisionDetect (
    00380         iCollider *collider,
    00381         iSector* sector,
    00382         csReversibleTransform* transform,
    00383         csReversibleTransform* old_transform);
    00384
    00394   int CollisionDetectIterative (
    00395         iCollider *collider,
    00396         iSector* sector,
    00397         csReversibleTransform* transform,
    00398         csReversibleTransform* old_transform, csVector3& maxmove);
    00399   bool MoveV (float delta, const csVector3& velBody);
    00400   bool RotateV (float delta, const csVector3& angularVelocity);
    00401   void InitializeColliders (const csVector3& legs,
    00402         const csVector3& body, const csVector3& shift);
    00403
    00404 public:
    00406   csColliderActor ();
    00407
    00409   void SetCollideSystem (iCollideSystem* cdsys)
    00410   {
    00411     csColliderActor::cdsys = cdsys;
    00412   }
    00413
    00415   void SetEngine (iEngine* engine)
    00416   {
    00417     csColliderActor::engine = engine;
    00418   }
    00419
    00429   void InitializeColliders (iMeshWrapper* mesh, const csVector3& legs,
    00430         const csVector3& body, const csVector3& shift);
    00431
    00443   void InitializeColliders (iCamera* camera, const csVector3& legs,
    00444         const csVector3& body, const csVector3& shift);
    00445
    00452   void SetCamera (iCamera* camera, bool adjustRotation = true);
    00453
    00457   void SetGravity (float g)
    00458   {
    00459     gravity = g;
    00460     velWorld.y = 0;
    00461   }
    00462
    00466   float GetGravity () const { return gravity; }
    00467
    00471   bool IsOnGround () const { return onground; }
    00472
    00476   void SetOnGround (bool og) { onground = og; }
    00477
    00481   bool HasCD () const { return cd; }
    00482
    00486   void SetCD (bool c) { cd = c; }
    00487
    00491   bool CheckRevertMove () const { return revertMove; }
    00492
    00499   void EnableHitMeshes (bool hm) { do_hit_meshes = hm; }
    00500
    00502   bool CheckHitMeshes () const { return do_hit_meshes; }
    00503
    00509   const csSet<csPtrKey<iMeshWrapper> >& GetHitMeshes ()
    00510   { return hit_meshes; }
    00511
    00524   bool Move (float delta, float speed, const csVector3& velBody,
    00525         const csVector3& angularVelocity);
    00526
    00531   const csVector3& GetRotation () { return rotation; }
    00532
    00537   void SetRotation (const csVector3& rot);
    00538
    00544   bool AdjustForCollisions (const csVector3& oldpos,
    00545         csVector3& newpos,
    00546         const csVector3& vel,
    00547         float delta);
    00548 };
    00549
    00550 #endif // __CS_COLLIDER_H__