Android Question B4xview SetRotationAnimated optimal rotation

Angel Maza

Member
Licensed User
Longtime 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
Longtime 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.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime 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
 
Upvote 0

Angel Maza

Member
Licensed User
Longtime 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
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I attach the code with degrees in a float variable, so it's consistent with the SetRotationAnimated and also doesn't do small steps.
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).
 
Upvote 0
Top