2016-03-15 63 views
1

我有一個java遊戲,我想要做一個形式的射擊,產生三個子彈在不同的角度。這個鏡頭的一個例子可能是這樣的:在java遊戲中的子彈實施

* * 
    * 
ship 

* *代表子彈。我有一個實現,以一定的速度在船前產生一顆子彈。怎麼可能產生另外兩顆子彈,就像上面那張非常糟糕的圖表一樣。

這是我如何實際創建盈船舶的子彈:

public void mkCannonball(){ 

    Vector2D shipPos = new Vector2D(direction); 
    shipPos.normalise().mult(this.radius +2).add(position); 
    Vector2D bulletTrajectory = new Vector2D(direction); 
    bulletTrajectory.normalise().mult(Constants.BULLET_SPEED).add(velocity); 
    cannonball = new Cannonball(new Vector2D(shipPos), new Vector2D(bulletTrajectory)); 
    SoundManager.fire(); 
} 
+0

只需使用'direction'並旋轉它所需的任何角度,然後再創建一個'Cannonball'。您也可以調用'mkCannonball'三次並將角度作爲參數傳遞,但您可能不想重複所有計算3次以及播放聲音3次。 – Thomas

+1

順便說一下,我猜這裏的子彈和速度標籤是不正確的,因爲它們適用於這些名稱的庫。 – Thomas

回答

1

我可能會做的東西沿着這些路線...

public void mkCannonball(){ 

    Vector2D shipPos = new Vector2D(direction); 
    shipPos.normalise().mult(this.radius +2).add(position); 

    Vector2D leftBulletTrajectory = new Vector2D(direction - 1); 
    Vector2D rightBulletTrajectory = new Vector2D(direction + 1); 

    leftBulletTrajectory.normalise().mult(Constants.BULLET_SPEED).add(velocity); 
    rightBulletTrajectory.normalise().mult(Constants.BULLET_SPEED).add(velocity); 

    leftCannonball = new Cannonball(new Vector2D(shipPos), new Vector2D(leftBulletTrajectory)); 
    rightCannonball = new Cannonball(new Vector2D(shipPos), new Vector2D(rightBulletTrajectory)); 

    SoundManager.fire(); 
} 

基本上只是創建兩個軌跡(左和右)修改方向。然後使用這些軌跡創建兩個炮彈。

我想這個技巧將會弄清楚如何修改矢量方向左右兩度。在上面的代碼中,我只使用了+和 - 1.

+0

這是一個很棒的解決方案!雖然我注意到當子彈在特定的船隻方向產卵時,它們在船內產卵並與其發生碰撞。無論哪種方式這個實現工作! – Volken