Sign in to follow this  
Ultimate_Fusion

finding angle of direction to adjust sprite animation

Recommended Posts

i am trying to get the direction of movement of my character to I can change the sprite . e.g of the charcter is facing East i am wanting the character to face right etc. I have 8 spites one for every direction, N, NE, E, SE, S, SW, W, NW. depending on the angle that the character is moving to The correct sprite should be chosen. I tried: sin(angle) = y (if radius is 1) Angle = asin (y); imagenumber = (angle/6.28)*8; //divide by radians, times by number of sprites is there anything else I have missed??

Share this post


Link to post
Share on other sites
The problem with sine and cosine independently, is that you can't use either by itself to determine the true angle of the vector. If this is C++, you can use atan2, which will give you the correct angle from [-PI,PI]. The code becomes:

angle = atan2(y,x);
image = (int)((angle + 17.0*PI/16.0) / (PI / 4.0)) % 8; // simplified: (int)(4.0*angle/PI + 17.0/4.0) % 8

The translation factor translates the angle from [-PI,PI] to [0,2*PI], and it also aligns the direction "slices" so that the principle angles 0°, 45°, 90°, etc., are in the middle of the slice, and not the edge. Finally, we divide by the size of each slice, and modulo into the image range. Image 0 is centered at angle 0°, image 1 is centered at angle 45°, i.e. it goes in a counter-clockwise direction. If you have any questions let me know :)

Share this post


Link to post
Share on other sites
thanks for that reply, it was really helpful.
it is working better than before but alot of the time the sprites are facing the opposite direction.

i think my sprite sheet goes clockwise n, ne, e etc.

i used

image = 8 - image;

if (image == 8)
image = 0;


but i got the results as above.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this