Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this 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
show 3 more comments

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

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 0 down vote accepted

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.

This is probably a result of the correspondence you have between positive/negative rotation and clockwise/counterclockwise direction.

share|improve this answer
    
I thought I had tried that before, but I must be tired. This answer worked perfectly. –  Manderin87 Nov 27 '12 at 1:34
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.