Rotating image in relation to mouse x and y
dothedru22

Im trying to rotate a gun so its always pointing towards the cursor. im sortof limited in physics.. i figured i needed to use the tan function which is opposite over adjacent to find that angle. (y would be the opposite and the x would be the adjacent). but its really not working out. is there some other function that i need to get this working. thanks for any help.

Matthew Leverton
dothedru22

not really giving the desired effect. it sortof just stops when the cursor gets above the gun..which i guess is right because the angle is zero.. im also getting pretty bad movement becasue for some reason it has to be a fixed type. i have to type cast the variable. its sortof wierd..

if i do it as float or double i dont get any movement.

void D_UpdateDraw(Player_S* p , BITMAP* b) {
     p->angle = atan2(float(mouse_y) , float(mouse_x));

  if (p->keyCheck == D_KEYRIGHT) {
    
    draw_sprite(b , p->playerImg , p->x += p->velX , p->y += p->velY);
  }
  else {
    draw_sprite_h_flip(b , p->playerImg , p->x += p->velX , p->y += p->velY);
  }
  rotate_sprite(b , p->gunImg , p->x + 22 , p->y + 20 , itofix(p->angle)); //gun

  circle(b , mouse_x , mouse_y , 2 , WHITE);  //cursor           
}

gnolam

The angle returned by atan2() is in radians. rotate_sprite wants fixed point 256 degree angles.

dothedru22

oh aight thanks. if anybody has ever played madness interactive i sortof want to do somethign like that..where the gun rotates around the center of the body. thats sortofg what im trying to do.

Neil Walker

This might help. It's targetted at Flash, but code is code, and maths is maths :)

http://www.codylindley.com/Tutorials/trigonometry/

Thread #586625. Printed from Allegro.cc