Make direction of rotation check include diagonals

Godot Version

4.3

Question

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.

this is my code so far:

if abs(angle) > 0.75 * PI:
		car_texture.rotation = deg_to_rad(270)
	elif abs(angle) < 0.25 * PI:
		car_texture.rotation = deg_to_rad(90)
	elif angle > 0.0:
		car_texture.rotation = deg_to_rad(0)
	else:
		car_texture.rotation = deg_to_rad(180)

if someone could help me figure out diagonals, i would super appreciate it.

cant you use the angle directly?

car_texture.rotation = angle

other than that shouldnt the difference be 1/4 * PI?

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.

45 degree rotation should be 0.25 * PI, so you need if-statements wich checks for rotation with a difference ± 0.25* PI

u/Arkaein on Reddit provided me this explanation:

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

1 Like

And as you can see the step-distance is 1/4 * PI :smiley:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.