[LIBGDX]How to align bullet to gun

Hello.
So I’m making a game where the player controls a turret in the middle of the screen.
So far, the turret rotates towards the mouse and can shoot.
But here is the problem, when the turret shoots, the bullet that spawns has the right angle and moves in the right direction, but it isn’t aligned with the gun.

The bullet is aligned with the gun when the angle is 0, but not with any other angle.

Here is my code for setting the bullet’s position:


float offsetx = 8f;
float offsety = 12.5f;
bullet.pos.x = (pos.x + offsetx) * MathUtils.cos(angle * MathUtils.degreesToRadians) - (pos.y + offsety) * MathUtils.sin(angle * MathUtils.degreesToRadians);
bullet.pos.y = (pos.x + offsetx) * MathUtils.sin(angle * MathUtils.degreesToRadians) + (pos.y + offsety) * MathUtils.cos(angle * MathUtils.degreesToRadians);

Here are some pictures of what im talking about:
(No rotation applied)

(Shooting while making the turret do a full 360)

As you can see in the second picture, I’m thinking if i can shrink the circle, then that would solve my problem, I just don’t know how.

Try something like this:


        // This controls the bullet moving forwards, to move backwards, make the bulletSpeed negative.
        position.x += (Math.sin(angle / 180 * pi)) * bulletSpeed;
        position.y -= (Math.cos(angle / 180 * pi)) * bulletSpeed;

        // This controls the bullet moving sideways
        position.x += (Math.sin((angle - 90) / 180 * pi)) * bulletSpeed;
        position.y -= (Math.cos((angle - 90) / 180 * pi)) * bulletSpeed;

I haven’t tested this, but I use the same calculations for first person controls.
I would put this code in a Bullet class, and when the turret fires, a new bullet is created, with the turret’s current angle, and location, so this allows for the turret to be moved around on screen.

Good luck with your project!