Explorar o código

Finalizing new collision feature

Panagiotis Christopoulos Charitos %!s(int64=14) %!d(string=hai) anos
pai
achega
ad4a2970cb

+ 12 - 0
.cproject

@@ -29,6 +29,9 @@
 									<listOptionValue builtIn="false" value="/usr/include"/>
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/anki/anki}&quot;"/>
 								</option>
+								<option id="gnu.cpp.compiler.option.preprocessor.def.703966797" superClass="gnu.cpp.compiler.option.preprocessor.def" valueType="definedSymbols">
+									<listOptionValue builtIn="false" value="__GXX_EXPERIMENTAL_CXX0X__"/>
+								</option>
 								<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.1705857210" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
 							</tool>
 							<tool id="cdt.managedbuild.tool.gnu.c.compiler.exe.debug.509716139" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.exe.debug">
@@ -38,6 +41,9 @@
 									<listOptionValue builtIn="false" value="/usr/include"/>
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/anki/anki}&quot;"/>
 								</option>
+								<option id="gnu.c.compiler.option.preprocessor.def.symbols.1930194539" superClass="gnu.c.compiler.option.preprocessor.def.symbols" valueType="definedSymbols">
+									<listOptionValue builtIn="false" value="__GXX_EXPERIMENTAL_CXX0X__"/>
+								</option>
 								<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.1888715461" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
 							</tool>
 							<tool id="cdt.managedbuild.tool.gnu.c.linker.exe.debug.1831689831" name="GCC C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.exe.debug"/>
@@ -84,11 +90,17 @@
 							<tool id="cdt.managedbuild.tool.gnu.cpp.compiler.exe.release.1634217479" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.exe.release">
 								<option id="gnu.cpp.compiler.exe.release.option.optimization.level.467488783" name="Optimization Level" superClass="gnu.cpp.compiler.exe.release.option.optimization.level" value="gnu.cpp.compiler.optimization.level.most" valueType="enumerated"/>
 								<option id="gnu.cpp.compiler.exe.release.option.debugging.level.1206763877" name="Debug Level" superClass="gnu.cpp.compiler.exe.release.option.debugging.level" value="gnu.cpp.compiler.debugging.level.none" valueType="enumerated"/>
+								<option id="gnu.cpp.compiler.option.preprocessor.def.1535300050" superClass="gnu.cpp.compiler.option.preprocessor.def" valueType="definedSymbols">
+									<listOptionValue builtIn="false" value="__GXX_EXPERIMENTAL_CXX0X__"/>
+								</option>
 								<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.1897753799" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
 							</tool>
 							<tool id="cdt.managedbuild.tool.gnu.c.compiler.exe.release.1453949923" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.exe.release">
 								<option defaultValue="gnu.c.optimization.level.most" id="gnu.c.compiler.exe.release.option.optimization.level.204107120" name="Optimization Level" superClass="gnu.c.compiler.exe.release.option.optimization.level" valueType="enumerated"/>
 								<option id="gnu.c.compiler.exe.release.option.debugging.level.1402099681" name="Debug Level" superClass="gnu.c.compiler.exe.release.option.debugging.level" value="gnu.c.debugging.level.none" valueType="enumerated"/>
+								<option id="gnu.c.compiler.option.preprocessor.def.symbols.1483582268" superClass="gnu.c.compiler.option.preprocessor.def.symbols" valueType="definedSymbols">
+									<listOptionValue builtIn="false" value="__GXX_EXPERIMENTAL_CXX0X__"/>
+								</option>
 								<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.813305505" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
 							</tool>
 							<tool id="cdt.managedbuild.tool.gnu.c.linker.exe.release.1557937760" name="GCC C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.exe.release"/>

+ 8 - 37
anki/collision/Aabb.cpp

@@ -10,48 +10,19 @@ namespace anki {
 //==============================================================================
 Aabb Aabb::getTransformed(const Transform& transform) const
 {
-	Aabb out;
-
-	// if there is no rotation our job is easy
-	if(transform.getRotation() == Mat3::getIdentity())
+	Mat3 absM;
+	for(int i = 0; i < 9; ++i)
 	{
-		out.min = (min * transform.getScale()) + transform.getOrigin();
-		out.max = (max * transform.getScale()) + transform.getOrigin();
+		absM[i] = fabs(transform.getRotation()[i]);
 	}
-	// if not then we are fucked
-	else
-	{
-		/*boost::array<Vec3, 8> points = {{
-			max,
-			Vec3(min.x(), max.y(), max.z()),
-			Vec3(min.x(), min.y(), max.z()),
-			Vec3(max.x(), min.y(), max.z()),
-			Vec3(max.x(), max.y(), min.z()),
-			Vec3(min.x(), max.y(), min.z()),
-			min,
-			Vec3(max.x(), min.y(), min.z())
-		}};
-
-		for(size_t i = 0; i < points.size(); i++)
-		{
-			points[i].transform(transform);
-		}
-
-		out.set(points);*/
 
-		// Fast way:
+	Vec3 center = (min + max) / 2.0;
+	Vec3 extend = (max - min) / 2.0;
 
-		// obb is the transformed this
-		Vec3 c = (min + max) / 2.0;
-		Obb obb = Obb(c.getTransformed(transform), transform.getRotation(),
-			(max - c) * transform.getScale());
+	Vec3 newC = center.getTransformed(transform);
+	Vec3 newE = absM * (extend * transform.getScale());
 
-		boost::array<Vec3, 8> points;
-		obb.getExtremePoints(points);
-		out.set(points);
-	}
-
-	return out;
+	return Aabb(newC - newE, newC + newE);
 }
 
 

+ 2 - 1
anki/collision/Aabb.h

@@ -87,6 +87,7 @@ public:
 		b = *this;
 	}
 
+	/// XXX Optimize it
 	Aabb getTransformed(const Transform& transform) const;
 
 	/// Get a collision shape that includes this and the given. Its not
@@ -114,7 +115,7 @@ void Aabb::set(const Container& container)
 	ANKI_ASSERT(container.size() >= 1);
 
 	min = container.front();
-	max = container.front();
+	max = min;
 
 	// for all the Vec3s calc the max and min
 	typename Container::const_iterator it = container.begin() + 1;

+ 24 - 8
anki/collision/Frustum.cpp

@@ -1,6 +1,6 @@
 #include "anki/collision/Frustum.h"
 #include "anki/collision/LineSegment.h"
-#include <boost/foreach.hpp>
+#include "anki/collision/Aabb.h"
 
 
 namespace anki {
@@ -23,7 +23,7 @@ Frustum& Frustum::operator=(const Frustum& b)
 //==============================================================================
 bool Frustum::insideFrustum(const CollisionShape& b) const
 {
-	BOOST_FOREACH(const Plane& plane, planes)
+	for(const Plane& plane : planes)
 	{
 		if(b.testPlane(plane) < 0.0)
 		{
@@ -39,9 +39,9 @@ bool Frustum::insideFrustum(const CollisionShape& b) const
 void Frustum::transform(const Transform& trf)
 {
 	// Planes
-	for(uint i = 0; i < planes.size(); ++i)
+	for(Plane& p : planes)
 	{
-		planes[i].transform(trf);
+		p.transform(trf);
 	}
 }
 
@@ -53,7 +53,7 @@ void Frustum::transform(const Transform& trf)
 //==============================================================================
 PerspectiveFrustum& PerspectiveFrustum::operator=(const PerspectiveFrustum& b)
 {
-	Frustum::operator=(*this);
+	Frustum::operator=(b);
 	eye = b.eye;
 	dirs = b.dirs;
 	fovX = b.fovX;
@@ -67,7 +67,7 @@ float PerspectiveFrustum::testPlane(const Plane& p) const
 {
 	float o = 0.0;
 
-	BOOST_FOREACH(const Vec3& dir, dirs)
+	for(const Vec3& dir : dirs)
 	{
 		LineSegment ls(eye, dir);
 		float t = ls.testPlane(p);
@@ -97,13 +97,22 @@ void PerspectiveFrustum::transform(const Transform& trf)
 
 	eye.transform(trf);
 
-	for(uint i = 0; i < dirs.size(); i++)
+	for(Vec3& dir : dirs)
 	{
-		dirs[i] = trf.getRotation() * dirs[i];
+		dir = trf.getRotation() * dir;
 	}
 }
 
 
+//==============================================================================
+void PerspectiveFrustum::getAabb(Aabb& aabb) const
+{
+	aabb.set(dirs);
+	aabb.getMin() += eye;
+	aabb.getMax() += eye;
+}
+
+
 //==============================================================================
 void PerspectiveFrustum::recalculate()
 {
@@ -204,6 +213,13 @@ void OrthographicFrustum::transform(const Transform& trf)
 }
 
 
+//==============================================================================
+void OrthographicFrustum::getAabb(Aabb& aabb) const
+{
+	obb.getAabb(aabb);
+}
+
+
 //==============================================================================
 Mat4 OrthographicFrustum::calculateProjectionMatrix() const
 {

+ 5 - 11
anki/collision/Frustum.h

@@ -5,7 +5,7 @@
 #include "anki/collision/Plane.h"
 #include "anki/collision/Obb.h"
 #include "anki/math/Math.h"
-#include <boost/array.hpp>
+#include <array>
 
 
 namespace anki {
@@ -103,7 +103,7 @@ public:
 
 protected:
 	/// Used to check against the frustum
-	boost::array<Plane, FP_COUNT> planes;
+	std::array<Plane, FP_COUNT> planes;
 
 	/// @name Viewing variables
 	/// @{
@@ -201,16 +201,13 @@ public:
 	Mat4 calculateProjectionMatrix() const;
 
 	/// Implements CollisionShape::getAabb
-	void getAabb(Aabb& b) const
-	{
-		/// XXX
-	}
+	void getAabb(Aabb& aabb) const;
 
 private:
 	/// @name Shape
 	/// @{
 	Vec3 eye; ///< The eye point
-	boost::array<Vec3, 4> dirs; ///< Directions
+	std::array<Vec3, 4> dirs; ///< Directions
 	/// @}
 
 	/// @name Viewing variables
@@ -319,10 +316,7 @@ public:
 	void transform(const Transform& trf);
 
 	/// Implements CollisionShape::getAabb
-	void getAabb(Aabb& b) const
-	{
-		/// XXX
-	}
+	void getAabb(Aabb& aabb) const;
 
 	/// Implements Frustum::calculateProjectionMatrix
 	Mat4 calculateProjectionMatrix() const;

+ 6 - 5
anki/collision/LineSegment.cpp

@@ -1,5 +1,6 @@
 #include "anki/collision/LineSegment.h"
 #include "anki/collision/Plane.h"
+#include "anki/collision/Aabb.h"
 #include <algorithm>
 
 
@@ -52,10 +53,10 @@ LineSegment LineSegment::getTransformed(const Transform& transform) const
 
 
 //==============================================================================
-void LineSegment::getAabb(Aabb& out) const
+void LineSegment::getAabb(Aabb& aabb) const
 {
-	Vec3 min = pls.getOrigin();
-	Vec3 max = pls.getOrigin() + ls.getDirection();
+	Vec3 min = origin;
+	Vec3 max = origin + dir;
 
 	for(uint i = 0; i < 3; ++i)
 	{
@@ -67,8 +68,8 @@ void LineSegment::getAabb(Aabb& out) const
 		}
 	}
 
-	out.setMin(min);
-	out.setMax(max);
+	aabb.setMin(min);
+	aabb.setMax(max);
 }
 
 

+ 21 - 5
anki/collision/Obb.cpp

@@ -1,5 +1,6 @@
 #include "anki/collision/Obb.h"
 #include "anki/collision/Plane.h"
+#include "anki/collision/Aabb.h"
 
 
 namespace anki {
@@ -65,13 +66,13 @@ Obb Obb::getCompoundShape(const Obb& b) const
 {
 	Obb out;
 
-	boost::array<Vec3, 8> points0;
-	boost::array<Vec3, 8> points1;
+	std::array<Vec3, 8> points0;
+	std::array<Vec3, 8> points1;
 
 	getExtremePoints(points0);
 	b.getExtremePoints(points1);
 
-	boost::array<Vec3, 16> points;
+	std::array<Vec3, 16> points;
 	for(uint i = 0; i < 8; i++)
 	{
 		points[i] = points0[i];
@@ -84,7 +85,7 @@ Obb Obb::getCompoundShape(const Obb& b) const
 
 
 //==============================================================================
-void Obb::getExtremePoints(boost::array<Vec3, 8>& points) const
+void Obb::getExtremePoints(std::array<Vec3, 8>& points) const
 {
 	// L: left, R: right, T: top, B: bottom, F: front, B: back
 	enum
@@ -118,7 +119,7 @@ void Obb::getExtremePoints(boost::array<Vec3, 8>& points) const
 	points[RTB] = 2.0 * points[LTF].dot(yAxis) * yAxis - points[LTF];
 	points[RBF] = 2.0 * points[LTF].dot(zAxis) * zAxis - points[LTF];
 
-	boost::array<Vec3, 8>::iterator it = points.begin();
+	std::array<Vec3, 8>::iterator it = points.begin();
 	for(; it != points.end(); ++it)
 	{
 		(*it) += center;
@@ -126,4 +127,19 @@ void Obb::getExtremePoints(boost::array<Vec3, 8>& points) const
 }
 
 
+//==============================================================================
+void Obb::getAabb(Aabb& aabb) const
+{
+	Mat3 absM;
+	for(int i = 0; i < 9; ++i)
+	{
+		absM[i] = fabs(rotation[i]);
+	}
+
+	Vec3 newE = absM * extends;
+
+	aabb = Aabb(center - newE, center + newE);
+}
+
+
 } // end namespace

+ 3 - 6
anki/collision/Obb.h

@@ -3,7 +3,7 @@
 
 #include "anki/collision/CollisionShape.h"
 #include "anki/math/Math.h"
-#include <boost/array.hpp>
+#include <array>
 
 
 namespace anki {
@@ -90,10 +90,7 @@ public:
 	}
 
 	/// Implements CollisionShape::getAabb
-	void getAabb(Aabb& b) const
-	{
-		/// XXX
-	}
+	void getAabb(Aabb& aabb) const;
 
 	Obb getTransformed(const Transform& transform) const;
 
@@ -106,7 +103,7 @@ public:
 	void set(const Container& container);
 
 	/// Get extreme points in 3D space
-	void getExtremePoints(boost::array<Vec3, 8>& points) const;
+	void getExtremePoints(std::array<Vec3, 8>& points) const;
 
 public:
 	/// @name Data

+ 1 - 1
anki/collision/Plane.cpp

@@ -38,7 +38,7 @@ void Plane::setFrom3Points(const Vec3& p0, const Vec3& p1, const Vec3& p2)
 	ANKI_ASSERT(!Math::isZero(normal.getLengthSquared()));
 
 	normal.normalize();
-	offset = normal.dot(p0); // ToDo: correct??
+	offset = normal.dot(p0); // XXX: correct??
 }
 
 

+ 1 - 1
anki/collision/Ray.h

@@ -85,7 +85,7 @@ public:
 	}
 
 	/// Implements CollisionShape::getAabb
-	void getAabb(Aabb& b) const;
+	void getAabb(Aabb& aabb) const;
 
 	Ray getTransformed(const Transform& transform) const;
 

+ 3 - 2
anki/collision/Sphere.cpp

@@ -1,5 +1,6 @@
 #include "anki/collision/Sphere.h"
 #include "anki/collision/Plane.h"
+#include "anki/collision/Aabb.h"
 
 
 namespace anki {
@@ -98,8 +99,8 @@ Sphere Sphere::getCompoundShape(const Sphere& b) const
 //==============================================================================
 void Sphere::getAabb(Aabb& aabb) const
 {
-	aabb.setMin(s.getCenter() - radius);
-	aabb.setMax(s.getCenter() + radius);
+	aabb.setMin(center - radius);
+	aabb.setMax(center + radius);
 }
 
 

+ 2 - 2
anki/math/Transform.inl.h

@@ -167,7 +167,7 @@ inline Transform Transform::getInverse() const
 
 
 // invert
-void Transform::invert()
+inline void Transform::invert()
 {
 	*this = getInverse();
 }
@@ -178,7 +178,7 @@ void Transform::invert()
 //==============================================================================
 
 // Print
-std::ostream& operator<<(std::ostream& s, const Transform& a)
+inline std::ostream& operator<<(std::ostream& s, const Transform& a)
 {
 	s << "o: " << a.origin << "\n" <<
 		"s: " << a.scale << "\n" <<