Explorar el Código

Refactoring collision a bit. Work on the Sector

Panagiotis Christopoulos Charitos hace 13 años
padre
commit
b7bae2217c

+ 7 - 0
include/anki/collision/Aabb.h

@@ -69,6 +69,13 @@ public:
 	}
 	/// @}
 
+	/// Check for collision
+	template<typename T>
+	Bool collide(const T& x) const
+	{
+		return detail::collide(*this, x);
+	}
+
 	/// Implements CollisionShape::accept
 	void accept(MutableVisitor& v)
 	{

+ 3 - 0
include/anki/collision/Collision.h

@@ -1,6 +1,9 @@
 #ifndef ANKI_COLLISION_COLLISION_H
 #define ANKI_COLLISION_COLLISION_H
 
+/// @defgroup Collision
+/// Collision detection module
+
 #include "anki/collision/Plane.h"
 #include "anki/collision/Sphere.h"
 #include "anki/collision/Obb.h"

+ 172 - 0
include/anki/collision/CollisionAlgorithms.h

@@ -0,0 +1,172 @@
+#ifndef ANKI_COLLISION_COLLISION_ALGORITHMS_H
+#define ANKI_COLLISION_COLLISION_ALGORITHMS_H
+
+#include "anki/collision/Forward.h"
+#include "anki/util/StdTypes.h"
+
+namespace anki {
+namespace detail {
+
+/// @addtogroup Collision
+/// @{
+/// @addtogroup Algorithms
+/// Provides the collision algorithms that detect collision between various
+/// shapes
+/// @code
+/// +------+------+------+------+------+------+------+------+
+/// |      | LS   | OBB  | FRU  | P    | R    | S    | AABB |
+/// +------+------+------+------+------+------+------+------+
+/// | LS   | N/A  | OK   | N/I  | OK   | N/A  | OK   | OK   |
+/// +------+------+------+------+------+------+------+------+
+/// | OBB  |      | OK   | N/I  | OK   | OK   | OK   | OK   |
+/// +------+------+------+------+------+------+------+------+
+/// | FRU  |      |      | N/I  | N/I  | N/I  | N/I  | N/I  |
+/// +------+------+------+------+------+------+------+------+
+/// | P    |      |      |      | OK   | OK   | OK   | OK   |
+/// +------+------+------+------+------+------+------+------+
+/// | R    |      |      |      |      | N/A  | OK   | OK   |
+/// +------+------+------+------+------+------+------+------+
+/// | S    |      |      |      |      |      | OK   | OK   |
+/// +------+------+------+------+------+------+------+------+
+/// | AABB |      |      |      |      |      |      | OK   |
+/// +------+------+------+------+------+------+------+------+
+/// @endcode
+/// @{
+
+/// Generic collide function. It doesn't uses visitor pattern for
+/// speed reasons
+extern Bool collide(const CollisionShape& a, const CollisionShape& b);
+
+// 1st line (LS)
+extern Bool collide(const LineSegment& a, const LineSegment& b);
+extern Bool collide(const LineSegment& a, const Obb& b);
+extern Bool collide(const LineSegment& a, const Frustum& b);
+extern Bool collide(const LineSegment& a, const Plane& b);
+extern Bool collide(const LineSegment& a, const Ray& b);
+extern Bool collide(const LineSegment& a, const Sphere& b);
+extern Bool collide(const LineSegment& a, const Aabb& b);
+
+// 2nd line (OBB)
+inline Bool collide(const Obb& a, const LineSegment& b)
+{
+	return collide(b, a);
+}
+extern Bool collide(const Obb& a, const Obb& b);
+extern Bool collide(const Obb& a, const Frustum& b);
+extern Bool collide(const Obb& a, const Plane& b);
+extern Bool collide(const Obb& a, const Ray& b);
+extern Bool collide(const Obb& a, const Sphere& b);
+extern Bool collide(const Obb& a, const Aabb& b);
+
+// 3rd line (FRU)
+inline Bool collide(const Frustum& a, const LineSegment& b)
+{
+	return collide(b, a);
+}
+inline Bool collide(const Frustum& a, const Obb& b)
+{
+	return collide(b, a);
+}
+extern Bool collide(const Frustum& a, const Frustum& b);
+extern Bool collide(const Frustum& a, const Plane& b);
+extern Bool collide(const Frustum& a, const Ray& b);
+extern Bool collide(const Frustum& a, const Sphere& b);
+extern Bool collide(const Frustum& a, const Aabb& b);
+
+// 4th line (P)
+inline Bool collide(const Plane& a, const LineSegment& b)
+{
+	return collide(b, a);
+}
+inline Bool collide(const Plane& a, const Obb& b)
+{
+	return collide(b, a);
+}
+inline Bool collide(const Plane& a,const Frustum& b)
+{
+	return collide(b, a);
+}
+extern Bool collide(const Plane& a, const Plane& b);
+extern Bool collide(const Plane& a, const Ray& b);
+extern Bool collide(const Plane& a, const Sphere& b);
+extern Bool collide(const Plane& a, const Aabb& b);
+
+// 5th line (R)
+inline Bool collide(const Ray& a, const LineSegment& b)
+{
+	return collide(b, a);
+}
+inline Bool collide(const Ray& a, const Obb& b)
+{
+	return collide(b, a);
+}
+inline Bool collide(const Ray& a, const Frustum& b)
+{
+	return collide(b, a);
+}
+inline Bool collide(const Ray& a, const Plane& b)
+{
+	return collide(b, a);
+}
+extern Bool collide(const Ray& a, const Ray& b);
+extern Bool collide(const Ray& a, const Sphere& b);
+extern Bool collide(const Ray& a, const Aabb& b);
+
+// 6th line (S)
+inline Bool collide(const Sphere& a, const LineSegment& b)
+{
+	return collide(b, a);
+}
+inline Bool collide(const Sphere& a, const Obb& b)
+{
+	return collide(b, a);
+}
+inline Bool collide(const Sphere& a, const Frustum& b)
+{
+	return collide(b, a);
+}
+inline Bool collide(const Sphere& a, const Plane& b)
+{
+	return collide(b, a);
+}
+inline Bool collide(const Sphere& a, const Ray& b)
+{
+	return collide(b, a);
+}
+extern Bool collide(const Sphere& a, const Sphere& b);
+extern Bool collide(const Sphere& a, const Aabb& b);
+
+// 7th line (AABB)
+inline Bool collide(const Aabb& a, const LineSegment& b)
+{
+	return collide(b, a);
+}
+inline Bool collide(const Aabb& a, const Obb& b)
+{
+	return collide(b, a);
+}
+inline Bool collide(const Aabb& a, const Frustum& b)
+{
+	return collide(b, a);
+}
+inline Bool collide(const Aabb& a, const Plane& b)
+{
+	return collide(b, a);
+}
+inline Bool collide(const Aabb& a, const Ray& b)
+{
+	return collide(b, a);
+}
+inline Bool collide(const Aabb& a, const Sphere& b)
+{
+	return collide(b, a);
+}
+extern Bool collide(const Aabb& a, const Aabb& b);
+
+/// @}
+/// @}
+
+} // end namespace detail
+} // end namespace anki
+
+#endif

+ 0 - 177
include/anki/collision/CollisionAlgorithmsMatrix.h

@@ -1,177 +0,0 @@
-#ifndef ANKI_COLLISION_COLLISION_ALGORITHMS_MATRIX_H
-#define ANKI_COLLISION_COLLISION_ALGORITHMS_MATRIX_H
-
-#include "anki/collision/Forward.h"
-#include "anki/util/StdTypes.h"
-
-namespace anki {
-
-/// @addtogroup Collision
-/// @{
-
-/// Provides the collision algorithms that detect collision between collision
-/// shapes
-///
-/// @code
-/// +------+------+------+------+------+------+------+------+
-/// |      | LS   | OBB  | FRU  | P    | R    | S    | AABB |
-/// +------+------+------+------+------+------+------+------+
-/// | LS   | N/A  | OK   | N/I  | OK   | N/A  | OK   | OK   |
-/// +------+------+------+------+------+------+------+------+
-/// | OBB  |      | OK   | N/I  | OK   | OK   | OK   | OK   |
-/// +------+------+------+------+------+------+------+------+
-/// | FRU  |      |      | N/I  | N/I  | N/I  | N/I  | N/I  |
-/// +------+------+------+------+------+------+------+------+
-/// | P    |      |      |      | OK   | OK   | OK   | OK   |
-/// +------+------+------+------+------+------+------+------+
-/// | R    |      |      |      |      | N/A  | OK   | OK   |
-/// +------+------+------+------+------+------+------+------+
-/// | S    |      |      |      |      |      | OK   | OK   |
-/// +------+------+------+------+------+------+------+------+
-/// | AABB |      |      |      |      |      |      | OK   |
-/// +------+------+------+------+------+------+------+------+
-/// @endcode
-class CollisionAlgorithmsMatrix
-{
-public:
-	typedef LineSegment Ls;
-
-	/// Generic collide function. It doesn't uses visitor pattern for
-	/// speed reasons
-	static Bool collide(const CollisionShape& a, const CollisionShape& b);
-
-	// 1st line (LS)
-	static Bool collide(const Ls& a, const Ls& b);
-	static Bool collide(const Ls& a, const Obb& b);
-	static Bool collide(const Ls& a, const Frustum& b);
-	static Bool collide(const Ls& a, const Plane& b);
-	static Bool collide(const Ls& a, const Ray& b);
-	static Bool collide(const Ls& a, const Sphere& b);
-	static Bool collide(const Ls& a, const Aabb& b);
-
-	// 2nd line (OBB)
-	static Bool collide(const Obb& a, const Ls& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Obb& a, const Obb& b);
-	static Bool collide(const Obb& a, const Frustum& b);
-	static Bool collide(const Obb& a, const Plane& b);
-	static Bool collide(const Obb& a, const Ray& b);
-	static Bool collide(const Obb& a, const Sphere& b);
-	static Bool collide(const Obb& a, const Aabb& b);
-
-	// 3rd line (FRU)
-	static Bool collide(const Frustum& a, const Ls& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Frustum& a, const Obb& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Frustum& a, const Frustum& b);
-	static Bool collide(const Frustum& a, const Plane& b);
-	static Bool collide(const Frustum& a, const Ray& b);
-	static Bool collide(const Frustum& a, const Sphere& b);
-	static Bool collide(const Frustum& a, const Aabb& b);
-
-	// 4th line (P)
-	static Bool collide(const Plane& a, const Ls& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Plane& a, const Obb& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Plane& a,const Frustum& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Plane& a, const Plane& b);
-	static Bool collide(const Plane& a, const Ray& b);
-	static Bool collide(const Plane& a, const Sphere& b);
-	static Bool collide(const Plane& a, const Aabb& b);
-
-	// 5th line (R)
-	static Bool collide(const Ray& a, const Ls& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Ray& a, const Obb& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Ray& a, const Frustum& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Ray& a, const Plane& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Ray& a, const Ray& b);
-	static Bool collide(const Ray& a, const Sphere& b);
-	static Bool collide(const Ray& a, const Aabb& b);
-
-	// 6th line (S)
-	static Bool collide(const Sphere& a, const Ls& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Sphere& a, const Obb& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Sphere& a, const Frustum& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Sphere& a, const Plane& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Sphere& a, const Ray& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Sphere& a, const Sphere& b);
-	static Bool collide(const Sphere& a, const Aabb& b);
-
-	// 7th line (AABB)
-	static Bool collide(const Aabb& a, const Ls& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Aabb& a, const Obb& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Aabb& a, const Frustum& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Aabb& a, const Plane& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Aabb& a, const Ray& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Aabb& a, const Sphere& b)
-	{
-		return collide(b, a);
-	}
-	static Bool collide(const Aabb& a, const Aabb& b);
-
-private:
-	template<typename T>
-	static Bool tcollide(const CollisionShape& a, const CollisionShape& b);
-};
-/// @}
-
-} // end namespace anki
-
-#endif

+ 8 - 0
include/anki/collision/CollisionShape.h

@@ -2,6 +2,7 @@
 #define ANKI_COLLISION_COLLISION_SHAPE
 
 #include "anki/collision/Forward.h"
+#include "anki/collision/CollisionAlgorithms.h"
 #include "anki/math/Forward.h"
 #include "anki/util/StdTypes.h"
 
@@ -72,6 +73,13 @@ public:
 	}
 	/// @}
 
+	/// Check for collision
+	template<typename T>
+	Bool collide(const T& x) const
+	{
+		return detail::collide(*this, x);
+	}
+
 	/// If the collision shape intersects with the plane then the method
 	/// returns 0.0, else it returns the distance. If the distance is < 0.0
 	/// then the collision shape lies behind the plane and if > 0.0 then

+ 7 - 0
include/anki/collision/Frustum.h

@@ -74,6 +74,13 @@ public:
 	}
 	/// @}
 
+	/// Check for collision
+	template<typename T>
+	Bool collide(const T& x) const
+	{
+		return detail::collide(*this, x);
+	}
+
 	/// Implements CollisionShape::accept
 	void accept(MutableVisitor& v)
 	{

+ 7 - 0
include/anki/collision/LineSegment.h

@@ -68,6 +68,13 @@ public:
 	}
 	/// @}
 
+	/// Check for collision
+	template<typename T>
+	Bool collide(const T& x) const
+	{
+		return detail::collide(*this, x);
+	}
+
 	/// Implements CollisionShape::accept
 	void accept(MutableVisitor& v)
 	{

+ 7 - 0
include/anki/collision/Obb.h

@@ -78,6 +78,13 @@ public:
 	}
 	/// @}
 
+	/// Check for collision
+	template<typename T>
+	Bool collide(const T& x) const
+	{
+		return detail::collide(*this, x);
+	}
+
 	/// Implements CollisionShape::accept
 	void accept(MutableVisitor& v)
 	{

+ 7 - 0
include/anki/collision/Plane.h

@@ -81,6 +81,13 @@ public:
 	}
 	/// @}
 
+	/// Check for collision
+	template<typename T>
+	Bool collide(const T& x) const
+	{
+		return detail::collide(*this, x);
+	}
+
 	/// Implements CollisionShape::accept
 	void accept(MutableVisitor& v)
 	{

+ 7 - 0
include/anki/collision/Ray.h

@@ -71,6 +71,13 @@ public:
 	}
 	/// @}
 
+	/// Check for collision
+	template<typename T>
+	Bool collide(const T& x) const
+	{
+		return detail::collide(*this, x);
+	}
+
 	/// Implements CollisionShape::accept
 	void accept(MutableVisitor& v)
 	{

+ 7 - 0
include/anki/collision/Sphere.h

@@ -71,6 +71,13 @@ public:
 	}
 	/// @}
 
+	/// Check for collision
+	template<typename T>
+	Bool collide(const T& x) const
+	{
+		return detail::collide(*this, x);
+	}
+
 	/// Implements CollisionShape::accept
 	void accept(MutableVisitor& v)
 	{

+ 1 - 1
include/anki/math/Euler.h

@@ -6,7 +6,7 @@
 namespace anki {
 
 /// @addtogroup Math
-///
+/// @{
 
 /// Euler angles. Used for rotations. It cannot describe a rotation
 /// accurately though

+ 1 - 1
include/anki/math/Vec2.h

@@ -96,7 +96,7 @@ private:
 	};
 	/// @}
 };
-/// @
+/// @}
 
 static_assert(sizeof(Vec2) == sizeof(F32) * 2, "Incorrect size");
 

+ 7 - 4
include/anki/scene/Sector.h

@@ -11,6 +11,9 @@ class SceneNode;
 class Scene;
 class Sector;
 
+/// @addtogroup Scene
+/// @{
+
 /// 2 way Portal
 struct Portal
 {
@@ -38,7 +41,7 @@ struct Sector
 	}
 };
 
-/// Sector group. This is supposed to represent the whole sceene
+/// Sector group. This is supposed to represent the whole scene
 class SectorGroup
 {
 public:
@@ -50,9 +53,7 @@ public:
 
 	/// Called when a node was moved or a change in shape happened. The node 
 	/// must be Spatial
-	///
-	/// @return false if scene node is out of all sectors.
-	Bool placeSceneNode(SceneNode* sp);
+	void placeSceneNode(SceneNode* sp);
 
 private:
 	Scene* scene; ///< Keep it here to access various allocators
@@ -60,6 +61,8 @@ private:
 	SceneVector<Portal*> portals;
 };
 
+/// @}
+
 } // end namespace anki
 
 #endif

+ 7 - 6
include/anki/util/Memory.h

@@ -10,6 +10,10 @@
 
 namespace anki {
 
+// Forward
+template<typename T>
+class Allocator;
+
 /// @addtogroup util
 /// @{
 /// @addtogroup memory
@@ -70,10 +74,7 @@ private:
 	PtrSize calcAlignSize(PtrSize size) const;
 };
 
-template<typename T>
-class Allocator;
-
-/// Function that imitates the new operator. The function allocates memory for
+/// Functior that imitates the new operator. The functior allocates memory for
 /// a number of elements and calls their constructor. The interesting thing is
 /// that if the elements size is >1 then it allocates size bigger than the
 /// required. The extra chunk is a number that will be used in
@@ -185,12 +186,12 @@ struct DeleteArray
 	}
 };
 
-/// Allocate memory using a constructor
+/// Allocate memory using an allocator
 #define ANKI_NEW(Type_, alloc_, ...) \
 	New<Type_, decltype(alloc_)::rebind<Type_>::other>{}( \
 		1, alloc_, ## __VA_ARGS__)
 
-/// Allocate memory using a constructor
+/// Allocate memory using an allocator
 #define ANKI_NEW_ARRAY(Type_, alloc_, n_, ...) \
 	New<Type_, decltype(alloc_)::rebind<Type_>::other>{}( \
 		n_, alloc_, ## __VA_ARGS__)

+ 54 - 39
src/collision/CollisionAlgorithmsMatrix.cpp → src/collision/CollisionAlgorithms.cpp

@@ -1,67 +1,81 @@
-#include "anki/collision/CollisionAlgorithmsMatrix.h"
+#include "anki/collision/CollisionAlgorithms.h"
 #include "anki/collision/Collision.h"
 #include "anki/util/Assert.h"
 #include "anki/math/Math.h"
 #include <limits>
 
 namespace anki {
+namespace detail {
 
 //==============================================================================
 template<typename T>
-bool CollisionAlgorithmsMatrix::tcollide(const CollisionShape& a,
-	const CollisionShape& b)
+static Bool tcollide(const CollisionShape& a, const CollisionShape& b)
 {
 	const T& t = static_cast<const T&>(a);
-	bool out;
+	Bool out = false;
 
 	switch(b.getCollisionShapeType())
 	{
 	case CollisionShape::CST_LINE_SEG:
 		out = collide(t, static_cast<const LineSegment&>(b));
+		break;
 	case CollisionShape::CST_RAY:
 		out = collide(t, static_cast<const Ray&>(b));
+		break;
 	case CollisionShape::CST_PLANE:
 		out = collide(t, static_cast<const Plane&>(b));
+		break;
 	case CollisionShape::CST_SPHERE:
 		out = collide(t, static_cast<const Sphere&>(b));
+		break;
 	case CollisionShape::CST_AABB:
 		out = collide(t, static_cast<const Aabb&>(b));
+		break;
 	case CollisionShape::CST_OBB:
 		out = collide(t, static_cast<const Obb&>(b));
+		break;
 	case CollisionShape::CST_FRUSTUM:
 		out = collide(t, static_cast<const Frustum&>(b));
+		break;
 	default:
 		ANKI_ASSERT(0 && "Forgot something");
-		out = false;
+		break;
 	}
 
 	return out;
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const CollisionShape& a,
-	const CollisionShape& b)
+Bool collide(const CollisionShape& a, const CollisionShape& b)
 {
-	bool out;
+	Bool out = false;
+
 	switch(a.getCollisionShapeType())
 	{
 	case CollisionShape::CST_LINE_SEG:
 		out = tcollide<LineSegment>(a, b);
+		break;
 	case CollisionShape::CST_RAY:
 		out = tcollide<Ray>(a, b);
+		break;
 	case CollisionShape::CST_PLANE:
 		out = tcollide<Plane>(a, b);
+		break;
 	case CollisionShape::CST_SPHERE:
 		out = tcollide<Sphere>(a, b);
+		break;
 	case CollisionShape::CST_AABB:
 		out = tcollide<Aabb>(a, b);
+		break;
 	case CollisionShape::CST_OBB:
 		out = tcollide<Obb>(a, b);
+		break;
 	case CollisionShape::CST_FRUSTUM:
 		out = tcollide<Frustum>(a, b);
+		break;
 	default:
 		ANKI_ASSERT(0 && "Forgot something");
-		out = false;
+		break;
 	}
 
 	return out;
@@ -72,14 +86,14 @@ bool CollisionAlgorithmsMatrix::collide(const CollisionShape& a,
 //==============================================================================
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Ls& /*a*/, const Ls& /*b*/)
+Bool collide(const LineSegment& /*a*/, const LineSegment& /*b*/)
 {
 	ANKI_ASSERT(0 && "N/A");
 	return false;
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Ls& ls, const Obb& obb)
+Bool collide(const LineSegment& ls, const Obb& obb)
 {
 	F32 maxS = std::numeric_limits<F32>::min();
 	F32 minT = std::numeric_limits<F32>::max();
@@ -141,26 +155,26 @@ bool CollisionAlgorithmsMatrix::collide(const Ls& ls, const Obb& obb)
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Ls& a, const Frustum& b)
+Bool collide(const LineSegment& a, const Frustum& b)
 {
 	return b.insideFrustum(a);
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Ls& ls, const Plane& p)
+Bool collide(const LineSegment& ls, const Plane& p)
 {
 	return ls.testPlane(p) == 0.0;
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Ls& /*a*/, const Ray& /*b*/)
+Bool collide(const LineSegment& /*a*/, const Ray& /*b*/)
 {
 	ANKI_ASSERT(0 && "N/A");
 	return false;
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Ls& ls, const Sphere& s)
+Bool collide(const LineSegment& ls, const Sphere& s)
 {
 	const Vec3& v = ls.getDirection();
 	Vec3 w0 = s.getCenter() - ls.getOrigin();
@@ -186,7 +200,7 @@ bool CollisionAlgorithmsMatrix::collide(const Ls& ls, const Sphere& s)
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Ls& ls, const Aabb& aabb)
+Bool collide(const LineSegment& ls, const Aabb& aabb)
 {
 	F32 maxS = std::numeric_limits<F32>::min();
 	F32 minT = std::numeric_limits<F32>::max();
@@ -245,7 +259,7 @@ bool CollisionAlgorithmsMatrix::collide(const Ls& ls, const Aabb& aabb)
 //==============================================================================
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Obb& o0, const Obb& o1)
+Bool collide(const Obb& o0, const Obb& o1)
 {
 	// extent vectors
 	const Vec3& a = o0.getExtend();
@@ -253,7 +267,7 @@ bool CollisionAlgorithmsMatrix::collide(const Obb& o0, const Obb& o1)
 
 	// test factors
 	F32 cTest, aTest, bTest;
-	bool parallelAxes = false;
+	Bool parallelAxes = false;
 
 	// transpose of rotation of B relative to A, i.e. (R_b^T * R_a)^T
 	Mat3 rt = o0.getRotation().getTransposed() * o1.getRotation();
@@ -424,19 +438,19 @@ bool CollisionAlgorithmsMatrix::collide(const Obb& o0, const Obb& o1)
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Obb& a, const Frustum& b)
+Bool collide(const Obb& a, const Frustum& b)
 {
 	return b.insideFrustum(a);
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Obb& a, const Plane& b)
+Bool collide(const Obb& a, const Plane& b)
 {
 	return a.testPlane(b) == 0.0;
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Obb& obb, const Ray& r)
+Bool collide(const Obb& obb, const Ray& r)
 {
 	Aabb aabb_(-obb.getExtend(), obb.getExtend());
 	Ray newray;
@@ -449,7 +463,7 @@ bool CollisionAlgorithmsMatrix::collide(const Obb& obb, const Ray& r)
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Obb& obb, const Sphere& s)
+Bool collide(const Obb& obb, const Sphere& s)
 {
 	Aabb aabb_(-obb.getExtend(), obb.getExtend()); // aabb_ is in "this" frame
 	Vec3 newCenter = obb.getRotation().getTransposed() 
@@ -460,7 +474,7 @@ bool CollisionAlgorithmsMatrix::collide(const Obb& obb, const Sphere& s)
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Obb& obb, const Aabb& aabb)
+Bool collide(const Obb& obb, const Aabb& aabb)
 {
 	Vec3 center_ = (aabb.getMax() + aabb.getMin()) * 0.5;
 	Vec3 extends_ = (aabb.getMax() - aabb.getMin()) * 0.5;
@@ -474,31 +488,31 @@ bool CollisionAlgorithmsMatrix::collide(const Obb& obb, const Aabb& aabb)
 //==============================================================================
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Frustum& a, const Frustum& b)
+Bool collide(const Frustum& a, const Frustum& b)
 {
 	return b.insideFrustum(a);
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Frustum& a, const Plane& b)
+Bool collide(const Frustum& a, const Plane& b)
 {
 	return a.insideFrustum(b);
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Frustum& a, const Ray& b)
+Bool collide(const Frustum& a, const Ray& b)
 {
 	return a.insideFrustum(b);
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Frustum& a, const Sphere& b)
+Bool collide(const Frustum& a, const Sphere& b)
 {
 	return a.insideFrustum(b);
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Frustum& a, const Aabb& b)
+Bool collide(const Frustum& a, const Aabb& b)
 {
 	return a.insideFrustum(b);
 }
@@ -508,25 +522,25 @@ bool CollisionAlgorithmsMatrix::collide(const Frustum& a, const Aabb& b)
 //==============================================================================
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Plane& p0, const Plane& p1)
+Bool collide(const Plane& p0, const Plane& p1)
 {
 	return p0.getNormal() != p1.getNormal();
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Plane& p, const Ray& r)
+Bool collide(const Plane& p, const Ray& r)
 {
 	return r.testPlane(p) == 0.0;
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Plane& p, const Sphere& s)
+Bool collide(const Plane& p, const Sphere& s)
 {
 	return s.testPlane(p) == 0.0;
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Plane& p, const Aabb& aabb)
+Bool collide(const Plane& p, const Aabb& aabb)
 {
 	return aabb.testPlane(p) == 0.0;
 }
@@ -536,14 +550,14 @@ bool CollisionAlgorithmsMatrix::collide(const Plane& p, const Aabb& aabb)
 //==============================================================================
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Ray& a, const Ray& b)
+Bool collide(const Ray& a, const Ray& b)
 {
 	ANKI_ASSERT(0 && "N/A");
 	return false;
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Ray& r, const Sphere& s)
+Bool collide(const Ray& r, const Sphere& s)
 {
 	Vec3 w(s.getCenter() - r.getOrigin());
 	const Vec3& v = r.getDirection();
@@ -562,7 +576,7 @@ bool CollisionAlgorithmsMatrix::collide(const Ray& r, const Sphere& s)
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Ray& r, const Aabb& aabb)
+Bool collide(const Ray& r, const Aabb& aabb)
 {
 	F32 maxS = std::numeric_limits<F32>::min();
 	F32 minT = std::numeric_limits<F32>::max();
@@ -622,14 +636,14 @@ bool CollisionAlgorithmsMatrix::collide(const Ray& r, const Aabb& aabb)
 //==============================================================================
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Sphere& a, const Sphere& b)
+Bool collide(const Sphere& a, const Sphere& b)
 {
 	F32 tmp = a.getRadius() + b.getRadius();
 	return (a.getCenter() - b.getCenter()).getLengthSquared() <= tmp * tmp;
 }
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Sphere& s, const Aabb& aabb)
+Bool collide(const Sphere& s, const Aabb& aabb)
 {
 	const Vec3& c = s.getCenter();
 
@@ -674,7 +688,7 @@ bool CollisionAlgorithmsMatrix::collide(const Sphere& s, const Aabb& aabb)
 //==============================================================================
 
 //==============================================================================
-bool CollisionAlgorithmsMatrix::collide(const Aabb& a, const Aabb& b)
+Bool collide(const Aabb& a, const Aabb& b)
 {
 	// if separated in x direction
 	if(a.getMin().x() > b.getMax().x() || b.getMin().x() > a.getMax().x())
@@ -698,4 +712,5 @@ bool CollisionAlgorithmsMatrix::collide(const Aabb& a, const Aabb& b)
 	return true;
 }
 
-} // end namespace
+} // end namespace detail
+} // end namespace anki

+ 1 - 2
src/scene/Octree.cpp

@@ -4,7 +4,6 @@
 #include "anki/scene/Light.h"
 #include "anki/util/Exception.h"
 #include "anki/core/Logger.h"
-#include "anki/collision/CollisionAlgorithmsMatrix.h"
 
 namespace anki {
 
@@ -124,7 +123,7 @@ void Octree::placeSceneNode(SceneNode* sn)
 //==============================================================================
 OctreeNode* Octree::place(const Aabb& aabb)
 {
-	if(CollisionAlgorithmsMatrix::collide(aabb, root.aabb))
+	if(aabb.collide(root.aabb))
 	{
 		// Run the recursive method
 		return placeInternal(aabb, 0, root);

+ 43 - 9
src/scene/Sector.cpp

@@ -2,7 +2,7 @@
 #include "anki/scene/Spatial.h"
 #include "anki/scene/SceneNode.h"
 #include "anki/scene/Scene.h"
-#include "anki/collision/CollisionAlgorithmsMatrix.h"
+#include "anki/core/Logger.h"
 
 namespace anki {
 
@@ -30,8 +30,7 @@ Bool Sector::placeSceneNode(SceneNode* sn)
 {
 	// XXX Optimize
 
-	if(!CollisionAlgorithmsMatrix::collide(sn->getSpatial()->getAabb(),
-		octree.getRoot().getAabb()))
+	if(!sn->getSpatial()->getAabb().collide(octree.getRoot().getAabb()))
 	{
 		return false;
 	}
@@ -66,16 +65,51 @@ SectorGroup::~SectorGroup()
 }
 
 //==============================================================================
-Bool SectorGroup::placeSceneNode(SceneNode* sp)
+void SectorGroup::placeSceneNode(SceneNode* sn)
 {
-	// Find the candidates first. Sectors overlap
-	SceneVector<Sector*> placeSectors(scene->getFrameAllocator());
-	for(Sector* sector : sectors)
+	ANKI_ASSERT(sn != nullptr);
+	Spatial* sp = sn->getSpatial();
+	ANKI_ASSERT(sp);
+	const Aabb& spAabb = sp->getAabb();
+
+	// Find the candidates first. Sectors overlap, chose the smaller(??!!??)
+	Sector* sector = nullptr;
+	for(Sector* s : sectors)
 	{
-		
+		// Spatial inside the sector?
+		if(s->getAabb().collide(spAabb))
+		{
+			// No other candidate?
+			if(sector == nullptr)
+			{
+				sector = s;
+			}
+			else
+			{
+				// Other candidata so chose the smaller
+				F32 lengthSqA = (sector->getAabb().getMax()
+					- sector->getAabb().getMin()).getLengthSquared();
+
+				F32 lengthSqB = (s->getAabb().getMax()
+					- s->getAabb().getMin()).getLengthSquared();
+
+				if(lengthSqB < lengthSqA)
+				{
+					sector = s;
+				}
+			}
+		}
 	}
 
-	return false;
+	// Ask Octree to place it
+	if(sector != nullptr)
+	{
+		sector->octree.placeSceneNode(sn);
+	}
+	else
+	{
+		ANKI_LOGW("Spatial outside all sectors");
+	}
 }
 
 } // end namespace anki