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 iRegion; 00047 struct iSector; 00048 struct iTerrainSystem; 00049 00050 struct csCollisionPair; 00051 class csReversibleTransform; 00052 00053 struct csIntersectingTriangle; 00054 00068 class CS_CRYSTALSPACE_EXPORT csColliderWrapper : 00069 public scfImplementationExt1<csColliderWrapper, 00070 csObject, 00071 scfFakeInterface<csColliderWrapper> > 00072 { 00073 private: 00074 csRef<iCollideSystem> collide_system; 00075 csRef<iCollider> collider; 00076 00077 public: 00078 SCF_INTERFACE(csColliderWrapper, 2,2,0); 00079 00080 CS_LEAKGUARD_DECLARE (csColliderWrapper); 00081 00085 csColliderWrapper (csObject& parent, iCollideSystem* collide_system, 00086 iTriangleMesh* mesh); 00087 00091 csColliderWrapper (iObject* parent, iCollideSystem* collide_system, 00092 iTriangleMesh* mesh); 00093 00095 csColliderWrapper (iObject* parent, iCollideSystem* collide_system, 00096 iTerraFormer* terrain); 00097 00099 csColliderWrapper (iObject* parent, iCollideSystem* collide_system, 00100 iTerrainSystem* terrain); 00101 00107 csColliderWrapper (iObject* parent, iCollideSystem* collide_system, 00108 iCollider* collider); 00109 00111 virtual ~csColliderWrapper (); 00112 00114 iCollider* GetCollider () { return collider; } 00115 00117 iCollideSystem* GetCollideSystem () { return collide_system; } 00118 00126 bool Collide (csColliderWrapper& pOtherCollider, 00127 csReversibleTransform* pThisTransform = 0, 00128 csReversibleTransform* pOtherTransform = 0); 00133 bool Collide (csObject& otherObject, 00134 csReversibleTransform* pThisTransform = 0, 00135 csReversibleTransform* pOtherTransform = 0); 00140 bool Collide (iObject* otherObject, 00141 csReversibleTransform* pThisTransform = 0, 00142 csReversibleTransform* pOtherTransform = 0); 00143 00148 static csColliderWrapper* GetColliderWrapper (csObject& object); 00149 00154 static csColliderWrapper* GetColliderWrapper (iObject* object); 00155 00159 void UpdateCollider (iTriangleMesh* mesh); 00160 00162 void UpdateCollider (iTerraFormer* terrain); 00163 00164 }; 00165 00169 struct CS_CRYSTALSPACE_EXPORT csTraceBeamResult 00170 { 00172 00176 csIntersectingTriangle closest_tri; 00181 csVector3 closest_isect; 00185 iMeshWrapper* closest_mesh; 00190 float sqdistance; 00194 iSector* end_sector; 00195 }; 00196 00201 class CS_CRYSTALSPACE_EXPORT csColliderHelper 00202 { 00203 public: 00215 static csColliderWrapper* InitializeCollisionWrapper (iCollideSystem* colsys, 00216 iMeshWrapper* mesh); 00217 00223 static void InitializeCollisionWrappers (iCollideSystem* colsys, 00224 iEngine* engine, iCollection* collection = 0); 00225 CS_DEPRECATED_METHOD_MSG("Regions are deprecated. Use Collections instead.") 00226 static void InitializeCollisionWrappers (iCollideSystem* colsys, 00227 iEngine* engine, iRegion* region); 00228 /* Hack to ensure source compatibility when a 0 collection/region is used. 00229 * Remove with region variant. */ 00230 static CS_FORCEINLINE void InitializeCollisionWrappers (iCollideSystem* colsys, 00231 iEngine* engine, int dummy) 00232 { 00233 InitializeCollisionWrappers (colsys, engine, (iCollection*)0); 00234 } 00235 00236 00254 static bool CollideArray ( 00255 iCollideSystem* colsys, 00256 iCollider* collider, 00257 const csReversibleTransform* trans, 00258 int num_colliders, 00259 iCollider** colliders, 00260 csReversibleTransform **transforms); 00261 00298 static int CollidePath ( 00299 iCollideSystem* colsys, 00300 iCollider* collider, const csReversibleTransform* trans, 00301 float nbrsteps, 00302 csVector3& newpos, 00303 int num_colliders, 00304 iCollider** colliders, 00305 csReversibleTransform** transforms); 00306 00330 static float TraceBeam (iCollideSystem* cdsys, iSector* sector, 00331 const csVector3& start, const csVector3& end, 00332 bool traverse_portals, 00333 csIntersectingTriangle& closest_tri, 00334 csVector3& closest_isect, 00335 iMeshWrapper** closest_mesh = 0, 00336 iSector** end_sector = 0); 00337 00354 static csTraceBeamResult TraceBeam (iCollideSystem* cdsys, iSector* sector, 00355 const csVector3& start, const csVector3& end, 00356 bool traverse_portals); 00357 }; 00358 00363 class CS_CRYSTALSPACE_EXPORT csColliderActor 00364 { 00365 private: 00366 bool revertMove; 00367 bool onground; 00368 bool cd; 00369 csArray<csCollisionPair> our_cd_contact; 00370 float gravity; 00371 iMeshWrapper* mesh; 00372 iCamera* camera; 00373 iMovable* movable; 00374 iCollideSystem* cdsys; 00375 iEngine* engine; 00376 csVector3 velWorld; 00378 csSet<csPtrKey<iMeshWrapper> > hit_meshes; 00379 bool do_hit_meshes; 00380 00382 csVector3 rotation; 00383 00384 csRef<iCollider> topCollider; 00385 csRef<iCollider> bottomCollider; 00386 csBox3 boundingBox; 00387 csVector3 shift; 00388 csVector3 topSize; 00389 csVector3 bottomSize; 00390 csVector3 intervalSize; 00391 00392 int revertCount; 00393 00408 int CollisionDetect ( 00409 iCollider *collider, 00410 iSector* sector, 00411 csReversibleTransform* transform, 00412 csReversibleTransform* old_transform); 00413 00423 int CollisionDetectIterative ( 00424 iCollider *collider, 00425 iSector* sector, 00426 csReversibleTransform* transform, 00427 csReversibleTransform* old_transform, csVector3& maxmove); 00428 bool MoveV (float delta, const csVector3& velBody); 00429 bool RotateV (float delta, const csVector3& angularVelocity); 00430 void InitializeColliders (const csVector3& legs, 00431 const csVector3& body, const csVector3& shift); 00432 00433 public: 00435 csColliderActor (); 00436 00438 void SetCollideSystem (iCollideSystem* cdsys) 00439 { 00440 csColliderActor::cdsys = cdsys; 00441 } 00442 00444 void SetEngine (iEngine* engine) 00445 { 00446 csColliderActor::engine = engine; 00447 } 00448 00458 void InitializeColliders (iMeshWrapper* mesh, const csVector3& legs, 00459 const csVector3& body, const csVector3& shift); 00460 00472 void InitializeColliders (iCamera* camera, const csVector3& legs, 00473 const csVector3& body, const csVector3& shift); 00474 00481 void SetCamera (iCamera* camera, bool adjustRotation = true); 00482 00486 void SetGravity (float g) 00487 { 00488 gravity = g; 00489 velWorld.y = 0; 00490 } 00491 00495 float GetGravity () const { return gravity; } 00496 00500 bool IsOnGround () const { return onground; } 00501 00505 void SetOnGround (bool og) { onground = og; } 00506 00510 bool HasCD () const { return cd; } 00511 00515 void SetCD (bool c) { cd = c; } 00516 00520 bool CheckRevertMove () const { return revertMove; } 00521 00528 void EnableHitMeshes (bool hm) { do_hit_meshes = hm; } 00529 00531 bool CheckHitMeshes () const { return do_hit_meshes; } 00532 00538 const csSet<csPtrKey<iMeshWrapper> >& GetHitMeshes () 00539 { return hit_meshes; } 00540 00553 bool Move (float delta, float speed, const csVector3& velBody, 00554 const csVector3& angularVelocity); 00555 00560 const csVector3& GetRotation () { return rotation; } 00561 00566 void SetRotation (const csVector3& rot); 00567 00573 bool AdjustForCollisions (const csVector3& oldpos, 00574 csVector3& newpos, 00575 const csVector3& vel, 00576 float delta); 00577 }; 00578 00579 #endif // __CS_COLLIDER_H__
Generated for Crystal Space 1.4.0 by doxygen 1.5.8