2
對於我們正在製作頂視圖遊戲的項目。角色可以轉向並在各個方向行走,並以點和角度表示。角度計算爲當前方向和程序頂部之間的角度,可以是0-359。用java計算一個角度點的移動
我對運動以下代碼:
public void moveForward()
{
position.x = position.x + (int) (Math.sin(Math.toRadians(angle)) * speed);
position.y = position.y + (int) (Math.cos(Math.toRadians(angle)) * speed);
}
public void strafeLeft()
{
position.x = position.x - (int) (Math.cos(Math.toRadians(angle)) * speed);
position.y = position.y - (int) (Math.sin(Math.toRadians(angle)) * speed);
}
public void strafeRight()
{
position.x = position.x + (int) (Math.cos(Math.toRadians(angle)) * speed);
position.y = position.y + (int) (Math.sin(Math.toRadians(angle)) * speed);
}
public void moveBackwards()
{
position.x = position.x - (int) (Math.sin(Math.toRadians(angle)) * speed);
position.y = position.y - (int) (Math.cos(Math.toRadians(angle)) * speed);
}
public void turnLeft()
{
angle = (angle - 1) % 360;
}
public void turnRight()
{
angle = (angle + 1) % 360;
}
移動時上下,並且可以打開,但只要你把這個工作好,左,右功能似乎是想在錯誤的方向(不只是90度的角度),有時開關