zwe|sam
06-12-2003, 11:14 PM
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 > 2*M_PI)
me->angle_x -= 2*M_PI;
else if (me->angle_x < -2*M_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
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 > 2*M_PI)
me->angle_x -= 2*M_PI;
else if (me->angle_x < -2*M_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