2015-12-15 59 views
1

我有兩個角度,currentAngle和goalAngle。兩個角度都在[0,360]之間。我需要一行代碼來告訴我以currentAngle開始的旋轉方式,以最便宜的方式到達goalAngle,其中旋轉+1或-1度需要花費1美元。玩家應該旋轉哪個方向?

這裏是我的代碼將是什麼樣子:

if(???) { rotateClockwise(); } 
else { rotateCounterclockwise(); } 

回答

0
 // Given the player's rotation, calculate which direction the player should rotate, if any. 
    if(Math.abs(goalAngle - super.getRotY()) > ROTATION_ACCURACY){ 
     if(Math.abs(goalAngle - super.getRotY()) < 180){ 
      if(goalAngle - super.getRotY() < 0){ super.increaseRotation(0, -super.getTurnSpeed(), 0); } 
      else{ super.increaseRotation(0, super.getTurnSpeed(), 0); } } 
     else{ 
      if(goalAngle - super.getRotY() < 0){ super.increaseRotation(0, super.getTurnSpeed(), 0); } 
      else{ super.increaseRotation(0, -super.getTurnSpeed(), 0); } } } 
0

<script type="text/javascript"> 
 
function rotateClockwise() 
 
{ 
 
\t alert('Clockwise'); 
 
} 
 
function rotateCounterclockwise() 
 
{ 
 
\t alert('Counterclockwise'); 
 
} 
 

 
function calc(currentAngle,goalAngle) 
 
{ 
 
\t if(isNaN(currentAngle)||isNaN(goalAngle)){alert('invalid angles');return false;} 
 
\t 
 
\t var diff=currentAngle-goalAngle; 
 
\t while (diff < -180) diff += 360; 
 
\t while (diff > 180) diff -= 360; 
 
\t if(diff<0){ 
 
\t \t rotateClockwise(); 
 
\t }else{ 
 
\t \t rotateCounterclockwise(); 
 
\t } 
 
} 
 
</script> 
 
<input type="number" id="c" value="45" step="1"> 
 
<input type="number" id="g" value="75" step="1"> 
 
<input type="button" onclick="calc(parseFloat(document.getElementById('c').value),parseFloat(document.getElementById('g').value))" value="Calc">