2012-05-22 157 views
2

显然,即使我已经通过微积分3,我的触发是可怕的失败我(我可能甚至不能称之为触发,这只是基本的数学)。 所以基本上我有一堆旋转的图像,我不希望它是瞬间的。 我明白如何让它旋转缓慢,但我不知道如何计算它应该旋转的方式。缓慢的角度旋转

所以如果我想转到的角度是在179度以下,转一圈。 如果它超过180度,转向另一个方向。不知道我是否说得对,但我基本上只是希望它使用最短距离旋转。 想象一下我想的导弹。

我找角度的两种方式,一种方式是使用矢量如下所示:

hero = new Vector2f(this.x, this.y); 
target = new Vector2f(mouseX, mouseY); 
target.sub(hero); 

// Calculates the rotation 
finalRotation = (int) target.getTheta(); 

的另一种方法是使用atan2(这看起来简单,我知道我可以把它缩短到一个return语句) :

direction = Math.atan2(player.x - this.x, player.y - this.y); 
return (int) Math.toDegrees(direction); 

我假设寻找最短旋转的方法对两者都适用。我一直在尝试一些尝试和错误的东西一段时间,我只是难住。 帮助我毫无价值的编程技巧!任何帮助感谢!

编辑: 我实际上忘了提到的是,我指着老鼠。 因此,如果我的鼠标与我的'图像'(这是玩家)和玩家目前正在面对的角度为200,我将如何确定它是否应该正向200或向下0,转移到360并继续向下到200

DOUBLE编辑: 所以我有什么,我想要一个工作版本,但由于某些原因最终与当前旋转是关闭的180,它会导致我的性格从来没有停止抽搐。 明天我需要更多的工作。由于我的精灵最初旋转的方式,最后我必须从currentRotation中减去90。

TRIPLE编辑: 好了,所以我有,所以我的递增和递减语句颠倒它试图晃过180和再反方向。我完善了它!

mouseX = input.getMouseX(); 
mouseY = input.getMouseY(); 

hero = new Vector2f(this.x, this.y); 
target = new Vector2f(mouseX, mouseY); 
target.sub(hero); 

// Calculates the rotation 
finalRotation = (int) target.getTheta(); 

if(!(currentRotation <= finalRotation + 1 && currentRotation >= finalRotation - 1)) { 
    double tempVal; 

    if (currentRotation < finalRotation) { 
     tempVal = finalRotation - currentRotation; 

     if (tempVal > 180) { 
      currentRotation -= .3 * delta; 
     } else { 
      currentRotation += .3 * delta; 
     } 
    } else if (currentRotation > finalRotation) { 
     tempVal = currentRotation - finalRotation; 

     if (tempVal < 180) { 
      currentRotation -= .3 * delta; 
     } else { 
      currentRotation += .3 * delta; 
     }    
    } 
} 

if (currentRotation < 0) 
    currentRotation = 359; 

if (currentRotation > 360) 
    currentRotation = 1; 

this.setAngle((int) (currentRotation+90)); 

输入的东西和向量来自Slick2D。

+0

你只需要最小的角度(请参阅@Amadan的回答),还是需要知道产生最小角度的旋转方向(顺时针或逆时针)? –

+0

我需要产生最小角度的顺时针或逆时针旋转。如同,如果我想从0到60,我移动正数(0到60)而不是负数(360到60)。 – Paha

回答

3
if (finalRotation >= 180) finalRotation = 360 - finalRotation; 

这将反转任何角度为180或以上的方向。

+0

哦不,不能这么简单。这会让我哭泣! – Paha

+0

@Paha:大声笑......它甚至可能与'finalRotation = -finalRotation'一起工作,但是'360-finalRotation'在0-360之间保持良好。 – Amadan

+0

啊,真好!感谢您的简单答案,我找不到:( – Paha