# Bullets won't shoot out of barrel in top-down shooter

So I am working on a top down shoot and have been stumped on why my bullets refuse to come out of the barrel of the gun. I know about the rotation equation which is

x2 = x * cos(theta) - y * sin(theta)
y2 = x * sin(theta) + y * cos(theta)


Here is part of my shoot method for my shotgun

    float dx = (float) (5 * Math.cos(direction) + 40 * Math.sin(direction));
float dy = (float) (5 * -Math.sin(direction) + 40 * Math.cos(direction));

//PlayState.effects.add(new MuzzleFlashEffect(x + dx, y + dy, (float) Math.toDegrees(-direction)));

for(int i = 0; i < randNum; i++){
float var = MathUtils.random(-accuracy, accuracy);
float dir = direction + (float) Math.toRadians(var);
PlayState.projectiles.add(new Bullet(this, x + dx, y + dy, dir));
}

bulletsInClip--;
lastShot = System.currentTimeMillis();


The bullets always tend to shoot out of the left side and I don't have a clue why. The player sprite origin is the center and not the top left but I can't get it shooting out of the barrel. Can someone explain why this is happening and how I can fix this. I have spent too many hours and losing my sanity.

EDIT

Here are some pictures of the character shooting in different directions

RIGHT LEFT DOWNWARD • What does it look like if the player is shooting in a different direction - to the left/right, or downwards? – congusbongus May 6 '14 at 7:53
• can you verify that the sprite is in the correct location, add a rectangle with the bounding box of the player and make sure they match up. my guess would be that the sprite is drawn in the wrong location – ratchet freak May 6 '14 at 8:48
• Added some pictures to show you guys in more detail what it looks like. – G3tinmybelly May 6 '14 at 14:55

Let's see what is happening.

float dx = (float) (5 * Math.cos(direction) + 40 * Math.sin(direction));
float dy = (float) (5 * -Math.sin(direction) + 40 * Math.cos(direction));


So facing right, dx = 40, so cos(direction) = 0 sin(direction) = 1.

dy = -5 for the same reasons. 0,0 is top left.

This means that direction = pi/2. This is unusual, normally pi/2 means up in java.

Facing down the shots are offset downward and a little left. So cos(direction) = 1, sin(direction) = 0, dx = -5, dy = 40. This means the angle is 0.

So far so good, though the angle is still funny, 0 meaning down.

Facing left it's down and below.

dx = -40, dy = 5. cos(direction) = 0, sin(direction) = -1, so this is -pi/2

Then up, it's greatly to the left and up.

What I would expect given the rest is that direction = pi. sin(direction) = 0, cos(direction) = -1

If that is the case, then you should have dx = -5, dy = -40.

But you have something that looks more like dy = -80, dx = -40.

So what's up?

My first test would be to check what exactly the direction is when you're pointing up.

Then I would watch the values of dx and dy to confirm that they were in fact the things that were screwing up and it wasn't something like your x and y values that were getting offset when you pointed up.

I know there's no way for both cos(x) and sin(x) to return positive numbers, and no way for cos(x) to return something less than -1.

So either something is screwing up the value of dy (like code that isn't shown) or the value of y when you create the bullets is incorrect, or possibly that there's some logic in your bullet constructor that is screwing up the position in that case.

I would look at the bullet constructor to make sure that it's not the one at fault. Finally I would worry about the casts to float. All the trig functions take and return doubles, and conversions to floats can do weird things which might be present elsewhere.

• As a small note, there are many ways for cos(x) and sin(x) to both return positive numbers; for instance, cos(pi/4) = sin(pi/4) ~= .707. – Steven Stadnicki May 6 '14 at 17:43
• I fixed it and I appreciate the time you put in helping me. Thanks! – G3tinmybelly May 6 '14 at 23:07
• @StevenStadnicki or more precisely all 0 < x < pi/2 will have both sin(x) and cos(x) be positive – ratchet freak May 7 '14 at 8:27