2011-01-12 101 views
1

所以基本上我一直試圖做的是讓一個物體(玩家)以直線從其中彈出子彈,角度,然後繼續,所以如果「槍」指向35度角(屏幕右側0度加360逆時針),則子彈將以恆定速度行進(比如5)從原點開始。以恆定速度將物體從點A移動到點B

因爲我一直在做我的動作的方式是我有一個稱爲更新的功能,將處理所有的繪圖和什麼不是,然後當它來運動它只會增加垂直速度和水平速度到已經存在的x和y,並且試圖繞過它變得太難了,所以它增加了一個合適的水平和垂直速度以恆定的速度在該角度移動,所以任何幫助都將被讚賞。 -Heath

回答

1

也許你可以使你的update()方法來測量自上次調用以來的時間,並根據該方法更新項目符號的位置?
這將使子彈以大致恆定的速度移動。 (考慮到FPS夠大)

EDIT2:

public class Bullet { 
    // speed is in units/second 
    // angle is in radians 
    double x; 
    double y; 
    double sv; 
    double sh; 
    public Bullet(double x, double y, double angle, double speed) { 
    this.x = x; 
    this.y = y; 
    sv = Math.sin(angle)*speed; 
    sh = Math.cos(angle)*speed; 
    last_updated = System.currentTimeMillis(); 
    } 

    long last_updated; 

    public void update() { 
    long time_elapsed = System.currentTimeMillis() - last_updated; 
    last_updated = System.currentTimeMillis(); 
    this.x += this.hs*(this.time_elapsed/1000) 
    this.y += this.vs*(this.time_elapsed/1000) 
    } 
} 

也許你會需要的角度發揮,使之適合你的座標系。 (類似於否定角度,添加/減去Pi等等。)

希望這會有所幫助。

+0

我可以嘗試,但我仍然沒有正確移動 – Foxx 2011-01-12 14:18:46

相關問題