collisionInterfaces.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef COLLISION_INTERFACES_H
  23. #define COLLISION_INTERFACES_H
  24. #ifndef _CONVEX_H_
  25. #include "collision/convex.h"
  26. #endif
  27. #ifndef _COLLISION_H_
  28. #include "collision/collision.h"
  29. #endif
  30. #ifndef _EARLYOUTPOLYLIST_H_
  31. #include "collision/earlyOutPolyList.h"
  32. #endif
  33. #ifndef _SIM_H_
  34. #include "console/sim.h"
  35. #endif
  36. #ifndef _SCENECONTAINER_H_
  37. #include "scene/sceneContainer.h"
  38. #endif
  39. #ifndef _T3D_PHYSICSCOMMON_H_
  40. #include "T3D/physics/physicsCommon.h"
  41. #endif
  42. struct ContactInfo
  43. {
  44. bool contacted, move;
  45. SceneObject *contactObject;
  46. VectorF idealContactNormal;
  47. VectorF contactNormal;
  48. Point3F contactPoint;
  49. F32 contactTime;
  50. S32 contactTimer;
  51. BaseMatInstance *contactMaterial;
  52. void clear()
  53. {
  54. contacted=move=false;
  55. contactObject = NULL;
  56. contactNormal.set(0,0,0);
  57. contactTime = 0.f;
  58. contactTimer = 0;
  59. idealContactNormal.set(0, 0, 1);
  60. contactMaterial = NULL;
  61. }
  62. ContactInfo() { clear(); }
  63. };
  64. class CollisionInterface// : public Interface<CollisionInterface>
  65. {
  66. public:
  67. // CollisionTimeout
  68. // This struct lets us track our collisions and estimate when they've have timed out and we'll need to act on it.
  69. struct CollisionTimeout
  70. {
  71. CollisionTimeout* next;
  72. SceneObject* object;
  73. U32 objectNumber;
  74. SimTime expireTime;
  75. VectorF vector;
  76. };
  77. Signal< void( SceneObject* ) > CollisionInterface::onCollisionSignal;
  78. Signal< void( SceneObject* ) > CollisionInterface::onContactSignal;
  79. protected:
  80. CollisionTimeout* mTimeoutList;
  81. static CollisionTimeout* sFreeTimeoutList;
  82. CollisionList mCollisionList;
  83. Vector<CollisionInterface*> mCollisionNotifyList;
  84. ContactInfo mContactInfo;
  85. Box3F mWorkingQueryBox;
  86. U32 CollisionMoveMask;
  87. Convex *mConvexList;
  88. bool mBlockColliding;
  89. void handleCollisionNotifyList();
  90. void queueCollision( SceneObject *obj, const VectorF &vec);
  91. /// checkEarlyOut
  92. /// This function lets you trying and early out of any expensive collision checks by using simple extruded poly boxes representing our objects
  93. /// If it returns true, we know we won't hit with the given parameters and can successfully early out. If it returns false, our test case collided
  94. /// and we should do the full collision sim.
  95. bool checkEarlyOut(Point3F start, VectorF velocity, F32 time, Box3F objectBox, Point3F objectScale,
  96. Box3F collisionBox, U32 collisionMask, CollisionWorkingList &colWorkingList);
  97. public:
  98. /// checkCollisions
  99. // This is our main function for checking if a collision is happening based on the start point, velocity and time
  100. // We do the bulk of the collision checking in here
  101. //virtual bool checkCollisions( const F32 travelTime, Point3F *velocity, Point3F start )=0;
  102. CollisionList *getCollisionList() { return &mCollisionList; }
  103. void clearCollisionList() { mCollisionList.clear(); }
  104. void clearCollisionNotifyList() { mCollisionNotifyList.clear(); }
  105. Collision *getCollision(S32 col);
  106. ContactInfo* getContactInfo() { return &mContactInfo; }
  107. Convex *getConvexList() { return mConvexList; }
  108. virtual bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere) = 0;
  109. enum PublicConstants {
  110. CollisionTimeoutValue = 250
  111. };
  112. bool doesBlockColliding() { return mBlockColliding; }
  113. /// handleCollisionList
  114. /// This basically takes in a CollisionList and calls handleCollision for each.
  115. void handleCollisionList(CollisionList &collisionList, VectorF velocity);
  116. /// handleCollision
  117. /// This will take a collision and queue the collision info for the object so that in knows about the collision.
  118. void handleCollision(Collision &col, VectorF velocity);
  119. virtual PhysicsCollision* getCollisionData() = 0;
  120. Signal< void(PhysicsCollision* collision) > CollisionInterface::onCollisionChanged;
  121. };
  122. class BuildConvexInterface //: public Interface<CollisionInterface>
  123. {
  124. public:
  125. virtual void buildConvex(const Box3F& box, Convex* convex)=0;
  126. };
  127. class BuildPolyListInterface// : public Interface<CollisionInterface>
  128. {
  129. public:
  130. virtual bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere) = 0;
  131. };
  132. #endif