I'm trying to figure out a nice way to interpolate rotation without it flipping. I have a working code for it, but just have this nudging feeling that there is a better way to do it.
Here is what I have now.
The Rotation input variable on the second method is just a flag to tell it if rotation is being interpolated, so that it can do some stuff to avoid flipping. The Update method take any number and moves it towards a goal with a physics like lag.
Is there a cleaner way to do this? The "If Angle = "nan" feels a bit hacky, and the whole thing that flips the rotation with lots of If's in the second Method too.
Cheers,
Ragnar
Here is what I have now.
Method AngleBetween:Float( x1:Float, y1:Float, x2:Float, y2:Float ) Local xd:Float = (x1-x2) Local yd:Float = (y1-y2) Local m:Float = 90 If xd < 0 Then m = 270 Local Angle:Float = ATan(yd/xd) + m If Angle = "nan" Then Return 0 Else Return Angle End Method ' And elsewhere in the code Method Update( Rotation:Int = 0) If Rotation And (Abs(Goal-Value)) > 180 Then If Value > 360 Then Value:- 360 If Value < 0 Then Value:+ 360 If Goal > 180 Then Goal:- 360 Else Goal:+ 360 End If Local Distance:Float = Goal - Value Local Acceleration:Float = (Distance - Vel*Lag)*2/(Lag*Lag) Vel:+ Acceleration Value:+ Vel End Method
The Rotation input variable on the second method is just a flag to tell it if rotation is being interpolated, so that it can do some stuff to avoid flipping. The Update method take any number and moves it towards a goal with a physics like lag.
Is there a cleaner way to do this? The "If Angle = "nan" feels a bit hacky, and the whole thing that flips the rotation with lots of If's in the second Method too.
Cheers,
Ragnar