# Android QuestionB4xview SetRotationAnimated optimal rotation

#### Angel Maza

##### Member
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

#### Erel

##### B4X founder
Staff member
Longtime User
You never want to cross 0 degrees?

#### Angel Maza

##### Member
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.

#### Erel

##### B4X founder
Staff member
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``````

#### Angel Maza

##### Member
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``````

#### Erel

##### B4X founder
Staff member
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).

Replies
39
Views
6K
Replies
7
Views
1K
Replies
26
Views
7K
Replies
2
Views
954
Replies
34
Views
51K