Jump to content
  • Advertisement
Sign in to follow this  
zeldatoothemax

Mouse follow?

This topic is 3584 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

I'm working on a simple game at the moment , actually im doing it in Lua trying to familiarize myself with the language. programing is my strong point , its some of the math im trying to use that is. here's my problem, i have a space ship that will remain at the center of my screen, all i want it to do is always point to where my mouse is.IE: have it rotate to constantly point to my cursor. could anyone give me some guidance on the math part of my problem ... like detecting how much the ship needs to rotate to remain pointing in the right direction? P.S- the solution doesn't need to be Lua, I have plenty of experience in C++ (2yrs) and s lot in Java(4yrs), so if it helps just post how you would do it in your language of choice , i will figure out how to do it in Lua, thanks in advanced to all who may be able to help.

Share this post


Link to post
Share on other sites
Advertisement
try:

#define ONEEIGHTYOVERPI 57.2957


float camvecx=mousex-playerx;
float camvecy=mousey-playery;

float dist=sqrt(camvecx*camvecx+camvecy*camvecy);

camvecx/=dist;
camvecy/=dist;

float rotz=0;

if(camvecy>=0)
rotz=acos(camvecx)*ONEEIGHTYOVERPI;

else if(camvecy<0)
rotz=-acos(camvecx)*ONEEIGHTYOVERPI;

Share this post


Link to post
Share on other sites
Quote:
Original post by zeldatoothemax
Thank you very much that worked perfectly, it makes sense too.
If you have an 'atan2' function available, you can just write:
angle = atan2(mousey-playery, mousex-playerx)

Share this post


Link to post
Share on other sites
Quote:
Original post by Th0ughtCr1me
#define ONEEIGHTYOVERPI 57.2957


how is this constant calculated? dont know what math-operation is "over" (guessing its ^) sorry - english is not my native language

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!