Moving a RigidBody to a specific rotation using only apply_torque_impulse

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ghost_wave

Hello, I’ve been using Godot for quite a while as a primarily 2D game engine. Recently, I’ve decided to dive into 3D.

Right now, I’m trying to move a RigidBody to a specific position and rotation using only apply impulse() and apply torque impulse().

I’ve figured out moving the body to a specific position using apply impulse():

func _physics_process(delta):
    self.linear_velocity = (self.translation.distance_to(targetPosition)/magnetFriction * linear_velocity * delta)
    apply_impulse(Vector3.ZERO, (targetPosition - self.translation) * delta * moveForce )

(this solution is not perfect but works…)

My problem is that I can’t figure out how to move an object to a specific rotation, in the same way, using only apply torque impulse().

My target angle is a Vector3, corresponding to an Euler angle.

First I naively thought I could just apply the difference of my current Euler angle and my target Euler angle :

func _physics_process(delta):
    apply_torque_impulse(targetAngle - rotation)

This ended in disaster as it turns out, you can’t rotate on two axis at once without your angles going crazy.

Next I tried to use Quaternions,

I figured out something that sort of works sometimes but will break at other times:

func _physics_process(delta):
    var targetQuat = Quat(targetRotation)
    var currentQuat = Quat(rotation)
    var intermediateQuat = currentQuat.slerp(targetQuat, 1 * delta)
    var intermediateEuler = intermediateQuat.get_euler()
    var targetEuler = intermediateEuler - rotation
    
    apply_torque_impulse(targetEuler * delta * 100)

This solution is really unpredictable and unstable,

The problem is that I’m having a hard time properly understanding Quaternions and how to use them in my problem.

I would really appreciate some help on this seemingly basic problem.