Browse Source

Collision

Panagiotis Christopoulos Charitos 14 years ago
parent
commit
95be1d7b7c
4 changed files with 105 additions and 0 deletions
  1. 0 0
      build/debug/Makefile
  2. 1 0
      src/Collision/Collision.h
  3. 47 0
      src/Collision/Ray.cpp
  4. 57 0
      src/Collision/Ray.h

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


+ 1 - 0
src/Collision/Collision.h

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

+ 47 - 0
src/Collision/Ray.cpp

@@ -0,0 +1,47 @@
+#include "Ray.h"
+#include "Plane.h"
+
+
+//======================================================================================================================
+// getTransformed                                                                                                      =
+//======================================================================================================================
+Ray Ray::getTransformed(const Transform& transform) const
+{
+	Ray out;
+	out.origin = origin.getTransformed(transform);
+	out.dir = transform.getRotation() * dir;
+	return out;
+}
+
+
+//======================================================================================================================
+// testPlane                                                                                                           =
+//======================================================================================================================
+float Ray::testPlane(const Plane& plane) const
+{
+	float dist = plane.test(origin);
+	float cos_ = plane.getNormal().dot(dir);
+
+	if(cos_ > 0.0) // the ray points to the same half-space as the plane
+	{
+		if(dist < 0.0) // the ray's origin is behind the plane
+		{
+			return 0.0;
+		}
+		else
+		{
+			return dist;
+		}
+	}
+	else
+	{
+		if(dist > 0.0)
+		{
+			return 0.0;
+		}
+		else
+		{
+			return dist;
+		}
+	}
+}

+ 57 - 0
src/Collision/Ray.h

@@ -0,0 +1,57 @@
+#ifndef RAY_H
+#define RAY_H
+
+#include "CollisionShape.h"
+#include "Math.h"
+#include "Accessors.h"
+
+
+class Plane;
+
+
+/// Ray collision shape. It has a starting point but the end extends to infinity
+class Ray: public CollisionShape
+{
+	public:
+		/// Default constructor
+		Ray(): CollisionShape(CST_RAY) {}
+
+		/// Copy constructor
+		Ray(const Ray& other);
+
+		/// Constructor
+		Ray(const Vec3& origin, const Vec3& direction);
+
+		/// @name Accessors
+		/// @{
+		GETTER_SETTER(Vec3, origin, getOrigin, setOrigin)
+		GETTER_SETTER(Vec3, dir, getDirection, setDirection)
+		/// @}
+
+		Ray getTransformed(const Transform& transform) const;
+
+		/// Implements CollisionShape::testPlane
+		/// @see CollisionShape::testPlane
+		float testPlane(const Plane& plane) const;
+
+	private:
+		Vec3 origin;
+		Vec3 dir;
+};
+
+
+inline Ray::Ray(const Ray& other):
+	CollisionShape(CST_RAY),
+	origin(other.origin),
+	dir(other.dir)
+{}
+
+
+inline Ray::Ray(const Vec3& origin_, const Vec3& direction_):
+	CollisionShape(CST_RAY),
+	origin(origin_),
+	dir(direction_)
+{}
+
+
+#endif

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