# Mouse follow?

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

## 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 on other sites
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 on other sites
Thank you very much that worked perfectly, it makes sense too.

##### Share on other sites
Quote:
 Original post by zeldatoothemaxThank 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 on other sites

that atan2 thing sounds cool I'll look into that :)

##### 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 on other sites
180 / PI, that's all it is.

It's a defined constant, probably depreciated in C++ but it does the job.

##### Share on other sites
ah - a radian - now all makes sense.. thanks!

1. 1
2. 2
Rutin
15
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633269
• Total Posts
3011154
• ### Who's Online (See full list)

There are no registered users currently online

×