2016-03-15 76 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