Przeglądaj źródła

Merge pull request #152 from blackberry-gaming/next-dgough

Next dgough
Steve Grenier 14 lat temu
rodzic
commit
d42f2c0452

+ 25 - 1
gameplay/src/Ray.cpp

@@ -105,7 +105,31 @@ float Ray::intersects(const Frustum& frustum) const
 
 
 float Ray::intersects(const Plane& plane) const
 float Ray::intersects(const Plane& plane) const
 {
 {
-    return plane.intersects(*this);
+    const Vector3& normal = plane.getNormal();
+    // If the origin of the ray is on the plane then the distance is zero.
+    float m = (normal.dot(_origin) + plane.getDistance());
+    if (fabs(m) < MATH_EPSILON)
+    {
+        return 0.0f;
+    }
+
+    float dot = normal.dot(_direction);
+    
+    // If the dot product of the plane's normal and this ray's direction is zero,
+    // then the ray is parallel to the plane and does not intersect it.
+    if ( dot == 0.0f )
+    {
+        return INTERSECTS_NONE;
+    }
+    
+    // Calculate the distance along the ray's direction vector to the point where
+    // the ray intersects the plane (if it is negative the plane is behind the ray).
+    float d = -m / dot;
+    if ( d < 0.0f )
+    {
+        return INTERSECTS_NONE;
+    }
+    return d;
 }
 }
 
 
 void Ray::set(const Vector3& origin, const Vector3& direction)
 void Ray::set(const Vector3& origin, const Vector3& direction)

+ 2 - 1
gameplay/src/Ray.h

@@ -110,7 +110,8 @@ public:
     float intersects(const Frustum& frustum) const;
     float intersects(const Frustum& frustum) const;
 
 
     /**
     /**
-     * Tests whether this ray intersects the specified plane.
+     * Tests whether this ray intersects the specified plane and returns the distance
+     * from the origin of the ray to the plane.
      *
      *
      * @param plane The plane to test intersection with.
      * @param plane The plane to test intersection with.
      * 
      * 

+ 1 - 1
gameplay/src/Vector2.cpp

@@ -139,7 +139,7 @@ float Vector2::distanceSquared(const Vector2& v) const
     return (dx * dx + dy * dy);
     return (dx * dx + dy * dy);
 }
 }
 
 
-float Vector2::dot(const Vector2& v)
+float Vector2::dot(const Vector2& v) const
 {
 {
     return (x * v.x + y * v.y);
     return (x * v.x + y * v.y);
 }
 }

+ 1 - 1
gameplay/src/Vector2.h

@@ -183,7 +183,7 @@ public:
      * 
      * 
      * @return The dot product.
      * @return The dot product.
      */
      */
-    float dot(const Vector2& v);
+    float dot(const Vector2& v) const;
 
 
     /**
     /**
      * Returns the dot product between the specified vectors.
      * Returns the dot product between the specified vectors.

+ 1 - 1
gameplay/src/Vector3.cpp

@@ -203,7 +203,7 @@ float Vector3::distanceSquared(const Vector3& v) const
     return (dx * dx + dy * dy + dz * dz);
     return (dx * dx + dy * dy + dz * dz);
 }
 }
 
 
-float Vector3::dot(const Vector3& v)
+float Vector3::dot(const Vector3& v) const
 {
 {
     return (x * v.x + y * v.y + z * v.z);
     return (x * v.x + y * v.y + z * v.z);
 }
 }

+ 1 - 1
gameplay/src/Vector3.h

@@ -230,7 +230,7 @@ public:
      * 
      * 
      * @return The dot product.
      * @return The dot product.
      */
      */
-    float dot(const Vector3& v);
+    float dot(const Vector3& v) const;
 
 
     /**
     /**
      * Returns the dot product between the specified vectors.
      * Returns the dot product between the specified vectors.

+ 1 - 1
gameplay/src/Vector4.cpp

@@ -204,7 +204,7 @@ float Vector4::distanceSquared(const Vector4& v) const
     return (dx * dx + dy * dy + dz * dz + dw * dw);
     return (dx * dx + dy * dy + dz * dz + dw * dw);
 }
 }
 
 
-float Vector4::dot(const Vector4& v)
+float Vector4::dot(const Vector4& v) const
 {
 {
     return (x * v.x + y * v.y + z * v.z + w * v.w);
     return (x * v.x + y * v.y + z * v.z + w * v.w);
 }
 }

+ 1 - 1
gameplay/src/Vector4.h

@@ -221,7 +221,7 @@ public:
      * 
      * 
      * @return The dot product.
      * @return The dot product.
      */
      */
-    float dot(const Vector4& v);
+    float dot(const Vector4& v) const;
 
 
     /**
     /**
      * Returns the dot product between the specified vectors.
      * Returns the dot product between the specified vectors.