new problem

Started by
6 comments, last by Sirisian 17 years, 5 months ago
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!
Advertisement
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.)
Quote:Original post by jyk
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.)



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!
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.
Quote:Original post by Vorpy
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.


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?
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;
Quote:Original post by Vorpy
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;


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

thanks to all!!
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.

This topic is closed to new replies.

Advertisement