Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Rotating image in relation to mouse x and y

This thread is locked; no one can reply to it. rss feed Print
Rotating image in relation to mouse x and y
dothedru22
Member #7,325
June 2006

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
Supreme Loser
January 1999
avatar

dothedru22
Member #7,325
June 2006

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
Member #2,030
March 2002
avatar

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

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

dothedru22
Member #7,325
June 2006

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
Member #210
April 2000
avatar

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

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

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Go to: