collisionComponent.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #pragma once
  23. #ifndef COLLISION_COMPONENT_H
  24. #define COLLISION_COMPONENT_H
  25. #ifndef COMPONENT_H
  26. #include "T3D/components/component.h"
  27. #endif
  28. #ifndef _CONVEX_H_
  29. #include "collision/convex.h"
  30. #endif
  31. #ifndef _COLLISION_H_
  32. #include "collision/collision.h"
  33. #endif
  34. #ifndef _EARLYOUTPOLYLIST_H_
  35. #include "collision/earlyOutPolyList.h"
  36. #endif
  37. #ifndef _SIM_H_
  38. #include "console/sim.h"
  39. #endif
  40. #ifndef _SCENECONTAINER_H_
  41. #include "scene/sceneContainer.h"
  42. #endif
  43. #ifndef _T3D_PHYSICSCOMMON_H_
  44. #include "T3D/physics/physicsCommon.h"
  45. #endif
  46. #ifndef PHYSICS_COMPONENT_H
  47. #include "T3D/components/physics/physicsComponent.h"
  48. #endif
  49. #ifndef _T3D_PHYSICS_PHYSICSWORLD_H_
  50. #include "T3D/physics/physicsWorld.h"
  51. #endif
  52. struct CollisionContactInfo
  53. {
  54. bool contacted, move;
  55. SceneObject *contactObject;
  56. VectorF idealContactNormal;
  57. VectorF contactNormal;
  58. Point3F contactPoint;
  59. F32 contactTime;
  60. S32 contactTimer;
  61. BaseMatInstance *contactMaterial;
  62. Vector<SceneObject*> overlapObjects;
  63. void clear()
  64. {
  65. contacted=move=false;
  66. contactObject = NULL;
  67. contactNormal.set(0,0,0);
  68. contactTime = 0.f;
  69. contactTimer = 0;
  70. idealContactNormal.set(0, 0, 1);
  71. contactMaterial = NULL;
  72. overlapObjects.clear();
  73. }
  74. CollisionContactInfo() { clear(); }
  75. };
  76. class CollisionComponent : public Component
  77. {
  78. typedef Component Parent;
  79. public:
  80. // CollisionTimeout
  81. // This struct lets us track our collisions and estimate when they've have timed out and we'll need to act on it.
  82. struct CollisionTimeout
  83. {
  84. CollisionTimeout* next;
  85. SceneObject* object;
  86. U32 objectNumber;
  87. SimTime expireTime;
  88. VectorF vector;
  89. };
  90. Signal< void( SceneObject* ) > onCollisionSignal;
  91. Signal< void( SceneObject* ) > onContactSignal;
  92. protected:
  93. PhysicsWorld* mPhysicsWorld;
  94. PhysicsBody* mPhysicsRep;
  95. CollisionTimeout* mTimeoutList;
  96. static CollisionTimeout* sFreeTimeoutList;
  97. CollisionList mCollisionList;
  98. Vector<CollisionComponent*> mCollisionNotifyList;
  99. CollisionContactInfo mContactInfo;
  100. U32 CollisionMoveMask;
  101. bool mBlockColliding;
  102. bool mCollisionInited;
  103. void handleCollisionNotifyList();
  104. void queueCollision( SceneObject *obj, const VectorF &vec);
  105. /// checkEarlyOut
  106. /// This function lets you trying and early out of any expensive collision checks by using simple extruded poly boxes representing our objects
  107. /// 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
  108. /// and we should do the full collision sim.
  109. bool checkEarlyOut(Point3F start, VectorF velocity, F32 time, Box3F objectBox, Point3F objectScale,
  110. Box3F collisionBox, U32 collisionMask, CollisionWorkingList &colWorkingList);
  111. public:
  112. CollisionComponent();
  113. virtual ~CollisionComponent();
  114. DECLARE_CONOBJECT(CollisionComponent);
  115. //Setup
  116. virtual void prepCollision() {};
  117. /// checkCollisions
  118. // This is our main function for checking if a collision is happening based on the start point, velocity and time
  119. // We do the bulk of the collision checking in here
  120. //virtual bool checkCollisions( const F32 travelTime, Point3F *velocity, Point3F start )=0;
  121. CollisionList *getCollisionList() { return &mCollisionList; }
  122. void clearCollisionList() { mCollisionList.clear(); }
  123. void clearCollisionNotifyList() { mCollisionNotifyList.clear(); }
  124. Collision *getCollision(S32 col);
  125. CollisionContactInfo* getContactInfo() { return &mContactInfo; }
  126. enum PublicConstants {
  127. CollisionTimeoutValue = 250
  128. };
  129. bool doesBlockColliding() { return mBlockColliding; }
  130. /// handleCollisionList
  131. /// This basically takes in a CollisionList and calls handleCollision for each.
  132. void handleCollisionList(CollisionList &collisionList, VectorF velocity);
  133. /// handleCollision
  134. /// This will take a collision and queue the collision info for the object so that in knows about the collision.
  135. void handleCollision(Collision &col, VectorF velocity);
  136. virtual bool checkCollisions(const F32 travelTime, Point3F *velocity, Point3F start);
  137. virtual bool updateCollisions(F32 time, VectorF vector, VectorF velocity);
  138. virtual void updateWorkingCollisionSet(const U32 mask);
  139. //
  140. bool buildConvexOpcode(TSShapeInstance* sI, S32 dl, const Box3F &bounds, Convex *c, Convex *list);
  141. bool buildMeshOpcode(TSMesh *mesh, const MatrixF &meshToObjectMat, const Box3F &bounds, Convex *convex, Convex *list);
  142. bool castRayOpcode(S32 dl, const Point3F & startPos, const Point3F & endPos, RayInfo *info);
  143. bool castRayMeshOpcode(TSMesh *mesh, const Point3F &s, const Point3F &e, RayInfo *info, TSMaterialList *materials);
  144. virtual PhysicsCollision* getCollisionData() {
  145. return nullptr;
  146. }
  147. virtual PhysicsBody *getPhysicsRep()
  148. {
  149. return mPhysicsRep;
  150. }
  151. void buildConvex(const Box3F& box, Convex* convex) {}
  152. bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere) { return false; }
  153. //
  154. Point3F getContactNormal();
  155. bool hasContact();
  156. S32 getCollisionCount();
  157. Point3F getCollisionNormal(S32 collisionIndex);
  158. F32 getCollisionAngle(S32 collisionIndex, Point3F upVector);
  159. S32 getBestCollision(Point3F upVector);
  160. F32 getBestCollisionAngle(VectorF upVector);
  161. Signal< void(PhysicsCollision* collision) > onCollisionChanged;
  162. };
  163. #endif