2017-02-14 102 views
0

我有一個數學問題,我正在努力。計算圓上的目標範圍Three.js

我有一個特殊的弧度我想指向相機(camera.rotation.y)。我想告訴用戶他們是否需要向左或向右平移以達到該弧度,並且具有PI/4的容差。

我已經讓相機+小車旋轉

function getCurrentPan(){ 
    return dolly.rotation.y + camera.rotation.y > 0 
     ? (dolly.rotation.y + camera.rotation.y)%Math.PI 
     : ((dolly.rotation.y + camera.rotation.y)%Math.PI) + Math.PI; 
} 

歸泛號碼所以它總是0和PI之間。

我曾計劃制定出我可以檢查的範圍,以查看用戶是否需要向左或向右平移才能達到該弧度。嘗試下面的代碼:

 point.leftRange = {}; 
     point.leftRange.min = point.pan - range > 0 ? point.pan - range : Math.PI - point.pan - range; 
     point.leftRange.max = point.leftRange.min - Math.PI/2 - range > 0 ? (point.pan - range)%Math.PI : (Math.PI - (point.leftRange.min - Math.PI/2 - range))% Math.PI; 

     console.log(point.leftRange); 

     point.rightRange = {}; 
     point.rightRange.min = (point.pan + range) % Math.PI; 
     point.rightRange.max = (point.rightRange.min + (Math.PI/2 - range)) % Math.PI; 

     console.log(point.rightRange); 

這是給我像

  • point.pan 1.464308692701496
  • left.min 1.071609611002772
  • left.max 0.8918857974908487
  • right.min 1.8570077744002202
  • right.max 3.0351050194963927

我已經試過另一種方法是

   var opp = (point.pan - Math.PI/2)%Math.PI; 
       opp = opp > 0 ? opp : Math.PI - opp; 

       if(getCurrentPan() > point.pan && getCurrentPan() < opp) 
        console.log('greater'); 
       else if(getCurrentPan() < point.pan && getCurrentPan() > opp) 
        console.log('less'); 

這要是鍋就像是0.5左右的範圍是3等(PI的另一側)再次將不考慮。

對不起,我沒有解釋得很好,但如果有人能指出我正確的方向,我會很感激!

回答

1

這是過度工程。如果你想找出一個目標是否是在相機的左側或右側,你這樣做:

d = camera.getworlddirection(); 
diff = target.position - camera.position; 
theta = atan2(diff.x,diff.z) - atan2(d.x,d.z); 

這是你的相機正在VS在對象爲相對於相機之間的夾角。

這樣:

if(abs(theta) < someThreshold){ 
    if(theta > 0){ 
    //its to the right 
    } 
    else{ 
    //its to the left 
    } 
} 
+0

感謝。這很有用。我的問題不在於它是一個對象......但是這可能對此有好處 - 更多的是存儲的視角。我可能能夠添加一個隱藏的對象在這個角度tho使用這個,如果我不能這樣做的角度.. – user5839

+1

其實我打算這樣做。我會標記答案正確.. – user5839

+0

什麼是目標的類型。位置'和'camera.position'就是'vector3'的類型,我收到錯誤了。 –