# How to Align Gun with Bullets

I have a top-down 2D shooter. I have an image of a player holding a gun, that rotates to face the mouse. Please note that the gun isn't a separate image tethered to the player, but rather part of the player. Right now, bullets are created at the player's x and y. This works when the player is facing the right way, but not when they rotate. The bullets move in the right direction, but don't come from the gun. How can I fix this?

TL;DR: When the player rotates, bullets don't come from gun.

``````public void fire() {
angle = sprite.getRotation();
System.out.println(angle);
x = sprite.getX();
y = sprite.getY();

Bullet b = new Bullet(x, y, angle);
}
``````
-

Let `x`, `y` be the offset of the end of the barrel from the (unrotated) sprite's center, and `theta` the angle of rotation (counter-clockwise)

Then the barrel's offsets `x2`, `y2` for the rotated sprite can be calculated like this:

``````x2 = x * cos(theta) - y * sin(theta)
y2 = x * sin(theta) + y * cos(theta)
``````
-
thank you! I would like to add, that theta must be in radians, just in case if someone needs it. – Gintas_ Jan 3 at 13:21

It's not easy to tell form the code you provided (what does addBullet do? where does it draw the bullet in relation to your sprite?) nor is there information about your sprite in general (how big it is, where the origin point of it is).

However, I would hazard a guess that your gun is not located at the X,Y coordinates that the Sprite returns. However, those are the coords you pass to your bullet-drawing routine, which I am guessing draws your bullets at the point you specify.

You need to determine the offset of the gun from your sprite's origin point, and then modify your x and y values before calling addBullet:

``````public void fire() {
angle = sprite.getRotation();
System.out.println(angle);
//One option, where the gun's offset from sprite origin
x = sprite.getX() - sprite.getGunOffsetX();
y = sprite.getY() - sprite.getGunOffsetY();

//Another option, where you store the gun's position on the sprite
x = sprite.getGunX();
y = sprite.getGunY();

Bullet b = new Bullet(x, y, angle);