afPathLinear.cpp
资源名称:AirForce.rar [点击查看]
上传用户:kaiguan
上传日期:2007-10-28
资源大小:1074k
文件大小:2k
源码类别:
其他游戏
开发平台:
Visual C++
- #pragma warning(disable: 4514)
- #include "afPathLinear.h"
- #include <stdio.h>
- afPathLinear::afPathLinear() : interpolRot(false)
- {
- type = TYPE_LINEAR;
- }
- bool afPathLinear::load(const char* nFileName)
- {
- //read the path file
- return true;
- }
- afVec3 afPathLinear::getPosition(float nPos)
- {
- afVec3 pos;
- afVec3n dir;
- getPosDir(nPos, pos, dir);
- return pos;
- }
- afVec3n afPathLinear::getDirection(float nPos)
- {
- afVec3 pos;
- afVec3n dir;
- getPosDir(nPos, pos, dir);
- return dir;
- }
- int afPathLinear::getPosDir(float nPos, afVec3& nPosition, afVec3n& nDirection)
- {
- if(nPos<0.0f)
- nPos = 0.0f;
- if(nPos>1.0f)
- nPos = 1.0f;
- // handle some special cases which might make
- // problems later on... (begin & end of path)
- if(nPos==0.0f)
- {
- nPosition = posList[0];
- nDirection = posList[1]-posList[0];
- return 0;
- }
- if(nPos==1.0f)
- {
- nPosition = posList[posList.getSize()-1];
- nDirection = posList[posList.getSize()-1]-posList[posList.getSize()-2];
- return posList.getSize()-1;
- }
- int minIdx=0, midIdx=lenList.getSize()/2, maxIdx=lenList.getSize();
- float minVal=0.0f, midVal=lenList[midIdx], maxVal=1.0f;
- int runs=0;
- // do a binary search (which is in average
- // a lot faster than linear search:
- // ld(maxIdx) instead of maxIdx/2 steps)
- while(maxIdx-minIdx>1)
- {
- if(midVal<nPos)
- {
- minIdx = midIdx;
- minVal = midVal;
- }
- else
- {
- maxIdx = midIdx;
- maxVal = midVal;
- }
- midIdx = (minIdx+maxIdx)/2;
- midVal = lenList[midIdx];
- runs++;
- }
- const afVec3& pos0 = posList[minIdx],
- pos1 = posList[maxIdx];
- float secMin = lenList[minIdx],
- secLen = lenList[maxIdx]-lenList[minIdx];
- float f = (nPos-secMin)/secLen;
- assert(f>=0.0f && f<=1.0f);
- nPosition = pos1 * f + pos0 * (1.0f-f);
- nDirection = pos1-pos0;
- return minIdx;
- }