Просмотр исходного кода

Optimize AABB vs AABB collision

Panagiotis Christopoulos Charitos 7 лет назад
Родитель
Сommit
e4f2421c55

+ 2 - 1
samples/physics_playground/assets/scene.lua

@@ -259,7 +259,8 @@ lcomp:setShadowEnabled(1)
 
 node = scene:newPerspectiveCameraNode("Camera")
 scene:setActiveCameraNode(node:getSceneNodeBase())
-node:setAll(1.22173, 1.0 / getMainRenderer():getAspectRatio() * 1.22173, 0.1, 500)
+frustumc = node:getSceneNodeBase():getFrustumComponent()
+frustumc:setPerspective(0.1, 500, 1.22173, 1.0 / getMainRenderer():getAspectRatio() * 1.22173)
 trf = Transform.new()
 trf:setOrigin(Vec4.new(7.48113, 7.87011, 6.50764, 0))
 rot = Mat3x4.new()

+ 115 - 47
src/anki/collision/Functions.h

@@ -60,63 +60,131 @@ Aabb computeAabb(const LineSegment& ls);
 /// @copydoc computeAabb(const ConvexHullShape&)
 Aabb computeAabb(const Cone& cone);
 
-#define ANKI_DEF_TEST_COLLISION_FUNC(type0, type1) \
-	Bool testCollision(const type0&, const type1&); \
-	inline Bool testCollision(const type1& arg1, const type0& arg0) \
-	{ \
-		return testCollision(arg0, arg1); \
-	}
-
-#define ANKI_DEF_TEST_COLLISION_FUNC_PLANE(type) \
-	inline Bool testCollision(const type& arg0, const Plane& plane) \
-	{ \
-		return testPlane(plane, arg0) == 0.0f; \
-	} \
-	inline Bool testCollision(const Plane& plane, const type& arg0) \
-	{ \
-		return testPlane(plane, arg0) == 0.0f; \
-	}
-
+// Aabb
 Bool testCollision(const Aabb& a, const Aabb& b);
-ANKI_DEF_TEST_COLLISION_FUNC(Aabb, Sphere)
-ANKI_DEF_TEST_COLLISION_FUNC(Aabb, Obb)
-ANKI_DEF_TEST_COLLISION_FUNC(Aabb, ConvexHullShape)
-ANKI_DEF_TEST_COLLISION_FUNC(Aabb, LineSegment)
-ANKI_DEF_TEST_COLLISION_FUNC(Aabb, Cone)
-ANKI_DEF_TEST_COLLISION_FUNC(Aabb, Ray)
-ANKI_DEF_TEST_COLLISION_FUNC_PLANE(Aabb)
-
+Bool testCollision(const Aabb& a, const Sphere& b);
+Bool testCollision(const Aabb& a, const Obb& b);
+Bool testCollision(const Aabb& a, const ConvexHullShape& b);
+Bool testCollision(const Aabb& a, const LineSegment& b);
+Bool testCollision(const Aabb& a, const Cone& b);
+Bool testCollision(const Aabb& a, const Ray& b);
+
+// Sphere
+inline Bool testCollision(const Sphere& a, const Aabb& b)
+{
+	return testCollision(b, a);
+}
 Bool testCollision(const Sphere& a, const Sphere& b);
-ANKI_DEF_TEST_COLLISION_FUNC(Sphere, Obb)
-ANKI_DEF_TEST_COLLISION_FUNC(Sphere, ConvexHullShape)
-ANKI_DEF_TEST_COLLISION_FUNC(Sphere, LineSegment)
-ANKI_DEF_TEST_COLLISION_FUNC(Sphere, Cone)
-ANKI_DEF_TEST_COLLISION_FUNC(Sphere, Ray)
-ANKI_DEF_TEST_COLLISION_FUNC_PLANE(Sphere)
-
+Bool testCollision(const Sphere& a, const Obb& b);
+Bool testCollision(const Sphere& a, const ConvexHullShape& b);
+Bool testCollision(const Sphere& a, const LineSegment& b);
+Bool testCollision(const Sphere& a, const Cone& b);
+Bool testCollision(const Sphere& a, const Ray& b);
+
+// Obb
+inline Bool testCollision(const Obb& a, const Aabb& b)
+{
+	return testCollision(b, a);
+}
+inline Bool testCollision(const Obb& a, const Sphere& b)
+{
+	return testCollision(b, a);
+}
 Bool testCollision(const Obb& a, const Obb& b);
-ANKI_DEF_TEST_COLLISION_FUNC(Obb, ConvexHullShape)
-ANKI_DEF_TEST_COLLISION_FUNC(Obb, LineSegment)
-ANKI_DEF_TEST_COLLISION_FUNC(Obb, Cone)
-ANKI_DEF_TEST_COLLISION_FUNC_PLANE(Obb)
+Bool testCollision(const Obb& a, const ConvexHullShape& b);
+Bool testCollision(const Obb& a, const LineSegment& b);
+Bool testCollision(const Obb& a, const Cone& b);
+Bool testCollision(const Obb& a, const Ray& b);
 
+// ConvexHullShape
+inline Bool testCollision(const ConvexHullShape& a, const Aabb& b)
+{
+	return testCollision(b, a);
+}
+inline Bool testCollision(const ConvexHullShape& a, const Sphere& b)
+{
+	return testCollision(b, a);
+}
+inline Bool testCollision(const ConvexHullShape& a, const Obb& b)
+{
+	return testCollision(b, a);
+}
 Bool testCollision(const ConvexHullShape& a, const ConvexHullShape& b);
-ANKI_DEF_TEST_COLLISION_FUNC(ConvexHullShape, LineSegment)
-ANKI_DEF_TEST_COLLISION_FUNC(ConvexHullShape, Cone)
-ANKI_DEF_TEST_COLLISION_FUNC(ConvexHullShape, Ray)
-ANKI_DEF_TEST_COLLISION_FUNC_PLANE(ConvexHullShape)
+Bool testCollision(const ConvexHullShape& a, const LineSegment& b);
+Bool testCollision(const ConvexHullShape& a, const Cone& b);
+Bool testCollision(const ConvexHullShape& a, const Ray& b);
 
+// LineSegment
+inline Bool testCollision(const LineSegment& a, const Aabb& b)
+{
+	return testCollision(b, a);
+}
+inline Bool testCollision(const LineSegment& a, const Sphere& b)
+{
+	return testCollision(b, a);
+}
+inline Bool testCollision(const LineSegment& a, const Obb& b)
+{
+	return testCollision(b, a);
+}
+inline Bool testCollision(const LineSegment& a, const ConvexHullShape& b)
+{
+	return testCollision(b, a);
+}
 Bool testCollision(const LineSegment& a, const LineSegment& b);
-ANKI_DEF_TEST_COLLISION_FUNC(LineSegment, Cone)
-ANKI_DEF_TEST_COLLISION_FUNC(LineSegment, Ray)
-ANKI_DEF_TEST_COLLISION_FUNC_PLANE(LineSegment)
+Bool testCollision(const LineSegment& a, const Cone& b);
+Bool testCollision(const LineSegment& a, const Ray& b);
 
+// Cone
+inline Bool testCollision(const Cone& a, const Aabb& b)
+{
+	return testCollision(b, a);
+}
+inline Bool testCollision(const Cone& a, const Sphere& b)
+{
+	return testCollision(b, a);
+}
+inline Bool testCollision(const Cone& a, const Obb& b)
+{
+	return testCollision(b, a);
+}
+inline Bool testCollision(const Cone& a, const ConvexHullShape& b)
+{
+	return testCollision(b, a);
+}
+inline Bool testCollision(const Cone& a, const LineSegment& b)
+{
+	return testCollision(b, a);
+}
 Bool testCollision(const Cone& a, const Cone& b);
-ANKI_DEF_TEST_COLLISION_FUNC(Cone, Ray)
-ANKI_DEF_TEST_COLLISION_FUNC_PLANE(Cone)
+Bool testCollision(const Cone& a, const Ray& b);
 
-Bool testCollision(const Ray& a, const Ray& b);
-ANKI_DEF_TEST_COLLISION_FUNC_PLANE(Ray)
+// Ray
+inline Bool testCollision(const Ray& a, const Aabb& b)
+{
+	return testCollision(b, a);
+}
+inline Bool testCollision(const Ray& a, const Sphere& b)
+{
+	return testCollision(b, a);
+}
+inline Bool testCollision(const Ray& a, const Obb& b)
+{
+	return testCollision(b, a);
+}
+inline Bool testCollision(const Ray& a, const ConvexHullShape& b)
+{
+	return testCollision(b, a);
+}
+inline Bool testCollision(const Ray& a, const LineSegment& b)
+{
+	return testCollision(b, a);
+}
+inline Bool testCollision(const Ray& a, const Cone& b)
+{
+	return testCollision(b, a);
+}
+Bool testCollision(const Cone& a, const Ray& b);
 
 // Extra testCollision functions
 

+ 11 - 0
src/anki/collision/FunctionsTestCollision.cpp

@@ -28,6 +28,16 @@ static Bool testCollisionGjk(const T& a, const Y& b)
 
 Bool testCollision(const Aabb& a, const Aabb& b)
 {
+#if ANKI_SIMD == ANKI_SIMD_SSE
+	const __m128 gt0 = _mm_cmpgt_ps(a.getMin().getSimd(), b.getMax().getSimd());
+	const __m128 gt1 = _mm_cmpgt_ps(b.getMin().getSimd(), a.getMax().getSimd());
+
+	const __m128 combined = _mm_or_ps(gt0, gt1);
+
+	const int res = _mm_movemask_ps(combined); // Will set the first bit of each byte of combined
+
+	return res == 0;
+#else
 	// if separated in x direction
 	if(a.getMin().x() > b.getMax().x() || b.getMin().x() > a.getMax().x())
 	{
@@ -48,6 +58,7 @@ Bool testCollision(const Aabb& a, const Aabb& b)
 
 	// no separation, must be intersecting
 	return true;
+#endif
 }
 
 Bool testCollision(const Aabb& aabb, const Sphere& s)