afMathTool.h
资源名称:AirForce.rar [点击查看]
上传用户:kaiguan
上传日期:2007-10-28
资源大小:1074k
文件大小:5k
源码类别:
其他游戏
开发平台:
Visual C++
- #ifndef AF_MATHTOOL
- #define AF_MATHTOOL
- #define afFLOAT_MIN 1.175494351e-38f
- #define afFLOAT_MAX 3.402823466e+38f
- #define afINT_MIN -(2147483647+1)
- #define afINT_MAX 2147483647
- #define afEPS 0.00001f
- #define afEPSL 0.05f
- #define afFPI 3.14159265f
- #define afFTWOPI 6.28318531f
- #define afFTWOPIRAND afFTWOPI/RAND_MAX
- #define afDECTORAD afFPI/180.0f
- #define af360RAND 360.0f/RAND_MAX
- class afPlane;
- class afVec3;
- class afVec3n;
- /// afIMathTool provides mathematical routines for 3d calculations
- class afMathTool
- {
- public:
- /// Initializes the tables used by sin() and cos()
- /**
- * This method must be called before sin() or cos() can be called.
- */
- static void initSinCos();
- /// Initializes the tables used by sqrt()
- /**
- * This method must be called before sqrt() can be called.
- */
- static void initSqrt();
- /// Uses a sine table for fast sinus calculation
- /**
- * Returns the sine of nVal
- * CAUTION: nVal has to be passed in degrees
- */
- static float sin(float nVal);
- /// Uses a cosine table for fast calculation
- /**
- * Returns the cosine of nVal
- * CAUTION: nVal has to be passed in degrees
- */
- static float cos(float nVal);
- /// Uses a sqrt table for fast sqrt calculation
- static float sqrt(float n);
- /// Calculates a random number that lies between nMin and nMax
- static float random(float nMin, float nMax);
- /// Returns true if nPoint lies in the sphere
- static bool
- isPointInSphere(const afVec3& nPoint, const afVec3& nSpherePos, float nRadius);
- /// Returns true if the point lies inside the triangle
- /**
- * It's assumed that the point always lies in the triangles plane
- */
- static bool
- isPointInTriangle(const afVec3& nPoint, const afVec3& nA, const afVec3& nB, const afVec3& nC);
- /// Returns the closest point from nPoint on the given line
- static afVec3
- getClosestPointOnLine(const afVec3& nPoint, const afVec3& nA, const afVec3& nB);
- /// Returns the closest point from nPoint which lies on the triangles edges.
- /**
- * It's assumed that nPoint lies NOT inside the triangle
- */
- static afVec3
- getClosestPointOnTriangle(const afVec3& nPoint, const afVec3& nA, const afVec3& nB, const afVec3& nC);
- /// Returns the signed distance of the given position to the plane
- static float
- getDistanceToPlane(const afVec3& nPos, const afPlane& nPlane);
- /// Returns true if a point lies behind a plane (planes are always directed)
- static bool
- isPointBehindPlane(const afVec3& nPos, const afPlane& nPlane);
- /// Returns true if a point lies behind all planes
- static bool
- isPointBehindPlanes(const afVec3& nPos, int nNumPlanes, const afPlane* nPlanes);
- /// Returns true if a point lies on the plane
- static bool
- isPointOnPlane(const afVec3& nPos, const afPlane& nPlane);
- /// Returns true if at least one of the points lies behind the plane
- static bool
- arePointsBehindPlane(int numPos, const afVec3* nPos, const afPlane& nPlane);
- /// Finds the intersection of a ray and a sphere
- /**
- * Returns the length of the ray in nT
- * Returns true if the ray really hit the sphere
- */
- static bool
- findIntersectionRaySphere(const afVec3& nPos0, const afVec3n& nDir0, const afVec3& nSpherePos, float nRadius, float& nT);
- /// Finds the intersection of a ray and a plane.
- /**
- * Returns the position in parameter pos and signed distance of nPos0 and pos in nT
- * Returns true as return value if there was an intersection
- */
- static bool
- findIntersectionRayPlane(const afPlane& nPlane, const afVec3& nPos0, const afVec3n& nDir, afVec3& nPos, float& nT);
- /// Finds the intersection of three planes
- /** Returns the point in nPoint
- * Returns true if nPoint is valid
- */
- static bool
- findIntersectionPlanes(const afPlane& nPlane1, const afPlane& nPlane2, const afPlane& nPlane3, afVec3& nPoint);
- /// Finds the intersection of the given sphere moving into direction nDir with the given triangle.
- /**
- * If the sphere did already collide with the triangle nStuck is set to true
- * If nPlane is not NULL it must lie inside the triangle
- * Returns: the distance the sphere moves until it collides
- * the position on the sphere, that will collide with the triangle
- * the position on the triangle that will collide with the sphere
- * sets nStuck to true if there was already a collision on the inital position
- */
- static bool
- findIntersectionSphereTriangle(const afVec3& nSpherePos, float nRadius, const afVec3& nDir,
- const afVec3& nCorner1, const afVec3& nCorner2, const afVec3& nCorner3, const afPlane* nPlane,
- float& nDist, afVec3& nColSpherePos, afVec3& nColTrianglePos, bool& nStuck);
- protected:
- static float *sinTable;
- static float *cosTable;
- static unsigned int *sqrtTable; // declare table of square roots
- };
- #endif