How to get a steering angle differential?

Hi, I created these two codes to get a character’s rotation angle differential, to tilt it when it rotates in some direction, but when the character completes a complete revolution, it rotates quickly within its axis, which stays ugly to see. I wonder if there is any way around this, I would appreciate any help.

The problem:

This was created recently, in a simpler way, but it still doesn’t work well.

own = self.object
position = own.worldTransform * own.worldPosition
self.posição_atual = position
result = self.current_position.xy.angle_signed(self.old_position.xy, 0.0)
rotation_limite = 0.02
if result > rotation_limite: result = rotation_limite 
if result < -rotation_limite: result = -rotation_limite 

xyz = own.localOrientation.to_euler()
xyz[1] = ((result*radians(300))*(vel_*0.3))
own.localOrientation = xyz.to_matrix()
self.old_position = position

This other code works much better, but it still has problems with what I described above.

own = self.object
rotaion_limite = 1.0 #here is a floating value entry that varies during gameplay
if rotaion_limite > 0.02:
    rotaion_limite = 0.02
diference = (Vector(self.wrd_oldori) - Vector(own.worldOrientation.to_euler()))[2]
if diference > rotaion_limite:
    diference = rotaion_limite
if diference < -rotaion_limite:
    diference = -rotaion_limite

smooth = 0.95
self.wrd_smoothori = ((self.wrd_smoothori * smooth) + (((diference *radians(300))*(vel_*0.3) * abs(1-smooth))))
xyz = own.localOrientation.to_euler()
xyz[1] = self.wrd_smoothori
own.localOrientation = xyz.to_matrix()
self.wrd_oldori = own.worldOrientation.to_euler()

I haven’t spent a heap of time reading this, so take my analysis with a grain of salt…
I think this is the spot that’s causing the problem. If you consider that your current rotation is between 0 and 360, then going from 3 > 6 means you’re gaining 3 degrees, right?
Well, when you go from 359 > 2, there are two ways to interpret that. Either you gained 3 degrees to the right and it rolled over, or you lost 357 degrees. I believe you’re seeing the latter in your animation.

I would recommend putting in some code to override the direction difference when you go under 0, or over 360. The easist way to do that is to check if the difference is over 180 (in either direction), and then take off 360 (or whatever value represents a full rotation for you) before you cap it.

e.g. (if you were working in degrees)

startAngle = 359
endAngle = 2
difference = startAngle - endAngle
print(difference) # 357

if difference > 180:
  difference -= 360
elif difference < -180
  difference += 360

print(difference)  # -3
1 Like

better work with quaternions than eulers when you are covering a whole circle. The first is direction-free and so you wont have the situation of having 358 having to go down to zero rather than just having to add 2 to sum 360