我正在使用DirectX根据航点图进行寻路AI。我有一辆带有前锋矢量的汽车告诉我它朝哪个方向。汽车将始终在XZ平面中移动,并且永远不会改变它的Y坐标。如果下一个航路点在右侧,我想告诉汽车右转,而左侧则相反。这是我对寻路功能的当前代码,以及我目前正在检查的内容(这是不正确的)
void Car::Pathfinding(XMVECTOR forwards)
{
int nextNode;
if(currentNode + 1 > waypoints->size())
nextNode = 0;
else
nextNode = currentNode + 1;
Accelerate();
if(onRightOf(forwards, waypoints->at(nextNode)))
TurnLeft();
else if(onLeftOf(forwards, waypoints->at(nextNode)))
TurnRight();
//SquaredDisplacement is just Pythagoras's theorem without sqrt
if(squaredDisplacement(waypoints->at(nextNode)) < 100)
currentNode = nextNode;
}
bool Car::onRightOf(XMVECTOR forwards, XMFLOAT3 b)
{
forwards = XMVector3AngleBetweenVectors(forwards, XMLoadFloat3(&b));
XMStoreFloat3(&b, forwards);
if(b.x <= XM_PI / 4) //So it doesn't have to be exact
//The onLeftOf has a >= instead of <=
return false;
return true;
}
我知道XMVector3AngleBetweenVectors总是会返回正数,但如果没有,它会很好。
答案 0 :(得分:1)
让我们的向量
CB = b - CurrentPosition
(我在代码中看不到curr.post的变量)
然后找到表达的符号(它是载体产品的Y-组分,如交叉产物)
forwards.X * CB.z -forwards.X * CB.X
如果是正数,则B保留为当前路径,如果是负数 - 则为