我正在使用 DirectX 基于航点图制作寻路 AI。我有一辆带有前向 vector 的汽车,可以告诉我它面向哪个方向。汽车将始终在 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 将始终返回正值,但如果不是的话,它会很出色。
最佳答案
让我们 vector
CB = b - CurrentPosition
(我在您的代码中没有看到 curr.pos. 的变量)
然后找到表达式的符号(它是 vector 乘积的Y分量,如叉积)
forwards.X * CB.z -forwards.X * CB.X
如果为正,则 B 离开当前路径,如果为负 - 向右
关于c++ - 如何检查 3D 点是在 vector 的左侧还是右侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23029334/