How do I create correction rotation?

I want to correct car rotation by adding torque. I can get current rotation and target rotation. I do not know what to do with them tho. I tried to just subtract them like vectors but this don’t work. Please help

Subtracting rotations must account for wrap around.
If I want to find the rotation to get from an angle of 350 degrees to 10 degrees, I would rotate +20 degrees. But 10 - 350 = -340 which is way wrong. It is “going the other way” around the circle. You need to check before subtracting.
A = 250 the start
B = 10 the destination
B - A is -340
If the answer is < -180 then add 360
If the answer is greater than +180 then subtract 360.

By that rule since B - A is -340
-340 + 360 = 20
The correct answer.
Now apply that torque.

I always end up with a blueprint function to correctly implement the above function.

Using degrees (angles) in 3D math almost always lead you astray. If you can get away with dot and cross products, you will always have a more robust, higher performing solution. And you should approximately never end up having to use Atan2(), or Acos(), or any function that uses those functions.

Let’s assume you use Forward=X, Right=Y, Up=Z.

Turn “current” and “target” rotations into basis vectors – eg, get the “forward,” “right,” and “up” of current, and the “forward,” “right,” and “up” of the target.

Now, apply a torque around “up” that is some constant (usually pretty big number) times the dot product of target.forward and current.right. This will add a twist around the “up” axis that will tend to center the “forward” vector of “current” to have zero dot product with “right” of “target,” which ends up meaning “drives forward.”

This is essentially a simple “P mode” controller, in control theory. You might want to add an I and D term, too, to make a full PID controller. The first thing to compensate for would be current spin of the object – if it’s already spinning in the right direction, add less torque; if it’s spinning in the wrong direction, add more torque. The second thing to control for is overshoot; if the current spin, 100 milliseconds from now, will lead to the object going too far left, then reduce spin by adding rightwards torque, and vice versa.

Tuning these parameters is a black art that requires careful study of all the math and quantities involved, as well as patience with many cycles of iteration. (And then someone decides to move the center of gravity of the models because it looks cooler, and you have to do it all again from scratch.)

3 Likes

Hello @Czlowiek_Siusiak
Create three Rotate System objects to rotate the image matrices by desired angles.

This is exactly what I was looking for! Thank you! I forget about DotProduct too often.