Bladeren bron

Collision

Panagiotis Christopoulos Charitos 15 jaren geleden
bovenliggende
commit
4a3e22efd1

File diff suppressed because it is too large
+ 0 - 0
build/debug/Makefile


+ 1 - 0
src/Collision/Collision.h

@@ -3,6 +3,7 @@
 
 #include "Plane.h"
 #include "Sphere.h"
+#include "Obb.h"
 
 
 #endif

+ 43 - 0
src/Collision/Obb.cpp

@@ -0,0 +1,43 @@
+#include "Obb.h"
+#include "Plane.h"
+
+
+//======================================================================================================================
+// getTransformed                                                                                                      =
+//======================================================================================================================
+Obb Obb::getTransformed(const Transform& transform) const
+{
+	Obb out;
+	out.extends = extends * transform.getScale();
+	out.center = center.getTransformed(transform);
+	out.rotation = transform.getRotation() * rotation;
+	return out;
+}
+
+
+//======================================================================================================================
+// testPlane                                                                                                           =
+//======================================================================================================================
+float Obb::testPlane(const Plane& plane) const
+{
+	Vec3 xNormal = rotation.getTransposed() * plane.getNormal();
+
+	// maximum extent in direction of plane normal
+	float r = fabs(extends.x() * xNormal.x()) + fabs(extends.y() * xNormal.y()) + fabs(extends.z() * xNormal.z());
+	// signed distance between box center and plane
+	float d = plane.test(center);
+
+	// return signed distance
+	if(fabs(d) < r)
+	{
+		return 0.0;
+	}
+	else if(d < 0.0)
+	{
+		return d + r;
+	}
+	else
+	{
+		return d - r;
+	}
+}

+ 58 - 1
src/Collision/Obb.h

@@ -2,14 +2,71 @@
 #define OBB_H
 
 #include "CollisionShape.h"
-#include "Properties.h"
 #include "Math.h"
 
 
+/// Object oriented bounding box
 class Obb: public CollisionShape
 {
+	public:
+		/// @name Constructors
+		/// @{
+		Obb(): CollisionShape(CST_OBB) {}
+		Obb(const Obb& b);
+		Obb(const Vec3& center, const Mat3& rotation, const Vec3& extends);
+		/// @}
 
+		/// @name Accessors
+		/// @{
+		const Vec3& getCenter() const {return center;}
+		Vec3& getCenter() {return center;}
+		void setCenter(const Vec3& c) {center = c;}
+
+		const Mat3& getRotation() const {return rotation;}
+		Mat3& getRotation() {return rotation;}
+		void setCenter(const Mat3& r) {rotation = r;}
+
+		const Vec3& getExtend() const {return extends;}
+		Vec3& getExtend() {return extends;}
+		void setExtend(const Vec3& e) {extends = e;}
+		/// @}
+
+		Obb getTransformed(const Transform& transform) const;
+
+		/// @see CollisionShape::testPlane
+		float testPlane(const Plane& plane) const;
+
+		/// Calculate from a set of points
+		template<typename Container>
+		void set(const Container& container);
+
+	private:
+		/// @name Data
+		/// @{
+		Vec3 center;
+		Mat3 rotation;
+		Vec3 extends;
+		/// @}
 };
 
 
+inline Obb::Obb(const Obb& b):
+	CollisionShape(CST_OBB),
+	center(b.center),
+	rotation(b.rotation),
+	extends(b.extends)
+{}
+
+
+inline Obb::Obb(const Vec3& center_, const Mat3& rotation_, const Vec3& extends_):
+	CollisionShape(CST_OBB),
+	center(center_),
+	rotation(rotation_),
+	extends(extends_)
+{}
+
+
+#include "Obb.inl.h"
+
+
 #endif

+ 36 - 0
src/Collision/Obb.inl.h

@@ -0,0 +1,36 @@
+#include <boost/foreach.hpp>
+#include <boost/range/iterator_range.hpp>
+
+
+//======================================================================================================================
+// set                                                                                                                 =
+//======================================================================================================================
+template<typename Container>
+void Obb::set(const Container& container)
+{
+	ASSERT(container.size() >= 1);
+
+	Vec3 min(container.front());
+	Vec3 max(container.front());
+
+	// for all the Vec3s calc the max and min
+	BOOST_FOREACH(const Vec3& v, boost::make_iterator_range(container.begin() + 1, container.end()))
+	{
+		for(int j = 0; j < 3; j++)
+		{
+			if(v[j] > max[j])
+			{
+				max[j] = v[j];
+			}
+			else if(v[j] < min[j])
+			{
+				min[j] = v[j];
+			}
+		}
+	}
+
+	// set the locals
+	center = (max + min) / 2.0;
+	rotation = Mat3::getIdentity();
+	extends = max - center;
+}

+ 17 - 4
src/Collision/Plane.h

@@ -3,15 +3,11 @@
 
 #include "CollisionShape.h"
 #include "Math.h"
-#include "Properties.h"
 
 
 /// Plane collision shape
 class Plane: public CollisionShape
 {
-	PROPERTY_RW(Vec3, normal, getNormal, setNormal)
-	PROPERTY_RW(float, offset, getOffset, setOffset)
-
 	public:
 		/// Default constructor
 		Plane(): CollisionShape(CST_PLANE) {}
@@ -28,6 +24,17 @@ class Plane: public CollisionShape
 		/// @see setFromPlaneEquation
 		Plane(float a, float b, float c, float d);
 
+		/// @name Accessors
+		/// @{
+		const Vec3& getNormal() const {return normal;}
+		Vec3& getNormal() {return normal;}
+		void setNormal(const Vec3& n) {normal = n;}
+
+		float getOffset() const {return offset;}
+		float& getOffset() {return offset;}
+		void setOffset(float o) {offset = o;}
+		/// @}
+
 		/// Return the transformed
 		Plane getTransformed(const Vec3& translate, const Mat3& rotate, float scale) const;
 
@@ -46,6 +53,12 @@ class Plane: public CollisionShape
 		float testPlane(const Plane&) const {return 0.0;}
 
 	private:
+		/// @name Data
+		/// @{
+		Vec3 normal;
+		float offset;
+		/// @}
+
 		/// Set the plane from 3 points
 		void setFrom3Points(const Vec3& p0, const Vec3& p1, const Vec3& p2);
 

+ 1 - 4
src/Collision/Sphere.h

@@ -37,10 +37,7 @@ class Sphere: public CollisionShape
 		/// @see CollisionShape::testPlane
 		float testPlane(const Plane& plane) const;
 
-		/// Set from a container containing Vec3s
-		/// @param pointer The start of the array
-		/// @param stride The space between the elements
-		/// @param count The number of 3D vectors
+		/// Calculate from a set of points
 		template<typename Container>
 		void set(const Container& container);
 

+ 1 - 1
src/Scene/Camera.h

@@ -105,7 +105,7 @@ class Camera: public SceneNode
 		Mat4 invProjectionMat;
 		/// @}
 
-		/// @name Visibility containers
+		/// @name Visible nodes
 		/// @{
 		std::deque<const RenderableNode*> msRenderableNodes;
 		std::deque<const RenderableNode*> bsRenderableNodes;

+ 2 - 2
src/Scene/VisibilityTester.cpp

@@ -121,7 +121,7 @@ void VisibilityTester::getRenderableNodes(bool skipShadowless, Camera& cam)
 
 		node->setVisible(true);
 
-		// If not test every patch individually
+		// If visible test every patch individually
 		BOOST_FOREACH(ModelPatchNode* modelPatchNode, node->getModelPatchNodes())
 		{
 			// Skip shadowless
@@ -133,7 +133,7 @@ void VisibilityTester::getRenderableNodes(bool skipShadowless, Camera& cam)
 			// Test if visible by main camera
 			if(test(*modelPatchNode, cam))
 			{
-				if(modelPatchNode->getCpMtl().isBlendingEnabled())
+				if(modelPatchNode->getCpMtl().renderInBlendingStage())
 				{
 					cam.getVisibleBsRenderableNodes().push_back(modelPatchNode);
 				}

Some files were not shown because too many files changed in this diff