📄 3dmath.cpp
字号:
//***********************************************************************//
// //
// - "Talk to me like I'm a 3 year old!" Programming Lessons - //
// //
// $Author: DigiBen digiben@gametutorials.com //
// //
// $Program: CameraWorldCollision //
// //
// $Description: Shows how to check if camera and world collide //
// //
// $Date: 1/23/02 //
// //
//***********************************************************************//
#include <math.h> // We include math.h so we can use the sqrt() function
#include <float.h> // This is so we can use _isnan() for acos()
#include "3DMath.h"
/////////////////////////////////////// ABSOLUTE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This returns the absolute value of the number passed in
/////
/////////////////////////////////////// ABSOLUTE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
float Absolute(float num)
{
// If num is less than zero, we want to return the absolute value of num.
// This is simple, either we times num by -1 or subtract it from 0.
if(num < 0)
return (0 - num);
// Return the original number because it was already positive
return num;
}
/////////////////////////////////////// CROSS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This returns a perpendicular vector from 2 given vectors by taking the cross product.
/////
/////////////////////////////////////// CROSS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
CVector3 Cross(CVector3 vVector1, CVector3 vVector2)
{
CVector3 vNormal; // The vector to hold the cross product
// The X value for the vector is: (V1.y * V2.z) - (V1.z * V2.y) // Get the X value
vNormal.x = ((vVector1.y * vVector2.z) - (vVector1.z * vVector2.y));
// The Y value for the vector is: (V1.z * V2.x) - (V1.x * V2.z)
vNormal.y = ((vVector1.z * vVector2.x) - (vVector1.x * vVector2.z));
// The Z value for the vector is: (V1.x * V2.y) - (V1.y * V2.x)
vNormal.z = ((vVector1.x * vVector2.y) - (vVector1.y * vVector2.x));
return vNormal; // Return the cross product (Direction the polygon is facing - Normal)
}
/////////////////////////////////////// MAGNITUDE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This returns the magnitude of a normal (or any other vector)
/////
/////////////////////////////////////// MAGNITUDE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
float Magnitude(CVector3 vNormal)
{
// This will give us the magnitude or "Norm" as some say, of our normal.
// Here is the equation: magnitude = sqrt(V.x^2 + V.y^2 + V.z^2) Where V is the vector
return (float)sqrt( (vNormal.x * vNormal.x) + (vNormal.y * vNormal.y) + (vNormal.z * vNormal.z) );
}
/////////////////////////////////////// NORMALIZE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This returns a normalize vector (A vector exactly of length 1)
/////
/////////////////////////////////////// NORMALIZE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
CVector3 Normalize(CVector3 vNormal)
{
float magnitude = Magnitude(vNormal); // Get the magnitude of our normal
// Now that we have the magnitude, we can divide our normal by that magnitude.
// That will make our normal a total length of 1. This makes it easier to work with too.
vNormal.x /= magnitude; // Divide the X value of our normal by it's magnitude
vNormal.y /= magnitude; // Divide the Y value of our normal by it's magnitude
vNormal.z /= magnitude; // Divide the Z value of our normal by it's magnitude
// Finally, return our normalized normal.
return vNormal; // Return the new normal of length 1.
}
/////////////////////////////////////// NORMAL \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This returns the normal of a polygon (The direction the polygon is facing)
/////
/////////////////////////////////////// NORMAL \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
CVector3 Normal(CVector3 vPolygon[])
{ // Get 2 vectors from the polygon (2 sides), Remember the order!
CVector3 vVector1 = vPolygon[2] - vPolygon[0];
CVector3 vVector2 = vPolygon[1] - vPolygon[0];
CVector3 vNormal = Cross(vVector1, vVector2); // Take the cross product of our 2 vectors to get a perpendicular vector
// Now we have a normal, but it's at a strange length, so let's make it length 1.
vNormal = Normalize(vNormal); // Use our function we created to normalize the normal (Makes it a length of one)
return vNormal; // Return our normal at our desired length
}
/////////////////////////////////// DISTANCE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This returns the distance between 2 3D points
/////
/////////////////////////////////// DISTANCE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
float Distance(CVector3 vPoint1, CVector3 vPoint2)
{
// This is the classic formula used in beginning algebra to return the
// distance between 2 points. Since it's 3D, we just add the z dimension:
//
// Distance = sqrt( (P2.x - P1.x)^2 + (P2.y - P1.y)^2 + (P2.z - P1.z)^2 )
//
double distance = sqrt( (vPoint2.x - vPoint1.x) * (vPoint2.x - vPoint1.x) +
(vPoint2.y - vPoint1.y) * (vPoint2.y - vPoint1.y) +
(vPoint2.z - vPoint1.z) * (vPoint2.z - vPoint1.z) );
// Return the distance between the 2 points
return (float)distance;
}
////////////////////////////// CLOSEST POINT ON LINE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This returns the point on the line vA_vB that is closest to the point vPoint
/////
////////////////////////////// CLOSEST POINT ON LINE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
CVector3 ClosestPointOnLine(CVector3 vA, CVector3 vB, CVector3 vPoint)
{
// Create the vector from end point vA to our point vPoint.
CVector3 vVector1 = vPoint - vA;
// Create a normalized direction vector from end point vA to end point vB
CVector3 vVector2 = Normalize(vB - vA);
// Use the distance formula to find the distance of the line segment (or magnitude)
float d = Distance(vA, vB);
// Using the dot product, we project the vVector1 onto the vector vVector2.
// This essentially gives us the distance from our projected vector from vA.
float t = Dot(vVector2, vVector1);
// If our projected distance from vA, "t", is less than or equal to 0, it must
// be closest to the end point vA. We want to return this end point.
if (t <= 0)
return vA;
// If our projected distance from vA, "t", is greater than or equal to the magnitude
// or distance of the line segment, it must be closest to the end point vB. So, return vB.
if (t >= d)
return vB;
// Here we create a vector that is of length t and in the direction of vVector2
CVector3 vVector3 = vVector2 * t;
// To find the closest point on the line segment, we just add vVector3 to the original
// end point vA.
CVector3 vClosestPoint = vA + vVector3;
// Return the closest point on the line segment
return vClosestPoint;
}
/////////////////////////////////// PLANE DISTANCE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This returns the distance between a plane and the origin
/////
/////////////////////////////////// PLANE DISTANCE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
float PlaneDistance(CVector3 Normal, CVector3 Point)
{
float distance = 0; // This variable holds the distance from the plane tot he origin
// Use the plane equation to find the distance (Ax + By + Cz + D = 0) We want to find D.
// So, we come up with D = -(Ax + By + Cz)
// Basically, the negated dot product of the normal of the plane and the point. (More about the dot product in another tutorial)
distance = - ((Normal.x * Point.x) + (Normal.y * Point.y) + (Normal.z * Point.z));
return distance; // Return the distance
}
/////////////////////////////////// INTERSECTED PLANE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This checks to see if a line intersects a plane
/////
/////////////////////////////////// INTERSECTED PLANE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
bool IntersectedPlane(CVector3 vPoly[], CVector3 vLine[], CVector3 &vNormal, float &originDistance)
{
float distance1=0, distance2=0; // The distances from the 2 points of the line from the plane
vNormal = Normal(vPoly); // We need to get the normal of our plane to go any further
// Let's find the distance our plane is from the origin. We can find this value
// from the normal to the plane (polygon) and any point that lies on that plane (Any vertex)
originDistance = PlaneDistance(vNormal, vPoly[0]);
// Get the distance from point1 from the plane using: Ax + By + Cz + D = (The distance from the plane)
distance1 = ((vNormal.x * vLine[0].x) + // Ax +
(vNormal.y * vLine[0].y) + // Bx +
(vNormal.z * vLine[0].z)) + originDistance; // Cz + D
// Get the distance from point2 from the plane using Ax + By + Cz + D = (The distance from the plane)
distance2 = ((vNormal.x * vLine[1].x) + // Ax +
(vNormal.y * vLine[1].y) + // Bx +
(vNormal.z * vLine[1].z)) + originDistance; // Cz + D
// Now that we have 2 distances from the plane, if we times them together we either
// get a positive or negative number. If it's a negative number, that means we collided!
// This is because the 2 points must be on either side of the plane (IE. -1 * 1 = -1).
if(distance1 * distance2 >= 0) // Check to see if both point's distances are both negative or both positive
return false; // Return false if each point has the same sign. -1 and 1 would mean each point is on either side of the plane. -1 -2 or 3 4 wouldn't...
return true; // The line intersected the plane, Return TRUE
}
/////////////////////////////////// DOT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This computers the dot product of 2 vectors
/////
/////////////////////////////////// DOT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
float Dot(CVector3 vVector1, CVector3 vVector2)
{
// The dot product is this equation: V1.V2 = (V1.x * V2.x + V1.y * V2.y + V1.z * V2.z)
// In math terms, it looks like this: V1.V2 = ||V1|| ||V2|| cos(theta)
// (V1.x * V2.x + V1.y * V2.y + V1.z * V2.z)
return ( (vVector1.x * vVector2.x) + (vVector1.y * vVector2.y) + (vVector1.z * vVector2.z) );
}
/////////////////////////////////// ANGLE BETWEEN VECTORS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This checks to see if a point is inside the ranges of a polygon
/////
/////////////////////////////////// ANGLE BETWEEN VECTORS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
double AngleBetweenVectors(CVector3 Vector1, CVector3 Vector2)
{
// Get the dot product of the vectors
float dotProduct = Dot(Vector1, Vector2);
// Get the product of both of the vectors magnitudes
float vectorsMagnitude = Magnitude(Vector1) * Magnitude(Vector2) ;
// Get the angle in radians between the 2 vectors
double angle = acos( dotProduct / vectorsMagnitude );
// Here we make sure that the angle is not a -1.#IND0000000 number, which means indefinate
if(_isnan(angle))
return 0;
// Return the angle in radians
return( angle );
}
/////////////////////////////////// INTERSECTION POINT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This returns the intersection point of the line that intersects the plane
/////
/////////////////////////////////// INTERSECTION POINT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
CVector3 IntersectionPoint(CVector3 vNormal, CVector3 vLine[], double distance)
{
CVector3 vPoint, vLineDir; // Variables to hold the point and the line's direction
double Numerator = 0.0, Denominator = 0.0, dist = 0.0;
// 1) First we need to get the vector of our line, Then normalize it so it's a length of 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -