[Gambas-user] angle interpolation algorithm troubles
Kevin Fishburne
kevinfishburne at ...1887...
Fri Feb 17 06:08:27 CET 2012
Apparently I'm too much of a dummy to figure this out. I have two
singles representing orientation in degrees, let's say Angle1 and
Angle2. I need to continually increment one (let's say Angle1) until
it's close to the value of the other (Angle2), but in the direction of
the shortest distance between the two angles.
So if Angle1 = 10 and Angle2 = 350, Angle1 should be repeatedly
decremented, wrapping around the upper limit of 360 (10, 5, 0, 355,
350). If Angle1 was 350 and Angle2 was 20, Angle1 would be incremented
like 350, 355, 0, 5, 10, 15, 20. I have a crude function to wrap a
number which may be of use:
Public Function Wrap_Single(Minimum As Single, Maximum As Single, Value
As Single) As Single
' Constrain value to specified range.
' General declarations.
Dim Spread As Single = Maximum - Minimum + 1 ' Range between minimum
and maximum values.
' Wrap if value is too large.
If Value > Maximum Then
Do
Value = Value - Spread
Loop Until Value <= Maximum
Endif
' Wrap if value is too small.
If Value < Minimum Then
Do
Value = Value + Spread
Loop Until Value >= Minimum
Endif
' Return wrapped value.
Return Value
End
This is my current code, which works except the Distance variable isn't
doing its job:
' Check if camera orientation is locked to client player orientation.
If Camera.Locked Then
' Check if difference in orientations exceeds interpolation threshold.
Distance = Abs(Camera.Orientation -
Client.Player[Client.Number].Orientation)
If Distance >= 5.625 Then
' Move camera orientation closer to player orientation (interpolate).
If Camera.Orientation - Client.Player[Client.Number].Orientation <
Client.Player[Client.Number].Orientation - Camera.Orientation Then
Camera.Orientation = Convert.Wrap_Single(0, 359,
Camera.Orientation + 0.1 * Distance)
Else
Camera.Orientation = Convert.Wrap_Single(0, 359,
Camera.Orientation - 0.1 * Distance)
Endif
Endif
Endif
My instinct tells me I'm almost there but am missing one critical thing.
If someone smarter than myself instantly sees my mistake please let me
know. :)
--
Kevin Fishburne
Eight Virtues
www: http://sales.eightvirtues.com
e-mail: sales at ...1887...
phone: (770) 853-6271
More information about the User
mailing list