Android Question B4xview SetRotationAnimated optimal rotation

Angel Maza

Member
Licensed User
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
 

Angel Maza

Member
Licensed User
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!!).

Thanks.
Best regards.
 

Erel

Administrator
Staff member
Licensed User
I understand.

B4X:
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
 

Angel Maza

Member
Licensed User
Works perfect!!. Thanks a lot.

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
 
Top