When there are more than 180 degrees of relative rotation, or going from 350 to 10, the displacement goes allways from angle1 to angle2, avoiding crossing 0.
The problem is when I cross 0 degrees (from 350 to 10, or 10 to 350 by example), the rotation is done without crossing 0, doing a large rotation (very ugly!!).
Sub RotateViewShortestArc (v As B4XView, Duration As Int, Target As Int)
Dim Rotation As Int = v.Rotation
Dim dx As Int = (Target - Rotation) Mod 360
If dx > 180 Then
dx = -(360 - dx)
Else if dx < -180 Then
dx = 360 + dx
End If
v.SetRotationAnimated(Duration, Rotation + dx)
End Sub
I attach the code with degrees in a float variable, so it's consistent with the SetRotationAnimated and also doesn't do small steps.
B4X:
Sub RotateViewShortestArc (v As B4XView, Duration As Int, Target As Float)
Dim Rotation As Float = v.Rotation
Dim dx As Float = (Target - Rotation) Mod 360
If dx > 180 Then
dx = -(360 - dx)
Else if dx < -180 Then
dx = 360 + dx
End If
v.SetRotationAnimated(Duration, Rotation + dx)
End Sub
This is fine. The reason that I used ints is to make it compatible with B4i. Mod in B4i expects two ints (you can use Bit.FMod if you want to use floats).