0
我正在使用DirectX基于路点图创建路径查找AI。我有一辆带有前进向量的汽车,告诉我它面对着哪个方向。汽车将始终在XZ平面内移动,并且永远不会改变它的Y坐标。如果下一个航点是正确的,我想告诉汽车向右转,而左侧是相反的。这是当前的代码,我有寻路功能,就是我目前正在做检查的权利(这是不正确的)如何检查3D点是向量的左侧还是右侧
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总是返回正数,虽然这将是如果没有的话,它将会是恒星。