TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CollisionDetection.h
Go to the documentation of this file.
1 
22 #ifndef G3D_CollisionDetection_h
23 #define G3D_CollisionDetection_h
24 
25 #include "G3D/platform.h"
26 #include "G3D/Vector3.h"
27 #include "G3D/Plane.h"
28 #include "G3D/Box.h"
29 #include "G3D/Triangle.h"
30 #include "G3D/Array.h"
31 #include "G3D/Ray.h"
32 #include "G3D/Line.h"
33 
34 namespace G3D {
35 
36 
95 private:
96 
101  static Vector3 ignore;
102 
107  static bool ignoreBool;
108 
114 
115  // Static class!
117  virtual ~CollisionDetection() {}
118 
119 public:
120 
133  const int separatingAxisIndex,
134  const Box & box1,
135  const Box & box2);
136 
153  const double* ca,
154  const double epsilon,
155  int & axis1,
156  int & axis2);
157 
180  const int separatingAxisIndex,
181  const Vector3 & a,
182  const Vector3 & b,
183  const Vector3 & D,
184  const double* c,
185  const double* ca,
186  const double* ad,
187  const double* bd);
188 
189 
225  static void fillSolidBoxSolidBoxInfo(
226  const Box & box1,
227  const Box & box2,
228  Vector3 & a,
229  Vector3 & b,
230  Vector3 & D,
231  double* c,
232  double* ca,
233  double* ad,
234  double* bd);
235 
250  static bool conservativeBoxBoxTest(
251  const Vector3 & a,
252  const Vector3 & b,
253  const Vector3 & D);
254 
277  const Box& box1,
278  const Box& box2,
279  const int lastSeparatingAxis = -1);
280 
301  const Line & line1,
302  const Line & line2,
303  Vector3 & closest1,
304  Vector3 & closest2);
305 
339  const Box& box1,
340  const Box& box2,
341  Array<Vector3>& contactPoints,
342  Array<Vector3>& contactNormals,
343  const int lastSeparatingAxis = -1);
344 
362  const class Sphere& sphereA,
363  const Sphere& sphereB,
364  Array<Vector3>& contactPoints,
365  Array<Vector3>& contactNormals = ignoreArray);
366 
400  const Sphere& sphere,
401  const Box& box,
402  Array<Vector3>& contactPoints,
403  Array<Vector3>& contactNormals = ignoreArray);
404 
420  const Sphere& sphereA,
421  const class Plane& planeB,
422  Array<Vector3>& contactPoints,
423  Array<Vector3>& contactNormals = ignoreArray);
424 
440  const Box& box,
441  const Plane& plane,
442  Array<Vector3>& contactPoints,
443  Array<Vector3>& contactNormals = ignoreArray);
444 
464  const Vector3& point,
465  const Vector3& velocity,
466  const class Plane& plane,
467  Vector3& outLocation,
468  Vector3& outNormal = ignore);
469 
488  const Vector3& orig,
489  const Vector3& dir,
490  const Vector3& v0,
491  const Vector3& v1,
492  const Vector3& v2) {
493  return Ray::fromOriginAndDirection(orig, dir).intersectionTime(v0, v1, v2);
494  }
495 
516  const Vector3& orig,
517  const Vector3& dir,
518  const Vector3& v0,
519  const Vector3& v1,
520  const Vector3& v2,
521  Vector3& location) {
522  float t = collisionTimeForMovingPointFixedTriangle(orig, dir, v0, v1, v2);
523  if (t < finf()) {
524  location = orig + dir * t;
525  }
526  return t;
527  }
528 
548  const Vector3& orig,
549  const Vector3& dir,
550  const Triangle& tri,
551  Vector3& location = ignore,
552  Vector3& normal = ignore) {
553 
555  orig, dir, tri.vertex(0), tri.vertex(1), tri.vertex(2));
556 
557  if ((t < finf()) && (&location != &ignore)) {
558  location = orig + dir * t;
559  normal = tri.normal();
560  }
561  return t;
562  }
563 
585  const Vector3& orig,
586  const Vector3& dir,
587  const Vector3& v0,
588  const Vector3& v1,
589  const Vector3& v2,
590  Vector3& location,
591  Vector3& normal) {
592  float t = collisionTimeForMovingPointFixedTriangle(orig, dir, v0, v1, v2);
593  if (t < finf()) {
594  location = orig + dir * t;
595  normal = (v1 - v0).cross(v2 - v0).direction();
596  }
597  return t;
598  }
599 
611  const Vector3& point,
612  const Vector3& velocity,
613  const class AABox& box,
614  Vector3& outLocation,
615  bool& inside = ignoreBool,
616  Vector3& outNormal = ignore);
617 
635  const Vector3& point,
636  const Vector3& velocity,
637  const class AABox& box,
638  Vector3& outLocation,
639  bool& inside = ignoreBool,
640  Vector3& normal = ignore);
641 
642 
656  static bool __fastcall rayAABox(
657  const Ray& ray,
658  const Vector3& invDir,
659  const AABox& box,
660  const Vector3& boxCenter,
661  float boundingRadiusSquared,
662  Vector3& location,
663  bool& inside);
664 
683  const Vector3& point,
684  const Vector3& velocity,
685  const class Sphere& sphere,
686  Vector3& outLocation,
687  Vector3& outNormal = ignore,
688  bool solid = false);
689 
706  const Vector3& point,
707  const Vector3& velocity,
708  const class Box& box,
709  Vector3& outLocation,
710  Vector3& outNormal = ignore);
711 
733  const Vector3& point,
734  const Vector3& velocity,
735  const Vector3& v0,
736  const Vector3& v1,
737  const Vector3& v2,
738  const Vector3& v3,
739  Vector3& outLocation,
740  Vector3& outNormal = ignore);
741 
756  const Vector3& point,
757  const Vector3& velocity,
758  const class Capsule& capsule,
759  Vector3& outLocation,
760  Vector3& outNormal = ignore);
761 
777  const class Sphere& sphere,
778  const Vector3& velocity,
779  const class Plane& plane,
780  Vector3& outLocation,
781  Vector3& outNormal = ignore);
782 
799  const class Sphere& sphere,
800  const Vector3& velocity,
801  const Triangle& triangle,
802  Vector3& outLocation,
803  float b[3] = (float*)&ignore);
804 
823  const class Sphere& sphere,
824  const Vector3& velocity,
825  const Vector3& v0,
826  const Vector3& v1,
827  const Vector3& v2,
828  const Vector3& v3,
829  Vector3& outLocation,
830  Vector3& outNormal = ignore);
831 
850  const class Sphere& sphere,
851  const Vector3& velocity,
852  const class Box& box,
853  Vector3& outLocation,
854  Vector3& outNormal = ignore);
855 
874  const Sphere& sphere,
875  const Vector3& velocity,
876  const Sphere& fixedSphere,
877  Vector3& outLocation,
878  Vector3& outNormal = ignore);
879 
898  const class Sphere& sphere,
899  const Vector3& velocity,
900  const class Capsule& capsule,
901  Vector3& outLocation,
902  Vector3& outNormal = ignore);
903 
919  static Vector3 bounceDirection(
920  const class Sphere& sphere,
921  const Vector3& velocity,
922  const float collisionTime,
923  const Vector3& collisionLocation,
924  const Vector3& collisionNormal);
925 
942  static Vector3 slideDirection(
943  const class Sphere& sphere,
944  const Vector3& velocity,
945  const float collisionTime,
946  const Vector3& collisionLocation);
947 
958  const Vector3& v0,
959  const Vector3& v1,
960  const Vector3& point);
961 
978  const Vector3& v0,
979  const Vector3& v1,
980  const Vector3& edgeDirection,
981  float edgeLength,
982  const Vector3& point);
983 
997  const Vector3& v0,
998  const Vector3& v1,
999  const Vector3& v2,
1000  const Vector3& point);
1001 
1019  const Vector3 v[3],
1020  const Vector3 edgeDirection[3],
1021  const float edgeLength[3],
1022  const Vector3& point,
1023  int& edgeIndex);
1024 
1041  static bool isPointInsideTriangle(
1042  const Vector3& v0,
1043  const Vector3& v1,
1044  const Vector3& v2,
1045  const Vector3& normal,
1046  const Vector3& point,
1047  float b[3],
1048  Vector3::Axis primaryAxis = Vector3::DETECT_AXIS);
1049 
1050  inline static bool isPointInsideTriangle(
1051  const Vector3& v0,
1052  const Vector3& v1,
1053  const Vector3& v2,
1054  const Vector3& normal,
1055  const Vector3& point,
1056  Vector3::Axis primaryAxis = Vector3::DETECT_AXIS) {
1057 
1058  float b[3];
1059  return isPointInsideTriangle(v0, v1, v2, normal, point, b, primaryAxis);
1060  }
1061 
1079  const Sphere& sphere,
1080  const Vector3& velocity,
1081  const Box& box,
1082  double timeLimit = inf());
1083 
1100  const Sphere& sphere,
1101  const Vector3& velocity,
1102  const Sphere& fixedSphere,
1103  double timeLimit = inf());
1104 
1115  const Sphere& sphere1,
1116  const Sphere& sphere2);
1117 
1128  const Sphere& sphere,
1129  const Box& box);
1130 
1132  const Sphere& sphere,
1133  const Triangle& triangle);
1134 
1136  const AABox& box,
1137  const Triangle& triangle);
1138 
1153  static bool isPointInsideRectangle(
1154  const Vector3& v0,
1155  const Vector3& v1,
1156  const Vector3& v2,
1157  const Vector3& v3,
1158  const Vector3& normal,
1159  const Vector3& point);
1160 
1176  const Vector3& v0,
1177  const Vector3& v1,
1178  const Vector3& v2,
1179  const Vector3& v3,
1180  const Vector3& point);
1181 
1196  const Vector3& v0,
1197  const Vector3& v1,
1198  const Vector3& v2,
1199  const Vector3& v3,
1200  const Vector3& point);
1201 };
1202 
1203 } // namespace
1204 
1205 #endif // G3D_COLLISIONDETECTION_H
float finf()
Definition: g3dmath.cpp:71
static float collisionTimeForMovingPointFixedRectangle(const Vector3 &point, const Vector3 &velocity, const Vector3 &v0, const Vector3 &v1, const Vector3 &v2, const Vector3 &v3, Vector3 &outLocation, Vector3 &outNormal=ignore)
Definition: CollisionDetection.cpp:1308
#define __fastcall
Fast call is a register-based optimized calling convention supported only by Visual C++...
Definition: platform.h:55
Definition: adtfile.h:46
Definition: Plane.h:25
static float penetrationDepthForFixedSphereFixedPlane(const Sphere &sphereA, const class Plane &planeB, Array< Vector3 > &contactPoints, Array< Vector3 > &contactNormals=ignoreArray)
Definition: CollisionDetection.cpp:788
Definition: Line.h:25
virtual ~CollisionDetection()
Definition: CollisionDetection.h:117
static float collisionTimeForMovingSphereFixedRectangle(const class Sphere &sphere, const Vector3 &velocity, const Vector3 &v0, const Vector3 &v1, const Vector3 &v2, const Vector3 &v3, Vector3 &outLocation, Vector3 &outNormal=ignore)
Definition: CollisionDetection.cpp:1750
static float penetrationDepthForFixedSphereFixedBox(const Sphere &sphere, const Box &box, Array< Vector3 > &contactPoints, Array< Vector3 > &contactNormals=ignoreArray)
Definition: CollisionDetection.cpp:554
Definition: AABox.h:25
Dynamic 1D array tuned for performance.
Definition: Array.h:95
static Vector3 slideDirection(const class Sphere &sphere, const Vector3 &velocity, const float collisionTime, const Vector3 &collisionLocation)
Definition: CollisionDetection.cpp:1874
static bool __fastcall rayAABox(const Ray &ray, const Vector3 &invDir, const AABox &box, const Vector3 &boxCenter, float boundingRadiusSquared, Vector3 &location, bool &inside)
Calculates intersection of a ray and a static Axis-Aligned Box (AABox).
Definition: CollisionDetection.cpp:886
static bool fixedSolidBoxIntersectsFixedTriangle(const AABox &box, const Triangle &triangle)
Definition: CollisionDetection.cpp:2367
Axis
Definition: Vector3.h:122
Vector3 direction() const
Definition: Vector3.h:756
static float collisionTimeForMovingSphereFixedTriangle(const class Sphere &sphere, const Vector3 &velocity, const Triangle &triangle, Vector3 &outLocation, float b[3]=(float *)&ignore)
Definition: CollisionDetection.cpp:1636
static Vector3 ignore
Definition: CollisionDetection.h:101
static void closestPointsBetweenLineAndLine(const Line &line1, const Line &line2, Vector3 &closest1, Vector3 &closest2)
Definition: CollisionDetection.cpp:309
Definition: CollisionDetection.h:94
An arbitrary (oriented) 3D box, useful as a bounding box.
Definition: Box.h:35
Definition: Vector3.h:58
static bool isPointInsideTriangle(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2, const Vector3 &normal, const Vector3 &point, float b[3], Vector3::Axis primaryAxis=Vector3::DETECT_AXIS)
Definition: CollisionDetection.cpp:2009
static bool isPointInsideRectangle(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2, const Vector3 &v3, const Vector3 &normal, const Vector3 &point)
Definition: CollisionDetection.cpp:2086
static float penetrationDepthForFixedSphereFixedSphere(const class Sphere &sphereA, const Sphere &sphereB, Array< Vector3 > &contactPoints, Array< Vector3 > &contactNormals=ignoreArray)
Definition: CollisionDetection.cpp:764
static bool fixedSolidSphereIntersectsFixedSolidSphere(const Sphere &sphere1, const Sphere &sphere2)
Definition: CollisionDetection.cpp:2172
Definition: Vector3.h:122
Definition: Sphere.h:24
static bool ignoreBool
Definition: CollisionDetection.h:107
double inf()
Definition: g3dmath.cpp:40
static bool fixedSolidBoxIntersectsFixedSolidBox(const Box &box1, const Box &box2, const int lastSeparatingAxis=-1)
Definition: CollisionDetection.cpp:251
static float collisionTimeForMovingPointFixedTriangle(const Vector3 &orig, const Vector3 &dir, const Vector3 &v0, const Vector3 &v1, const Vector3 &v2, Vector3 &location, Vector3 &normal)
Definition: CollisionDetection.h:584
static float collisionTimeForMovingPointFixedAABox(const Vector3 &point, const Vector3 &velocity, const class AABox &box, Vector3 &outLocation, bool &inside=ignoreBool, Vector3 &outNormal=ignore)
Definition: CollisionDetection.cpp:1210
static void fillSolidBoxSolidBoxInfo(const Box &box1, const Box &box2, Vector3 &a, Vector3 &b, Vector3 &D, double *c, double *ca, double *ad, double *bd)
Definition: CollisionDetection.cpp:202
static float collisionTimeForMovingPointFixedTriangle(const Vector3 &orig, const Vector3 &dir, const Vector3 &v0, const Vector3 &v1, const Vector3 &v2)
Definition: CollisionDetection.h:487
static Vector3 closestPointToRectanglePerimeter(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2, const Vector3 &v3, const Vector3 &point)
Definition: CollisionDetection.cpp:2099
static bool collisionLocationForMovingPointFixedAABox(const Vector3 &point, const Vector3 &velocity, const class AABox &box, Vector3 &outLocation, bool &inside=ignoreBool, Vector3 &normal=ignore)
Definition: CollisionDetection.cpp:1226
Definition: Capsule.h:27
static Vector3 closestPointOnTrianglePerimeter(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2, const Vector3 &point)
Definition: CollisionDetection.cpp:1935
static Ray fromOriginAndDirection(const Point3 &point, const Vector3 &direction)
Definition: Ray.h:87
static float collisionTimeForMovingPointFixedCapsule(const Vector3 &point, const Vector3 &velocity, const class Capsule &capsule, Vector3 &outLocation, Vector3 &outNormal=ignore)
Definition: CollisionDetection.cpp:1542
static bool movingSpherePassesThroughFixedSphere(const Sphere &sphere, const Vector3 &velocity, const Sphere &fixedSphere, double timeLimit=inf())
Definition: CollisionDetection.cpp:2225
Definition: Ray.h:24
Definition: AABox.h:32
static bool fixedSolidSphereIntersectsFixedSolidBox(const Sphere &sphere, const Box &box)
Definition: CollisionDetection.cpp:2180
static Array< Vector3 > ignoreArray
Definition: CollisionDetection.h:113
static Vector3 closestPointOnLineSegment(const Vector3 &v0, const Vector3 &v1, const Vector3 &point)
Definition: CollisionDetection.cpp:1889
static float penetrationDepthForFixedBoxFixedPlane(const Box &box, const Plane &plane, Array< Vector3 > &contactPoints, Array< Vector3 > &contactNormals=ignoreArray)
Definition: CollisionDetection.cpp:813
static bool parallelAxisForSolidBoxSolidBox(const double *ca, const double epsilon, int &axis1, int &axis2)
Definition: CollisionDetection.cpp:183
static float collisionTimeForMovingSphereFixedBox(const class Sphere &sphere, const Vector3 &velocity, const class Box &box, Vector3 &outLocation, Vector3 &outNormal=ignore)
Definition: CollisionDetection.cpp:1790
static float collisionTimeForMovingPointFixedSphere(const Vector3 &point, const Vector3 &velocity, const class Sphere &sphere, Vector3 &outLocation, Vector3 &outNormal=ignore, bool solid=false)
Definition: CollisionDetection.cpp:977
static float collisionTimeForMovingSphereFixedPlane(const class Sphere &sphere, const Vector3 &velocity, const class Plane &plane, Vector3 &outLocation, Vector3 &outNormal=ignore)
Definition: CollisionDetection.cpp:1589
Vector3 cross(const Vector3 &v1, const Vector3 &v2)
Definition: vectorMath.h:144
const Vector3 & normal() const
Definition: Triangle.cpp:93
static float collisionTimeForMovingSphereFixedCapsule(const class Sphere &sphere, const Vector3 &velocity, const class Capsule &capsule, Vector3 &outLocation, Vector3 &outNormal=ignore)
Definition: CollisionDetection.cpp:1826
static float penetrationDepthForFixedBoxFixedBox(const Box &box1, const Box &box2, Array< Vector3 > &contactPoints, Array< Vector3 > &contactNormals, const int lastSeparatingAxis=-1)
Definition: CollisionDetection.cpp:347
static Vector3 bounceDirection(const class Sphere &sphere, const Vector3 &velocity, const float collisionTime, const Vector3 &collisionLocation, const Vector3 &collisionNormal)
Definition: CollisionDetection.cpp:1850
static bool movingSpherePassesThroughFixedBox(const Sphere &sphere, const Vector3 &velocity, const Box &box, double timeLimit=inf())
Definition: CollisionDetection.cpp:2207
static float collisionTimeForMovingPointFixedTriangle(const Vector3 &orig, const Vector3 &dir, const Vector3 &v0, const Vector3 &v1, const Vector3 &v2, Vector3 &location)
Definition: CollisionDetection.h:515
static Vector3 separatingAxisForSolidBoxSolidBox(const int separatingAxisIndex, const Box &box1, const Box &box2)
Definition: CollisionDetection.cpp:45
static float collisionTimeForMovingPointFixedPlane(const Vector3 &point, const Vector3 &velocity, const class Plane &plane, Vector3 &outLocation, Vector3 &outNormal=ignore)
Definition: CollisionDetection.cpp:847
static bool isPointInsideTriangle(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2, const Vector3 &normal, const Vector3 &point, Vector3::Axis primaryAxis=Vector3::DETECT_AXIS)
Definition: CollisionDetection.h:1050
static float collisionTimeForMovingPointFixedTriangle(const Vector3 &orig, const Vector3 &dir, const Triangle &tri, Vector3 &location=ignore, Vector3 &normal=ignore)
Definition: CollisionDetection.h:547
const Point3 & vertex(int n) const
Definition: Triangle.h:70
static Vector3 closestPointToRectangle(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2, const Vector3 &v3, const Vector3 &point)
Definition: CollisionDetection.cpp:2148
static float collisionTimeForMovingSphereFixedSphere(const Sphere &sphere, const Vector3 &velocity, const Sphere &fixedSphere, Vector3 &outLocation, Vector3 &outNormal=ignore)
Definition: CollisionDetection.cpp:1032
static float collisionTimeForMovingPointFixedBox(const Vector3 &point, const Vector3 &velocity, const class Box &box, Vector3 &outLocation, Vector3 &outNormal=ignore)
Definition: CollisionDetection.cpp:1176
Definition: Triangle.h:34
float intersectionTime(const class Sphere &sphere, bool solid=false) const
Definition: Ray.cpp:179
CollisionDetection()
Definition: CollisionDetection.h:116
static bool conservativeBoxBoxTest(const Vector3 &a, const Vector3 &b, const Vector3 &D)
Definition: CollisionDetection.cpp:239
static float projectedDistanceForSolidBoxSolidBox(const int separatingAxisIndex, const Vector3 &a, const Vector3 &b, const Vector3 &D, const double *c, const double *ca, const double *ad, const double *bd)
Definition: CollisionDetection.cpp:69
static bool fixedSolidSphereIntersectsFixedTriangle(const Sphere &sphere, const Triangle &triangle)
Definition: CollisionDetection.cpp:2245