Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Possible Duplicate:
How to Align Gun with Bullets

Hi all i have a problem in my Andengine code,

I need , when I touch the screen, shoot a bullet from the cannon (in the same direction of the cannon) The cannon rotates perfectly but when I touch the screen the bullet is not created at the end of the turret

This is my code:

private void shootProjectile(final float pX, final float pY){
    int offX = (int) (pX-canon.getSceneCenterCoordinates()[0]);
    int offY = (int) (pY-canon.getSceneCenterCoordinates()[1]);
    if (offX <= 0)
            return ;
    if(offY>=0)
                    return;

    double X=canon.getX()+canon.getWidth()*0,5;
    double Y=canon.getY()+canon.getHeight()*0,5 ;
    final Sprite projectile;
    projectile = new Sprite( (float) X, (float) Y,
                    mProjectileTextureRegion,this.getVertexBufferObjectManager() );

    mMainScene.attachChild(projectile);


    int realX = (int) (mCamera.getWidth()+ projectile.getWidth()/2.0f);
    float ratio = (float) offY / (float) offX;
    int realY = (int) ((realX*ratio) + projectile.getY());

    int offRealX = (int) (realX- projectile.getX());
    int offRealY = (int) (realY- projectile.getY());
    float length = (float) Math.sqrt((offRealX*offRealX)+(offRealY*offRealY));
    float velocity = (float) 480.0f/1.0f;
    float realMoveDuration = length/velocity;

    MoveModifier modifier = new MoveModifier(realMoveDuration,projectile.getX(), realX, projectile.getY(), realY);
    projectile.registerEntityModifier(modifier);
}

@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {

    if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_MOVE){

        double  dx = pSceneTouchEvent.getX() -  canon.getSceneCenterCoordinates()[0];
        double  dy = pSceneTouchEvent.getY() -  canon.getSceneCenterCoordinates()[1];

        double  Radius = Math.atan2(dy,dx);
        double Angle = Radius * 180 / Math.PI;
        canon.setRotation((float)Angle);

        return  true;

    }
    else if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN){

        final float touchX = pSceneTouchEvent.getX();
        final float touchY = pSceneTouchEvent.getY();

        double  dx = pSceneTouchEvent.getX() -  canon.getSceneCenterCoordinates()[0];
        double  dy = pSceneTouchEvent.getY() -  canon.getSceneCenterCoordinates()[1];

        double  Radius = Math.atan2(dy,dx);
        double Angle = Radius * 180 / Math.PI;

        canon.setRotation((float)Angle);
        shootProjectile(touchX, touchY);
    }

    return false;
}

Anyone know how to calculate the coordinates (X,Y) of the end of the barrel to draw the bullet?

share|improve this question

marked as duplicate by Byte56, John McDonald, Darth Satan, bummzack, doppelgreener Oct 7 '12 at 1:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

If the base (point of rotation) of the cannon is (CannonBaseX, CannonBaseY), and the length of the cannon barrel is Radius, then:

BulletX = CannonBaseX + Radius * Cos(Angle);
BulletY = CannonBaseY + Radius * Sin(Angle);

This may vary a bit depending on your coordinate system; it assumes an angle of 0 is pointing straight to the right, and that positive angles proceed counterclockwise, and that (0,0) is the upper left corner of the screen. If you have a different system, you may need to transform the parameters somewhat.

share|improve this answer
    
Isnt that the same answer as in the other question Linked above –  GameDev-er Oct 6 '12 at 20:11
    
thanks for you answer but i tryed your solution and the bulled is creates in diferents parts of the screen and not in de end of barrel :( see the modifications: [syntax="java"] int x2 = (int) (canon.getSceneCenterCoordinates()[0]+LENGTH_SPRITEMath.cos(canon.getRotation()‌​)); int y2 = (int) (canon.getSceneCenterCoordinates()[1]+LENGTH_SPRITEMath.sin(canon.getRotation()‌​)); final Sprite projectile; projectile = new Sprite( (float) x2, (float) y2, mProjectileTextureRegion,this.getVertexBufferObjectManager() ); [/syntax] any idea? thx again. –  Alberto Oct 6 '12 at 21:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.