3D寻路算法,出现频繁拉回问题 第2页
if (m_vtPath.size() == 0)
{
return m_stopCoordinate3D;
}
float nTotalDistance = GetCompletePath(dwNowTime);
if (nTotalDistance < 0)
{
cout << "计算玩家移动距离错误" << endl;
return m_stopCoordinate3D;
}
// ceshi
//cout << "距离:" << nTotalDistance << "时间" << (dwNowTime-m_dwStartMoveTime) << endl;
COORDINATE_3D coordinate3D;
// 上面已经计算出玩家行走总距离,计算玩家位置
vector<COORDINATE_3DPATH>::iterator itPath = m_vtPath.begin();
for (; itPath!=m_vtPath.end(); ++itPath)
{
if (itPath->allDistance > nTotalDistance)
{
// 角色当前位置在当前path中,计算当前位置
float nCurDistance = nTotalDistance - (itPath->allDistance - itPath->curDistance);
if (nCurDistance < 0)
{
cout << "[严重错误]获取坐标" << endl;
return m_stopCoordinate3D;
}
coordinate3D.x = itPath->x + itPath->dFormula*itPath->xDistance*nCurDistance;
coordinate3D.y = itPath->y + itPath->dFormula*itPath->yDistance*nCurDistance;
coordinate3D.z = itPath->z + itPath->dFormula*itPath->zDistance*nCurDistance;
coordinate3D.dir = itPath->dir;
if (coordinate3D.x ==1 && coordinate3D.y==1 && coordinate3D.z == 1)
{
int i = 0;
}
///yang
//cout << "当前移动坐标:x:" << coordinate3D.x << ",y:" << coordinate3D.y << ",z:" << coordinate3D.z << endl;
///yang
return coordinate3D;
}
}
// 到达目标点做先前点路径的清理工作
m_vtPath.clear();
return m_stopCoordinate3D;
}
const vector<COORDINATE_3DPATH>* CCoordinatePath::GetPath()
{
return &m_vtPath;
}
COORDINATE_3D CCoordinatePath::GetStopCoordinate()
{
return m_stopCoordinate3D;
}
float CCoordinatePath::GetCompletePath( DWORD dwNowTime )
{
// 无变速的移动距离计算
DWORD dwMoveTime = dwNowTime-m_dwStartMoveTime;
return (m_nCompletePath + m_wCurSpeed*dwMoveTime/1000.0f);
}
void CCoordinatePath::UpdateSpeed( unsigned short wSpeed, DWORD dwNowTime )
{
// 计算已经完成路径
m_nCompletePath += GetCompletePath(dwNowTime);
m_dwStartMoveTime = dwNowTime;
m_wCurSpeed = wSpeed;//当前速度
}
unsigned short CCoordinatePath::GetSpeed()
{
return m_wCurSpeed;
}
bool CCoordinatePath::IsMoving( DWORD dwNowTime )
{
GetCoordinate(dwNowTime);
if (m_vtPath.size() > 0)
{
return true;
}
else
{
return false;
}
}