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:
#include "DarkGDK.h"
#include <valarray>
// the main entry point for the application is this function
//DECLARATIONS:
float x = 400;
float y = 300;
float a=0;
float b=0;
float angle=0;
float koeficient=0;
const float PI= 3.141593;
void set_window () {
dbSetDisplayMode (800, 600, 32);
}
void load_background () {
dbLoadImage ("media\\pics\\Background.png", 1);
dbSprite (1,0,0,1);
}
void load_guy () {
dbLoadImage("media\\pics\\Guy.png", 2);
}
void make_guy () {
dbSprite (2, x, y, 2);
dbSizeSprite (2, 38, 23.5);
dbOffsetSprite (2, 19, 11.75);
}
void move_guy () {
dbSprite (2, x, y, 2);
if (dbKeyState(17)==1){
y=y-2;
}
if (dbKeyState (31)==1){
y=y+2;
}
if (dbKeyState (30)==1){
x=x-2;
}
if (dbKeyState (32)==1){
x=x+2;
}
}
void rotate_guy (){
a=dbMouseX();
b=dbMouseY();
koeficient=((b-y)/(a-x));
angle=((atan(koeficient))*180/PI);
dbRotateSprite (2,angle-90);
}
void DarkGDK ( void )
{
// turn on sync rate and set maximum rate to 60 fps
dbSyncOn ( );
dbSyncRate ( 60 );
set_window ();
load_background ();
load_guy ();
make_guy ();
// our main loop
while ( LoopGDK ( ) )
{
move_guy();
rotate_guy();
// update the screen
dbSync ( );
}
// return back to windows
return;
}
I just love the smell of code in the morning...