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 iCollection; 00039 struct iCollider; 00040 struct iCollideSystem; 00041 struct iEngine; 00042 struct iMeshWrapper; 00043 struct iMovable; 00044 struct iObject; 00045 struct iTriangleMesh; 00046 struct iSector; 00047 struct iTerrainSystem; 00048 00049 struct csCollisionPair; 00050 class csReversibleTransform; 00051 00052 struct csIntersectingTriangle; 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,2,0); 00078 00079 CS_LEAKGUARD_DECLARE (csColliderWrapper); 00080 00084 csColliderWrapper (csObject& parent, iCollideSystem* collide_system, 00085 iTriangleMesh* mesh); 00086 00090 csColliderWrapper (iObject* parent, iCollideSystem* collide_system, 00091 iTriangleMesh* mesh); 00092 00094 csColliderWrapper (iObject* parent, iCollideSystem* collide_system, 00095 iTerraFormer* terrain); 00096 00098 csColliderWrapper (iObject* parent, iCollideSystem* collide_system, 00099 iTerrainSystem* terrain); 00100 00106 csColliderWrapper (iObject* parent, iCollideSystem* collide_system, 00107 iCollider* collider); 00108 00110 virtual ~csColliderWrapper (); 00111 00113 iCollider* GetCollider () { return collider; } 00114 00116 iCollideSystem* GetCollideSystem () { return collide_system; } 00117 00125 bool Collide (csColliderWrapper& pOtherCollider, 00126 csReversibleTransform* pThisTransform = 0, 00127 csReversibleTransform* pOtherTransform = 0); 00132 bool Collide (csObject& otherObject, 00133 csReversibleTransform* pThisTransform = 0, 00134 csReversibleTransform* pOtherTransform = 0); 00139 bool Collide (iObject* otherObject, 00140 csReversibleTransform* pThisTransform = 0, 00141 csReversibleTransform* pOtherTransform = 0); 00142 00147 static csColliderWrapper* GetColliderWrapper (csObject& object); 00148 00153 static csColliderWrapper* GetColliderWrapper (iObject* object); 00154 00158 void UpdateCollider (iTriangleMesh* mesh); 00159 00161 void UpdateCollider (iTerraFormer* terrain); 00162 00163 }; 00164 00168 struct CS_CRYSTALSPACE_EXPORT csTraceBeamResult 00169 { 00171 00175 csIntersectingTriangle closest_tri; 00180 csVector3 closest_isect; 00184 iMeshWrapper* closest_mesh; 00189 float sqdistance; 00193 iSector* end_sector; 00194 }; 00195 00200 class CS_CRYSTALSPACE_EXPORT csColliderHelper 00201 { 00202 public: 00214 static csColliderWrapper* InitializeCollisionWrapper (iCollideSystem* colsys, 00215 iMeshWrapper* mesh); 00216 00222 static void InitializeCollisionWrappers (iCollideSystem* colsys, 00223 iEngine* engine, iCollection* collection = 0); 00224 00230 static void InitializeCollisionWrappers (iCollideSystem* colsys, 00231 iSector* sector, iCollection* collection = 0); 00232 00250 static bool CollideArray ( 00251 iCollideSystem* colsys, 00252 iCollider* collider, 00253 const csReversibleTransform* trans, 00254 int num_colliders, 00255 iCollider** colliders, 00256 csReversibleTransform **transforms); 00257 00294 static int CollidePath ( 00295 iCollideSystem* colsys, 00296 iCollider* collider, const csReversibleTransform* trans, 00297 float nbrsteps, 00298 csVector3& newpos, 00299 int num_colliders, 00300 iCollider** colliders, 00301 csReversibleTransform** transforms); 00302 00326 static float TraceBeam (iCollideSystem* cdsys, iSector* sector, 00327 const csVector3& start, const csVector3& end, 00328 bool traverse_portals, 00329 csIntersectingTriangle& closest_tri, 00330 csVector3& closest_isect, 00331 iMeshWrapper** closest_mesh = 0, 00332 iSector** end_sector = 0); 00333 00350 static csTraceBeamResult TraceBeam (iCollideSystem* cdsys, iSector* sector, 00351 const csVector3& start, const csVector3& end, 00352 bool traverse_portals); 00353 }; 00354 00359 class CS_CRYSTALSPACE_EXPORT csColliderActor 00360 { 00361 private: 00362 bool onground; 00363 bool cd; 00364 csArray<csCollisionPair> our_cd_contact; 00365 float gravity; 00366 iMeshWrapper* mesh; 00367 iCamera* camera; 00368 iMovable* movable; 00369 iCollideSystem* cdsys; 00370 iEngine* engine; 00371 csVector3 velWorld; 00373 csSet<csPtrKey<iMeshWrapper> > hit_meshes; 00374 bool do_hit_meshes; 00375 00377 csVector3 rotation; 00378 00379 csRef<iCollider> topCollider; 00380 csRef<iCollider> bottomCollider; 00381 csBox3 boundingBox; 00382 csVector3 shift; 00383 csVector3 topSize; 00384 csVector3 bottomSize; 00385 csVector3 intervalSize; 00386 00401 int CollisionDetect ( 00402 iCollider *collider, 00403 iSector* sector, 00404 csReversibleTransform* transform, 00405 csReversibleTransform* old_transform); 00406 00416 int CollisionDetectIterative ( 00417 iCollider *collider, 00418 iSector* sector, 00419 csReversibleTransform* transform, 00420 csReversibleTransform* old_transform, csVector3& maxmove); 00421 bool MoveV (float delta, const csVector3& velBody); 00422 bool RotateV (float delta, const csVector3& angularVelocity); 00423 void InitializeColliders (const csVector3& legs, 00424 const csVector3& body, const csVector3& shift); 00425 00426 public: 00428 csColliderActor (); 00429 00431 void SetCollideSystem (iCollideSystem* cdsys) 00432 { 00433 csColliderActor::cdsys = cdsys; 00434 } 00435 00437 void SetEngine (iEngine* engine) 00438 { 00439 csColliderActor::engine = engine; 00440 } 00441 00451 void InitializeColliders (iMeshWrapper* mesh, const csVector3& legs, 00452 const csVector3& body, const csVector3& shift); 00453 00465 void InitializeColliders (iCamera* camera, const csVector3& legs, 00466 const csVector3& body, const csVector3& shift); 00467 00474 void SetCamera (iCamera* camera, bool adjustRotation = true); 00475 00479 void SetGravity (float g) 00480 { 00481 gravity = g; 00482 velWorld.y = 0; 00483 } 00484 00488 float GetGravity () const { return gravity; } 00489 00493 bool IsOnGround () const { return onground; } 00494 00498 void SetOnGround (bool og) { onground = og; } 00499 00503 bool HasCD () const { return cd; } 00504 00508 void SetCD (bool c) { cd = c; } 00509 00516 void EnableHitMeshes (bool hm) { do_hit_meshes = hm; } 00517 00519 bool CheckHitMeshes () const { return do_hit_meshes; } 00520 00526 const csSet<csPtrKey<iMeshWrapper> >& GetHitMeshes () 00527 { return hit_meshes; } 00528 00541 bool Move (float delta, float speed, const csVector3& velBody, 00542 const csVector3& angularVelocity); 00543 00548 const csVector3& GetRotation () { return rotation; } 00549 00554 void SetRotation (const csVector3& rot); 00555 00561 bool AdjustForCollisions (const csVector3& oldpos, 00562 csVector3& newpos, 00563 const csVector3& vel, 00564 float delta); 00565 }; 00566 00567 #endif // __CS_COLLIDER_H__
Generated for Crystal Space 2.0 by doxygen 1.6.1