[英]How to check whether a 3D point is left or right of a vector
我正在使用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將始終返回正值,盡管如果不是這樣的話它會很出色。
讓我們來
CB = b - CurrentPosition
(在您的代碼中,我沒有看到curr。pos。的變量)
然后找到表達符號(它是矢量乘積的Y分量,如交叉乘積)
forwards.X * CB.z -forwards.X * CB.X
如果為正,則B保留到當前路徑;如果為負,則保留至當前路徑
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.