Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Making the main sprite face the direction of mouse position.

Author
Message
Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 14th Mar 2010 10:52
Hello.
I'm making a game with a top-down view on the game area. Firtst of all i'm trying to make the main character to face the direction of the mouse pointer. I'm trying to achieve this by using a formula:

coordinates:
mouse pointer(X2, Y2)
character(X1, Y1)

formula:
(Y2-Y1)/(X1-X2) = k

and then the arcusTanget of k is the angle.
This works fine exept for when i come over 180 degrees. When this happens it starts giving me measures from 0 - 180 again. So when i want the character to face down, he is facing the opposite way.
Any ideas?
Thank you!

code:


I just love the smell of code in the morning...
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 14th Mar 2010 14:20
Use either the atan2 function (C++) or dbATANFULL (Dark GDK).
Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 14th Mar 2010 19:32 Edited at: 14th Mar 2010 19:32
Works! Thank you very much ^^
A note for others who were looking for this:
If you use dbAtanFull () this function takes two paragraphs. So instead of inserting (y2 - y1)/(x2-x1) you need to insert it like this:
dbAtanFull (y2-y1, x2-x1);
Also this function returns the value in degrees, not radians.

I just love the smell of code in the morning...

Login to post a reply

Server time is: 2024-10-02 01:31:25
Your offset time is: 2024-10-02 01:31:25