-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* @file | ||
* @author Benny Bobaganoosh <[email protected]> | ||
* @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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* @file | ||
* @author Benny Bobaganoosh <[email protected]> | ||
* @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 | ||
|