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,
KG
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 ...
Here you go:
_______________________________________
include math.h
#include <math.h>
_______________________________________
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.
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..
#ifndef PI
#define PI 3.1415927 //++
#endif
What is the //++ for?
What is the //++ for?
It was my extremely bad way of saying: "you can use more decimals if you want". ><
Why not just use M_PI?
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));
What is M_PI? -.-
Its a constant that represents Pi. Part of some standard and included with your compiler via cmath or math.h
Oops. Should have read.
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?