|
Mouse Aiming |
Kevlar Games
Member #7,757
September 2006
|
Hey, I'm trying to get a sprite to always face the mouse. How to I do this? Is there a formula I need to calculate the angle based on the mouse position? If so, what is it? Thanks, |
23yrold3yrold
Member #1,134
March 2001
|
Depends a lot on your game. If this is some top down angle where the camera is perfectly perpendicular to the playfield, easy as pie (your alliteration skill increases by 1). If not, it gets stickier ... -- |
Albin Engström
Member #8,110
December 2006
|
Here you go: #include <math.h>
_______________________________________ #ifndef PI #define PI 3.1415927 //++ #endif
_______________________________________ BITMAP *test;
_______________________________________ double player_angle;
_______________________________________
mouse_y is the vertical screen position of the mouse pointer. 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. _______________________________________ pivot_sprite(buffer, test, pX, pY, cX, cY, itofix(player_angle));
pX and pY is the posision of the sprite. enjoy! this should work, i think.. |
James Stanley
Member #7,275
May 2006
|
Albin said:
#ifndef PI What is the //++ for? |
Albin Engström
Member #8,110
December 2006
|
James Stanley said: What is the //++ for? It was my extremely bad way of saying: "you can use more decimals if you want". >< |
ImLeftFooted
Member #3,935
October 2003
|
Why not just use M_PI? Quote: 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));
|
Albin Engström
Member #8,110
December 2006
|
What is M_PI? -.- |
ImLeftFooted
Member #3,935
October 2003
|
Its a constant that represents Pi. Part of some standard and included with your compiler via cmath or math.h |
Ceagon Xylas
Member #5,495
February 2005
|
Oops. Should have read. |
Kevlar Games
Member #7,757
September 2006
|
Thanks, everybody. I wasn't expecting this many replies! I'll try those methods and tell you how they worked. EDIT: Well, they were a bit confusing at first, but I got them to work! Now, is there any way to rotate a sprite without distortion? |
|