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 iTriangleMesh; 00046 struct iRegion; 00047 struct iSector; 00048 struct iTerrainSystem; 00049 00050 struct csCollisionPair; 00051 class csReversibleTransform; 00052 00053 struct csIntersectingTriangle; 00054 00055 // for iPolygonMesh 00056 #include "csutil/win32/msvc_deprecated_warn_off.h" 00057 00071 class CS_CRYSTALSPACE_EXPORT csColliderWrapper : 00072 public scfImplementationExt1<csColliderWrapper, 00073 csObject, 00074 scfFakeInterface<csColliderWrapper> > 00075 { 00076 private: 00077 csRef<iCollideSystem> collide_system; 00078 csRef<iCollider> collider; 00079 00080 public: 00081 SCF_INTERFACE(csColliderWrapper, 2,1,0); 00082 00083 CS_LEAKGUARD_DECLARE (csColliderWrapper); 00084 00089 #ifndef CS_DEPRECATION_SUPPRESS_HACK 00090 CS_DEPRECATED_METHOD_MSG("Use version with iTriangleMesh instead.") 00091 #endif 00092 csColliderWrapper (csObject& parent, iCollideSystem* collide_system, 00093 iPolygonMesh* mesh); 00094 00099 #ifndef CS_DEPRECATION_SUPPRESS_HACK 00100 CS_DEPRECATED_METHOD_MSG("Use version with iTriangleMesh instead.") 00101 #endif 00102 csColliderWrapper (iObject* parent, iCollideSystem* collide_system, 00103 iPolygonMesh* mesh); 00104 00108 csColliderWrapper (csObject& parent, iCollideSystem* collide_system, 00109 iTriangleMesh* mesh); 00110 00114 csColliderWrapper (iObject* parent, iCollideSystem* collide_system, 00115 iTriangleMesh* mesh); 00116 00118 csColliderWrapper (iObject* parent, iCollideSystem* collide_system, 00119 iTerraFormer* terrain); 00120 00122 csColliderWrapper (iObject* parent, iCollideSystem* collide_system, 00123 iTerrainSystem* terrain); 00124 00130 csColliderWrapper (iObject* parent, iCollideSystem* collide_system, 00131 iCollider* collider); 00132 00134 virtual ~csColliderWrapper (); 00135 00137 iCollider* GetCollider () { return collider; } 00138 00140 iCollideSystem* GetCollideSystem () { return collide_system; } 00141 00149 bool Collide (csColliderWrapper& pOtherCollider, 00150 csReversibleTransform* pThisTransform = 0, 00151 csReversibleTransform* pOtherTransform = 0); 00156 bool Collide (csObject& otherObject, 00157 csReversibleTransform* pThisTransform = 0, 00158 csReversibleTransform* pOtherTransform = 0); 00163 bool Collide (iObject* otherObject, 00164 csReversibleTransform* pThisTransform = 0, 00165 csReversibleTransform* pOtherTransform = 0); 00166 00171 static csColliderWrapper* GetColliderWrapper (csObject& object); 00172 00177 static csColliderWrapper* GetColliderWrapper (iObject* object); 00178 00183 CS_DEPRECATED_METHOD_MSG("Use version with iTriangleMesh instead.") 00184 void UpdateCollider (iPolygonMesh* mesh); 00185 00189 void UpdateCollider (iTriangleMesh* mesh); 00190 00192 void UpdateCollider (iTerraFormer* terrain); 00193 00194 }; 00195 00196 // for iPolygonMesh 00197 #include "csutil/win32/msvc_deprecated_warn_on.h" 00198 00202 struct CS_CRYSTALSPACE_EXPORT csTraceBeamResult 00203 { 00205 00209 csIntersectingTriangle closest_tri; 00214 csVector3 closest_isect; 00218 iMeshWrapper* closest_mesh; 00223 float sqdistance; 00227 iSector* end_sector; 00228 }; 00229 00234 class CS_CRYSTALSPACE_EXPORT csColliderHelper 00235 { 00236 public: 00248 static csColliderWrapper* InitializeCollisionWrapper (iCollideSystem* colsys, 00249 iMeshWrapper* mesh); 00250 00256 static void InitializeCollisionWrappers (iCollideSystem* colsys, 00257 iEngine* engine, iRegion* region = 0); 00258 00276 static bool CollideArray ( 00277 iCollideSystem* colsys, 00278 iCollider* collider, 00279 const csReversibleTransform* trans, 00280 int num_colliders, 00281 iCollider** colliders, 00282 csReversibleTransform **transforms); 00283 00320 static int CollidePath ( 00321 iCollideSystem* colsys, 00322 iCollider* collider, const csReversibleTransform* trans, 00323 float nbrsteps, 00324 csVector3& newpos, 00325 int num_colliders, 00326 iCollider** colliders, 00327 csReversibleTransform** transforms); 00328 00352 static float TraceBeam (iCollideSystem* cdsys, iSector* sector, 00353 const csVector3& start, const csVector3& end, 00354 bool traverse_portals, 00355 csIntersectingTriangle& closest_tri, 00356 csVector3& closest_isect, 00357 iMeshWrapper** closest_mesh = 0, 00358 iSector** end_sector = 0); 00359 00376 static csTraceBeamResult TraceBeam (iCollideSystem* cdsys, iSector* sector, 00377 const csVector3& start, const csVector3& end, 00378 bool traverse_portals); 00379 }; 00380 00385 class CS_CRYSTALSPACE_EXPORT csColliderActor 00386 { 00387 private: 00388 bool revertMove; 00389 bool onground; 00390 bool cd; 00391 csArray<csCollisionPair> our_cd_contact; 00392 float gravity; 00393 iMeshWrapper* mesh; 00394 iCamera* camera; 00395 iMovable* movable; 00396 iCollideSystem* cdsys; 00397 iEngine* engine; 00398 csVector3 velWorld; 00400 csSet<csPtrKey<iMeshWrapper> > hit_meshes; 00401 bool do_hit_meshes; 00402 00404 csVector3 rotation; 00405 00406 csRef<iCollider> topCollider; 00407 csRef<iCollider> bottomCollider; 00408 csBox3 boundingBox; 00409 csVector3 shift; 00410 csVector3 topSize; 00411 csVector3 bottomSize; 00412 csVector3 intervalSize; 00413 00414 int revertCount; 00415 00430 int CollisionDetect ( 00431 iCollider *collider, 00432 iSector* sector, 00433 csReversibleTransform* transform, 00434 csReversibleTransform* old_transform); 00435 00445 int CollisionDetectIterative ( 00446 iCollider *collider, 00447 iSector* sector, 00448 csReversibleTransform* transform, 00449 csReversibleTransform* old_transform, csVector3& maxmove); 00450 bool MoveV (float delta, const csVector3& velBody); 00451 bool RotateV (float delta, const csVector3& angularVelocity); 00452 void InitializeColliders (const csVector3& legs, 00453 const csVector3& body, const csVector3& shift); 00454 00455 public: 00457 csColliderActor (); 00458 00460 void SetCollideSystem (iCollideSystem* cdsys) 00461 { 00462 csColliderActor::cdsys = cdsys; 00463 } 00464 00466 void SetEngine (iEngine* engine) 00467 { 00468 csColliderActor::engine = engine; 00469 } 00470 00480 void InitializeColliders (iMeshWrapper* mesh, const csVector3& legs, 00481 const csVector3& body, const csVector3& shift); 00482 00494 void InitializeColliders (iCamera* camera, const csVector3& legs, 00495 const csVector3& body, const csVector3& shift); 00496 00503 void SetCamera (iCamera* camera, bool adjustRotation = true); 00504 00508 void SetGravity (float g) 00509 { 00510 gravity = g; 00511 velWorld.y = 0; 00512 } 00513 00517 float GetGravity () const { return gravity; } 00518 00522 bool IsOnGround () const { return onground; } 00523 00527 void SetOnGround (bool og) { onground = og; } 00528 00532 bool HasCD () const { return cd; } 00533 00537 void SetCD (bool c) { cd = c; } 00538 00542 bool CheckRevertMove () const { return revertMove; } 00543 00550 void EnableHitMeshes (bool hm) { do_hit_meshes = hm; } 00551 00553 bool CheckHitMeshes () const { return do_hit_meshes; } 00554 00560 const csSet<csPtrKey<iMeshWrapper> >& GetHitMeshes () 00561 { return hit_meshes; } 00562 00575 bool Move (float delta, float speed, const csVector3& velBody, 00576 const csVector3& angularVelocity); 00577 00582 const csVector3& GetRotation () { return rotation; } 00583 00588 void SetRotation (const csVector3& rot); 00589 00595 bool AdjustForCollisions (const csVector3& oldpos, 00596 csVector3& newpos, 00597 const csVector3& vel, 00598 float delta); 00599 }; 00600 00601 #endif // __CS_COLLIDER_H__
Generated for Crystal Space 1.2 by doxygen 1.4.7