afMathTool.cpp
资源名称:AirForce.rar [点击查看]
上传用户:kaiguan
上传日期:2007-10-28
资源大小:1074k
文件大小:9k
源码类别:
其他游戏
开发平台:
Visual C++
- #pragma warning(disable: 4514)
- #include "afMathTool.h"
- #include "afPlane.h"
- #include "afVec3.h"
- #include "afVec3n.h"
- #include <string.h>
- #include <math.h>
- float *afMathTool::sinTable = NULL;
- float *afMathTool::cosTable = NULL;
- unsigned int *afMathTool::sqrtTable = NULL;
- void afMathTool::initSinCos()
- {
- int i;
- if(!sinTable)
- {
- sinTable = new float[3600];
- for(i=0; i<3600; i++)
- sinTable[i] = (float)::sin(i*afFTWOPI/360.0f/10.0f);
- }
- if(!cosTable)
- {
- cosTable = new float[3600];
- for(i=0; i<3600; i++)
- cosTable[i] = (float)::cos(i*afFTWOPI/360.0f/10.0f);
- }
- }
- void afMathTool::initSqrt()
- {
- union //FastSqrtUnion
- {
- float f;
- unsigned int i;
- }s;
- unsigned int i;
- if(!sqrtTable)
- {
- sqrtTable = new unsigned int[0x10000];
- for (i = 0; i <= 0x7FFF; i++)
- {
- s.i = (i << 8) | (0x7F << 23);
- s.f = (float)sqrt(s.f);
- sqrtTable[i + 0x8000] = (s.i & 0x7FFFFF);
- s.i = (i << 8) | (0x80 << 23);
- s.f = (float)sqrt(s.f);
- sqrtTable[i] = (s.i & 0x7FFFFF);
- }
- }
- }
- float afMathTool::sin(float nVal)
- {
- int idx = (int)(10.0f*nVal) % 3600;
- if(idx<0)
- idx += 3600;
- return sinTable[idx];
- }
- float afMathTool::cos(float nVal)
- {
- int idx = (int)(10.0f*nVal) % 3600;
- if(idx<0)
- idx += 3600;
- return cosTable[idx];
- }
- float afMathTool::sqrt(float nVal)
- {
- #define FP_BITS(fp) (*(DWORD *)&(fp))
- if (FP_BITS(nVal) == 0) return 0.0f;
- FP_BITS(nVal) = sqrtTable[(FP_BITS(nVal) >> 8) & 0xFFFF] | ((((FP_BITS(nVal) - 0x3F800000) >> 1) + 0x3F800000) & 0x7F800000);
- return nVal;
- }
- float afMathTool::random(float nMin, float nMax)
- {
- float d = nMax-nMin;
- float r = (rand() * d) / RAND_MAX;
- return nMin + r;
- }
- bool afMathTool::isPointInSphere(const afVec3& nPoint, const afVec3& nSpherePos, float nRadius) {
- float d = (nPoint-nSpherePos).sqrLength();
- return d<=nRadius*nRadius;
- }
- bool afMathTool::isPointInTriangle(const afVec3& nPoint, const afVec3& nA, const afVec3& nB, const afVec3& nC)
- {
- double total_angles = 0.0f;
- afVec3 v1 = nPoint-nA,
- v2 = nPoint-nB,
- v3 = nPoint-nC;
- v1.normalize();
- v2.normalize();
- v3.normalize();
- total_angles += acos(v1*v2);
- total_angles += acos(v2*v3);
- total_angles += acos(v3*v1);
- if(fabs(total_angles-afFTWOPI) <= 0.005)
- return true;
- return false;
- }
- afVec3 afMathTool::getClosestPointOnLine(const afVec3& nPoint, const afVec3& nA, const afVec3& nB)
- {
- // determine t (the length of the vector from 慳