Allegro.cc Forums » Programming Questions » Mouse Aiming

 Albin Engström Member #8,110 December 2006 Here you go:_______________________________________include math.h ```#include ``` _______________________________________define pi. ```#ifndef PI #define PI 3.1415927 //++ #endif ``` _______________________________________you need a bitmap... ```BITMAP *test; ``` _______________________________________you need to store a value.. ```double player_angle; ``` _______________________________________you need some logic. ```player_angle = (atan2(mouse_y - pY, mouse_x - pX)*128/PI) + V; ``` mouse_y is the vertical screen position of the mouse pointer.you can figure mouse_x out yourself.pY is the second point of which is needed for the calculation of the degree, and which i suggest is you bitmaps rotation point is.V is the additional degrees added to the calculated direction, you want to adjust this until it looks good . NOTE: instead of a 360 degree, use 256._______________________________________and you need to draw the sprite. ```pivot_sprite(buffer, test, pX, pY, cX, cY, itofix(player_angle)); ``` pX and pY is the posision of the sprite.and cX and xY is from which point the sprite should be rotated.enjoy!this should work, i think..
 Dustin Dettmer Member #3,935 October 2003 Why not just use M_PI?Quote: and you need to draw the sprite. ```pivot_sprite(buffer, test, pX, pY, cX, cY, itofix(player_angle)); ``` Slight correction ```pivot_sprite(buffer, test, pX, pY, cX, cY, itofix(player_angle * (256 / 360.0f)); ``` But personally I would leave the number in radians, which would result in this (slightly simpler) code: ```player_angle = atan2(mouse_y - pY, mouse_x - pX) + V; pivot_sprite(buffer, test, pX, pY, cX, cY, itofix(player_angle * (128 / M_PI)); ```