diff --git a/src/main.cpp b/src/main.cpp index 25d24f3..0687c27 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -78,6 +78,7 @@ void TestGame::Init(const Window& window) #include "boundingSphere.h" #include "aabb.h" +#include "plane.h" #include int main() @@ -124,6 +125,27 @@ int main() << ", Distance: " << aabb1Intersectaabb5.GetDistance() << std::endl; + std::cout << std::endl; + + Plane plane1(Vector3f(0.0f, 1.0f, 0.0f), 0.0f); + + IntersectData plane1IntersectSphere1 = plane1.IntersectSphere(sphere1); + IntersectData plane1IntersectSphere2 = plane1.IntersectSphere(sphere2); + IntersectData plane1IntersectSphere3 = plane1.IntersectSphere(sphere3); + IntersectData plane1IntersectSphere4 = plane1.IntersectSphere(sphere4); + + std::cout << "Plane1 intersect Sphere1: " << plane1IntersectSphere1.GetDoesIntersect() + << ", Distance: " << plane1IntersectSphere1.GetDistance() << std::endl; + + std::cout << "Plane1 intersect Sphere2: " << plane1IntersectSphere2.GetDoesIntersect() + << ", Distance: " << plane1IntersectSphere2.GetDistance() << std::endl; + + std::cout << "Plane1 intersect Sphere3: " << plane1IntersectSphere3.GetDoesIntersect() + << ", Distance: " << plane1IntersectSphere3.GetDistance() << std::endl; + + std::cout << "Plane1 intersect Sphere4: " << plane1IntersectSphere4.GetDoesIntersect() + << ", Distance: " << plane1IntersectSphere4.GetDistance() << std::endl; + // TestGame game; // Window window(800, 600, "3D Game Engine"); // RenderingEngine renderer(window); diff --git a/src/plane.cpp b/src/plane.cpp new file mode 100644 index 0000000..197370b --- /dev/null +++ b/src/plane.cpp @@ -0,0 +1,56 @@ +/* + * @file + * @author Benny Bobaganoosh + * @section LICENSE + * + * Copyright (C) 2014 Benny Bobaganoosh + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "plane.h" + +Plane Plane::Normalized() const +{ + float magnitude = m_normal.Length(); + + //Dividing the normal by it's length performs vector normalization. + //Distance however, must also be divided by the normal's length in + //order to create an equivalent plane. + return Plane(m_normal/magnitude, m_distance/magnitude); +} + +IntersectData Plane::IntersectSphere(const BoundingSphere& other) const +{ + //Calculating the dot product between the Plane's normal and the Sphere's + //center gets how far the sphere's center is along the Plane's normal. + // + //Adding the distance adjusts this value based on how far the Plane itself + //is along the normal. + // + //The end result of this is how far the Sphere's center is from the Plane. + //The absolute value is taken so that this result is always positive. + float distanceFromSphereCenter = + (float)fabs(m_normal.Dot(other.GetCenter()) + m_distance); + + //As long as the distanceFromSphereCenter is valid and positive, then + //the distance from the sphere can be calculated simply by subtracting + //it's radius. + float distanceFromSphere = distanceFromSphereCenter - other.GetRadius(); + + //The only time the plane can be intersecting the sphere is if the sphere + //has less than 0 distance from the plane. Otherwise, if there is distance + //between the plane and sphere, then there must be a gap between the + //plane and sphere, and they cannot be intersecting. + return IntersectData(distanceFromSphere < 0, distanceFromSphere); +} diff --git a/src/plane.h b/src/plane.h new file mode 100644 index 0000000..9cc7776 --- /dev/null +++ b/src/plane.h @@ -0,0 +1,69 @@ +/* + * @file + * @author Benny Bobaganoosh + * @section LICENSE + * + * Copyright (C) 2014 Benny Bobaganoosh + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PLANE_INCLUDED_H +#define PLANE_INCLUDED_H + +#include "math3d.h" +#include "boundingSphere.h" + +/** + * The Plane class represents an infinitely large plane that can be used as + * a collider in a physics engine. + */ +class Plane +{ +public: + /** + * Creates a Plane in a usable state. + * + * @param normal The "up" direction from the plane's surface. + * @param distance The distance to the plane from the world origin + * along the normal + */ + Plane(const Vector3f& normal, float distance) : + m_normal(normal), + m_distance(distance) {} + + /** + * Creates an equivalent plane with a normal at unit length and distance + * adjusted accordingly. + */ + Plane Normalized() const; + + /** + * Computes information about if this Plane intersects a Sphere. + * + * @param other The Sphere that's being tested for intersection with this + * Plane. + */ + IntersectData IntersectSphere(const BoundingSphere& other) const; + + inline const Vector3f& GetNormal() const { return m_normal; } + inline float GetDistance() const { return m_distance; } +private: + /** The "up" direction from the plane's surface. */ + const Vector3f m_normal; + /** The distance to the plane from the world origin along the normal */ + const float m_distance; +}; + +#endif +