move by mouse

hi all
i have a problem with my move by mouse implementation
whatever the angle is in a quadrant, my player moves with a PI/4 angle in the direction
i check the angle, it changes correctly with the mouse position

here is the pieces of code i’m using :
/* function: mouse_manager
desc : mouse management
args : void
returns : void */
void
mouse_manager (void)
{
int middle_x = WIDTH >> 1;
int middle_y = HEIGHT >> 1;
int dx = 0;
int dy = 0;

SDL_GetMouseState(&dx, &dy);

dx -= middle_x;
dy = middle_y - dy;

me->angle_x = atan2 (dy, dx);
if (me->angle_x > 2M_PI)
me->angle_x -= 2
M_PI;
else if (me->angle_x < -2M_PI)
me->angle_x += 2
M_PI;
}

this function is called on each iteration of my main loop (while (finished == 0) { … })

and now how i rotate and move the player :
glRotatef(temp->angle_x*(180/M_PI)-90, 0.0f, 0.0f, 1.0f);

notice that the z-axis is from bottom to top in my viewport

/* up and down */
if (keys->w)
{
dx += cos (me->angle_x) * speed;
dy += sin (me->angle_x) * speed;
}
if (keys->s)
{
dx -= cos (me->angle_x) * speed;
dy -= sin (me->angle_x) * speed;
}

and of course, i add dx and dy to my player position

i can’t see where is the problem !
thanks for help

  • sam

Originally posted by zwe|sam:
[b]hi all
i have a problem with my move by mouse implementation
whatever the angle is in a quadrant, my player moves with a PI/4 angle in the direction
i check the angle, it changes correctly with the mouse position

here is the pieces of code i’m using :
/* function: mouse_manager
desc : mouse management
args : void
returns : void */
void
mouse_manager (void)
{
int middle_x = WIDTH >> 1;
int middle_y = HEIGHT >> 1;
int dx = 0;
int dy = 0;

SDL_GetMouseState(&dx, &dy);

dx -= middle_x;
dy = middle_y - dy;

me->angle_x = atan2 (dy, dx);
if (me->angle_x > 2M_PI)
me->angle_x -= 2
M_PI;
else if (me->angle_x < -2M_PI)
me->angle_x += 2
M_PI;
}

this function is called on each iteration of my main loop (while (finished == 0) { … })

and now how i rotate and move the player :
glRotatef(temp->angle_x*(180/M_PI)-90, 0.0f, 0.0f, 1.0f);

notice that the z-axis is from bottom to top in my viewport

/* up and down */
if (keys->w)
{
dx += cos (me->angle_x) * speed;
dy += sin (me->angle_x) * speed;
}
if (keys->s)
{
dx -= cos (me->angle_x) * speed;
dy -= sin (me->angle_x) * speed;
}

and of course, i add dx and dy to my player position

i can’t see where is the problem !
thanks for help

  • sam[/b]

I am sorry that i don’t know what your problem is .and i think it’s usually not a good idea to post code on the net to let others debug for you ,it’s kind of thing you must do yourself,however,It’s ok to ask the thing you don’t understand.People would like to help

i’ve been debugging this piece of code for hours !
that’s why i need an external point of vue on it …
the problem is that whatever the angle with the X axis is in [0 ; PI/2], the player will go with an angle equal to PI/4
if it is between [PI/2 ; PI], angle will be 3PI/4, etc …
but i check the angle on every frame, it is correct … but the player doesn’t move in the correct direction.
can’t understand why ;(