2013-11-01 34 views
1

我正在與Andengine進行一場遊戲,並且我被困在旋轉精靈的射擊中2天。我在幾何學方面不是英雄,並且已經問過老師,但他也不能給我提供正確的答案。那麼誰是數學英雄,並幫助我:)。Andengine - 在旋轉精靈前面射擊子彈(炮塔/艦船)

問題是,我不知道子彈在炮塔前面產卵的位置。旋轉並找到子彈必須去的目的地是沒有問題的。它只是關於重生點。

我刪除了很多這個問題的不有趣的代碼。

好了,所以這裏是從炮塔旋轉的旋轉編碼:

public class AlienShip extends Ship { 

public static final float BASE_ROTATION_SPEED = 0.25f; 
public static final int DEFAULT_IMAGE_ROTATION = 90; //90 degrees 



protected PlayerShip ship; 


public AlienShip(float pX, float pY, TextureRegion pTextureRegion, 
     VertexBufferObjectManager pVertexBufferObject,FixedStepPhysicsWorld pw, int baseDurability) { 

    super(pX, pY, pTextureRegion, pVertexBufferObject, pw, baseDurability); 
} 

public void rotateToPlayer() 
{ 
    if (ship != null) { 

     float dX = this.getX() - ship.getX(); 
     float dY = this.getY() - ship.getY(); 

     float angle = (float) Math.atan2(-dY, dX); 

     float rotation = MathUtils.radToDeg(angle) + DEFAULT_IMAGE_ROTATION; 

     RotationModifier rotMod = new RotationModifier(BASE_ROTATION_SPEED, this.getRotation(), rotation); 
     this.registerEntityModifier(rotMod); 

    } 

} 

public void rotateToInitPos() { 

    RotationModifier rotMod = new RotationModifier(BASE_ROTATION_SPEED, this.getRotation(), 0); 
    this.registerEntityModifier(rotMod); 
} 
} 

上面的代碼工作正常。

以下是船舶正在拍攝的激光代碼。 閱讀意見,找出女巫部分無法正常工作。

public class GameScene extends Scene { 

protected PlayerShip playerShip; 



private SpawnCallback createShootCallback(boolean player) { 

return new SpawnCallback() { 
      @Override 
      public void spawn(SpawnTimer spawnTimer) { 
       PhysicsSprite laser = null; 
       AlienShip alienShip = (AlienShip) spawnTimer.getPhysicsSprite(); 


        // laser = alienMissilePool.getMissileFromPool(x,y) 
        //spawn the laser in front of the rotating ship [Not working :(] 
        laser = alienMissilePool.getMissileFromPool((alienShip.getX() * FloatMath.cos(MathUtils.degToRad(rotation)) - ((1280 - alienShip.getY() - alienShip.getY()/2) * FloatMath.sin(MathUtils.degToRad(rotation)))) , 
                   (alienShip.getX() * FloatMath.sin(MathUtils.degToRad(rotation)) + ((1280 - alienShip.getY() - alienShip.getY()/2) * FloatMath.cos(MathUtils.degToRad(rotation))))); 

        //Set the rotation from the laser same to the ship rotation [Is working perfectly]. 
        float rotation = alienShip.getRotation(); 
        laser.setRotation(rotation); 

        //Set laser speed and direction [Is working perfectly] 
        float pX = 0.01f * (playerShip.getX() - laser.getX()); 
        float pY = 0.01f * (playerShip.getY() - laser.getY()); 

        laser.getSpriteBody().setLinearVelocity(pX, pY); 


       spawnPhysicsSprite(laser); 
       } 

     }; 
} 

} 

下面是一個圖,顯示了x軸和y軸的值的鏈接。 http://s24.postimg.org/citz29339/gamescene.png

謝謝!

+0

這並不完全清楚你的「旋轉炮塔」是什麼樣子。這是否像坦克上的俯視圖? –

+0

看起來像你使用它後設置旋轉。那是故意的嗎? –

回答

0

爲什麼不把一個物體(實體)放在炮塔的地方,並用它的位置作爲激光的重生點,而不是進入數學?

所以你的炮塔將有一個實體附在它的槍的地方。 告訴我,如果你需要一個示例代碼

+0

你說的是正確的。我們固定它。 – TheHagueCoder