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.
Best regards.
Angel
Best regards.
Angel
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
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).I attach the code with degrees in a float variable, so it's consistent with the SetRotationAnimated and also doesn't do small steps.