# Move projectile in direction the gun is facing [duplicate]

Possible Duplicate:
Move sprite in the direction it is facing?

I am attempting to have a projectile follow the direction a gun is facing. When using the following code I am unable to make the projectile go in the right direction.

``````float speed = .5f;

float dX = (float) -Math.cos(Math.toRadians(degree)) * speed;
float dY = (float) Math.sin(Math.toRadians(degree)) * speed;
``````

Can anyone tell me what I am doing wrong? The degree is the direction the gun is facing in degree's.

-

## marked as duplicate by Byte56♦, Laurent Couvidou, Trevor Powell, Josh Petrie♦, Ali.SDec 9 '12 at 6:55

This question was marked as an exact duplicate of an existing question.

I tried that, doesn't work, not sure why the dx and dy do not go in the right direction. One problem I am having and I'm not sure if this has anything to do with it... but all I have is the center of the gun and the degree its facing, but I get the movement in other directions but the correct one. – Manderin87 Nov 27 '12 at 1:13
What behavior are you observing? Which direction does the projectile travel in relative to the gun? What else have you tried? – ktodisco Nov 27 '12 at 1:15
I set the gun at 45 degrees and I get the bullets traveling at a right angle to it at approximately 315 degrees, I try and correct for it, but then it comes out at some other angle. – Manderin87 Nov 27 '12 at 1:19
At 90 degrees the bullets travel straight down at approximately 270. – Manderin87 Nov 27 '12 at 1:22
Try applying the unary `-` to `dY` instead of `dX`. – ktodisco Nov 27 '12 at 1:23

Given the behavior you were experiencing and the results of my suggestion in the comments, the problem is that you need to apply the unary `-` to the result of `Math.sin` instead of `Math.cos`.