# new problem

This topic is 4413 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi. i got the standard math.h to work with my lib, so thats taken care of. but, now i have a new problem. ok, here is my situation: i have a topdown picture of a dude with his gun pointing UP. (straight 90 degrees) i have a cursor, which i can move. i want it so that the guys gun is always pointed TOWARDS the cursor. (i have rotation functions) now, i think something along the lines of using atan2 to find the arc should work, but my guy is just going crazy, rotating 50 degrees a second. any idea how to do this? (BTW, i can basically use all trig functions now) also, i can set the angle of my player manually, so that isnt a problem. thanks!

##### Share on other sites
Yes, atan2() is what you want, so if it's not working for you, you might post the code where you compute the angle. (And don't forget that C/C++ math functions work with radians, not degrees.)

##### Share on other sites
Quote:
 Original post by jykYes, atan2() is what you want, so if it's not working for you, you might post the code where you compute the angle. (And don't forget that C/C++ math functions work with radians, not degrees.)

i tried this:

player->angle = (atan2((player->x - cursor->x), (player->y - cursor ->y)) * PI) / 180;

but then, the player doesnt rotate a lot. then i tried:

player->angle = (atan2((player->x - cursor->x), (player->y - cursor ->y)) * 180) / PI;

now the player rotates, but hes always oppisite of where i want him. if my cursoe is to the right of him, and has the same X value as him, then he looks EXACTLY the other way. same for all the other values, the "basic" rotation is right, but just inverted.

how can i fix this?

thanks!

##### Share on other sites
player->angle = (atan2((cursor->x - player->x), (cursor->y - player->y)) * 180) / PI;

All I did was negate the arguments to atan2. This should change the result by 180 degrees, thus fliping the direction the character is facing.

##### Share on other sites
Quote:
 Original post by Vorpyplayer->angle = (atan2((cursor->x - player->x), (cursor->y - player->y)) * 180) / PI;All I did was negate the arguments to atan2. This should change the result by 180 degrees, thus fliping the direction the character is facing.

ok, i tried that, and it now works halfway. meaning, when the cursor is either directly to the left or right of the person, he faces it correctly, BUT, when the cursor goes vertical, he gets flipped again. any way to fix that?

##### Share on other sites
OK, I think before you might have meant that just the x axis was flipped the wrong way. Try this out:

player->angle = (atan2((player->x - cursor->x), (cursor->y - player->y)) * 180) / PI;

##### Share on other sites
Quote:
 Original post by VorpyOK, I think before you might have meant that just the x axis was flipped the wrong way. Try this out:player->angle = (atan2((player->x - cursor->x), (cursor->y - player->y)) * 180) / PI;

well, actually, i meant the Y axis. so i inverted the Y arguments, AND IT WORKS PERFECTLY!! YAY!!

thanks to all!!

##### Share on other sites
For a test, try to get it to rotate slowly toward the mouse :)

off-topic:
You sound like where I was 4 years when I first started on my top down game.

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633757
• Total Posts
3013711
×