shooting with mouse [Libgdx] [box2d]

hi guys, i have a problem, i don’t can do my spaceship shooting with mouse, my spaceship rotation changes with mouse cursor, but i dont can changes my shot direction.

my spaceship and my fire ball extends Sprite [box2d]
the space ship look in mouse cursor direction.

my method Fire, stay in my spaceship class

public void fire(){
        fireballs.add(new FireBall(screen, corpo.getPosition().x, corpo.getPosition().y));
    }

My method constructor and defineFireBall,stay in my fireball class

 
public FireBall(PlayScreen screen, float x, float y){
    	super(screen.getAtlas().findRegion("fireball"));
        
        this.screen = screen;
        this.world = screen.mundo;
        frames = new Array<TextureRegion>();   
            
        
        
        setBounds(x, y, 6 / ExGame.PPM, 6 /ExGame.PPM);
        defineFireBall();
    }


public void defineFireBall(){
        BodyDef bdef = new BodyDef();       
        bdef.position.set(getX(), getY());
        bdef.type = BodyDef.BodyType.DynamicBody;
        if(!world.isLocked())
        b2body = world.createBody(bdef);

       
        
        FixtureDef fdef = new FixtureDef();
        CircleShape shape = new CircleShape();
        shape.setRadius(3 / ExGame.PPM);      

        fdef.shape = shape;
        fdef.restitution = 1;
        fdef.friction = 0;
        b2body.createFixture(fdef).setUserData(this);
        
        b2body.setLinearVelocity(new Vector2( 2, 2.5f));
        setOriginCenter();
        setRotation(screen.jogador.getRotation());
        
      
    }

my update method

public void update(float dt){
        stateTime += dt;
       
       
        setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2);
        
        
        
        if((stateTime > 3 || setToDestroy) && !destroyed) {
            world.destroyBody(b2body);
            destroyed = true;
        }
        if(b2body.getLinearVelocity().y > 2f)
            b2body.setLinearVelocity(b2body.getLinearVelocity().x, 2f);
        if((b2body.getLinearVelocity().x < 0))
            setToDestroy();
    }

If you can help me, I thank you.

I’m not super familiar with box2D but I’m pretty sure the setLinearVelocity function is not dependant on the angle of the object. You have to set the parameters to the cos(angle) in the x, and sin(angle) in the y. so try:


public void defineFireBall(){
        BodyDef bdef = new BodyDef();       
        bdef.position.set(getX(), getY());
        bdef.type = BodyDef.BodyType.DynamicBody;
        if(!world.isLocked())
        b2body = world.createBody(bdef);

       
        
        FixtureDef fdef = new FixtureDef();
        CircleShape shape = new CircleShape();
        shape.setRadius(3 / ExGame.PPM);      

        fdef.shape = shape;
        fdef.restitution = 1;
        fdef.friction = 0;
        b2body.createFixture(fdef).setUserData(this);

        float angle = screen.jogador.getRotation();//assuming the angle is in radians already
        float speed = 2.5f;
        b2body.setLinearVelocity(new Vector2( Math.cos(angle)*speed, Math.sin(angle)*speed);
        setOriginCenter();
        setRotation(angle);
    }

and here’s a link to the documentation: link