Jump to content
  • Advertisement
Sign in to follow this  
Grimfate126

new problem

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
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 this post


Link to post
Share on other sites
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?

Share this post


Link to post
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 this post


Link to post
Share on other sites
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!!

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!