title: Andengine - Shooting bullets in front of a rotating sprite (Turret / Ship)
body: <p>I'm making a game with Andengine and i'm stuck for 2 days on shooting from a rotating sprite. I'm not a hero at geometry and already ask a teacher but he could also not provide me the correct answer. So who is a mathematics hero and help me out :).</p>
  
  <p>The problem is that i cannot figure out where the bullet has to spawn in front of the turret. Rotating and finding the destination where the bullet has to go is no problem. It only about the spawn point.</p>
  
  <p>I removed a lot of not-interesting-code for this question.</p>
  
  <p>Okay so here is the rotating code from the turret-rotation:</p>
  
  <pre><code>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);
  }
  }
  </code></pre>
  
  <p>The code above is working fine.</p>
  
  <p>Here is the code from the laser that the ship is shooting.
  Read the comments to find out witch part is not working.</p>
  
  <pre><code>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);
                  }
  
          };
  }
  
  }
  </code></pre>
  
  <p><strong>Here is a link to a drawing that shows the x-axis and y-axis values.</strong>
  <a href="http://s24.postimg.org/citz29339/gamescene.png" rel="nofollow">http://s24.postimg.org/citz29339/gamescene.png</a></p>
  
  <p><strong>Thank you!</strong></p>