Jump to content
• Advertisement

# finding angle of direction to adjust sprite animation

This topic is 4268 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## 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

##### Share on other sites
Advertisement
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

##### 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

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5
• Advertisement

• 11
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633722
• Total Posts
3013538
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!