Browse Source

Some collision work

Panagiotis Christopoulos Charitos 11 năm trước cách đây
mục cha
commit
94312b79e9
1 tập tin đã thay đổi với 72 bổ sung8 xóa
  1. 72 8
      src/collision/Tests.cpp

+ 72 - 8
src/collision/Tests.cpp

@@ -7,6 +7,8 @@
 #include "anki/collision/Aabb.h"
 #include "anki/collision/Sphere.h"
 #include "anki/collision/CompoundShape.h"
+#include "anki/collision/LineSegment.h"
+#include "anki/collision/Plane.h"
 #include "anki/collision/GjkEpa.h"
 #include "anki/util/Rtti.h"
 
@@ -49,6 +51,61 @@ static Bool test(const Aabb& a, const Aabb& b)
 	return true;
 }
 
+//==============================================================================
+Bool test(const Aabb& aabb, const LineSegment& ls)
+{
+	F32 maxS = MIN_F32;
+	F32 minT = MAX_F32;
+
+	// do tests against three sets of planes
+	for(U i = 0; i < 3; ++i)
+	{
+		// segment is parallel to plane
+		if(isZero(ls.getDirection()[i]))
+		{
+			// segment passes by box
+			if(ls.getOrigin()[i] < aabb.getMin()[i] 
+				|| ls.getOrigin()[i] > aabb.getMax()[i])
+			{
+				return false;
+			}
+		}
+		else
+		{
+			// compute intersection parameters and sort
+			F32 s = (aabb.getMin()[i] - ls.getOrigin()[i]) 
+				/ ls.getDirection()[i];
+			F32 t = (aabb.getMax()[i] - ls.getOrigin()[i]) 
+				/ ls.getDirection()[i];
+			if(s > t)
+			{
+				F32 temp = s;
+				s = t;
+				t = temp;
+			}
+
+			// adjust min and max values
+			if(s > maxS)
+			{
+				maxS = s;
+			}
+			if(t < minT)
+			{
+				minT = t;
+			}
+
+			// check for intersection failure
+			if(minT < 0.0 || maxS > 1.0 || maxS > minT)
+			{
+				return false;
+			}
+		}
+	}
+
+	// done, have intersection
+	return true;
+}
+
 //==============================================================================
 static Bool test(const Sphere& a, const Sphere& b)
 {
@@ -66,18 +123,25 @@ Bool t(const CollisionShape& a, const CollisionShape& b)
 	return test(dcast<const A&>(a), dcast<const B&>(b));
 }
 
-using Callback = Bool (*)(const CollisionShape& a, const CollisionShape& b);
+/// Test plane.
+template<typename A>
+Bool tp(const CollisionShape& a, const CollisionShape& p)
+{
+	return dcast<const A&>(a).testPlane(dcast<const Plane&>(p));
+}
+
+using Callback = Bool(*)(const CollisionShape& a, const CollisionShape& b);
 
 static const U COUNT = U(CollisionShape::Type::COUNT);
 
 static const Callback matrix[COUNT][COUNT] = {
-	// AABB         Comp     LS       OBB      PL       S
-	{t<Aabb, Aabb>, nullptr, nullptr, gjk,     nullptr, nullptr          },  // AABB
-	{nullptr,       nullptr, nullptr, nullptr, nullptr, nullptr          },  // Comp
-	{nullptr,       nullptr, nullptr, nullptr, nullptr, nullptr          },  // LS
-	{gjk,           nullptr, nullptr, gjk,     nullptr, nullptr          },  // OBB
-	{nullptr,       nullptr, nullptr, nullptr, nullptr, nullptr          },  // PL
-	{nullptr,       nullptr, nullptr, gjk,     nullptr, t<Sphere, Sphere>}}; // S
+/*          AABB                  Comp     LS                    OBB      PL       S               */
+/* AABB */ {t<Aabb, Aabb>,        nullptr, t<Aabb, LineSegment>, gjk,     nullptr, nullptr          },  
+/* Comp */ {nullptr,              nullptr, nullptr,              nullptr, nullptr, nullptr          },  
+/* LS   */ {t<Aabb, LineSegment>, nullptr, nullptr,              nullptr, nullptr, nullptr          },
+/* OBB  */ {gjk,                  nullptr, nullptr,              gjk,     nullptr, nullptr          },
+/* PL   */ {tp<Aabb>,             nullptr, nullptr,              nullptr, nullptr, nullptr          },
+/* S    */ {nullptr,              nullptr, nullptr,              gjk,     nullptr, t<Sphere, Sphere>}};
 
 } // end namespace anki