i’m changing the rotation of a texture depending on the parent’s rotation, to kind of mimic it. i have code for up, down, left, and right, but can not figure out diagonals at all.
i’m not trying to copy the rotation directly, just kind of mimic it by either being directly up, down, left, right, or diagonal, to give it kind of a retro aesthetic.
To better understand this, just know that atan2 takes any 2D vector (by x and y values) and returns an angle on the unit circle. If it’s in the top half of the circle it will be a positive value, and the bottom of the circle will be a negative value.
Here’s a good illustration of the unit circle: File:Unit circle angles color.svg - Wikipedia However this one goes from 0 to 2 * PI instead of -Pt to +PI, that is at the left side of the circle (180 degrees or PI from 0 at the right), the angle keeps getting bigger. With atan2 it will instead flip to negative, and then get smaller and return to 0 as it gets back to the right side going counter-clockwise.
and this code:
if velocity.length() > MIN_MOVE_SPEED_FOR_ANIM:
var angle = atan2(velocity.y, velocity.x) # angle in [-PI, PI]
if abs(angle) < 0.125 * PI:
# right
elif abs(angle) > 0.875 * PI:
# left
# top of unit circle
elif angle > 0:
if angle < 0.375 * PI
# up-right
elif angle < 0.625 * PI
# up
elif angle < 0.875 * PI
# up-left
# bottom of unit circle
else: # angle < 0
if angle < -0.375 * PI
# down-right
elif angle < -0.625 * PI
# down
elif angle < -0.875 * PI
# down-left
else:
# stopped or almost stopped
after a reply to a 4 year old comment. helped me understand the whole thing!!