#In my 2d game, when I try to rotate my object 90 degrees, it sometimes takes the longer rotation ins

5 messages · Page 1 of 1 (latest)

still sparrow
#
extends CharacterBody2D

const FLOAT_EPSILON = 0.001

@onready var shape = $CollisionShape2D
@onready var ray: RayCast2D = $Ray
var settled = false
var rotating = false
var current = 0.0
var touched_ground_again = false

static func compare_floats(a, b, epsilon = FLOAT_EPSILON):
    var diff =  abs(a - b)
    return diff <= epsilon

func raycast(origin, target):
    var space_state = get_world_2d().direct_space_state
    var query = PhysicsRayQueryParameters2D.create(origin, target)
    return space_state.intersect_ray(query)

func _physics_process(delta):
    if !settled && ray.get_collider():
        settled = true
    if ray.get_collider():
        touched_ground_again = true
    if !settled:
        return
    if !ray.get_collider() && touched_ground_again:
        rotating = true
    if rotating:
        var target = lerp(rotation_degrees, current + 90, 0.3)
        if compare_floats(rotation_degrees, current + 90.0):
            touched_ground_again = false
            current += 90.0
            rotating = false
        return
    velocity = transform.x * 80
    
    move_and_slide()

I know the code is terrible, i'm mostly messing around

onyx pawn
#

What you want is a Quaternion Slerp

#

That will lerp between any two arbitrary rotations, taking the route with the less rotational distance between them

#

What you're doing is called Euler Angle Lerping, which basically lerps between two arbitrary degrees of rotation

still sparrow
# onyx pawn What you want is a Quaternion Slerp
        var quat = Quaternion.from_euler(Vector3(0, 0, rotation_degrees))
        var target = Quaternion.from_euler(Vector3(0, 0, current + 90))
        rotation_degrees = quat.slerp(target, 0.1).get_euler().z

I tried your approach, to mixed results
This results in only a 2 degree rotation, shouldn't it be 90 degrees ?