2014-04-12 125 views
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總是返回正數,雖然這將是如果沒有的話,它將會是恆星。

回答

1

讓我們的矢量

CB = b - CurrentPosition 

(我沒有看到CURR。POS的變量。在你的代碼)

然後找到表達的跡象(這是載體產品的Y成分,如跨產品)

forwards.X * CB.z -forwards.X * CB.X 

如果是陽性,B是留給電流路徑,如果爲負 - 向右

相關問題