' Provided by Vincent DeCampo 12/28/03
Option Explicit
Public Const pi = 3.1415927
Global COS_LUT(359) As Single
Global SIN_LUT(359) As Single
' Provided by Vincent DeCampo 12/28/03
'
' This function pre-calculates the SIN/COS of every angle from 0-359 degrees.
' Instead of using the SIN/COS function you can simply access the array of
' values to get the desired angle. (ie: COS_LUT(45) gives the Cosine of 45 degrees.
'
Public Sub Calc_SIN_COS_LUT()
Dim Counter As Long
For Counter = 0 To 359 Step 1
COS_LUT(Counter) = Cos(Deg2Rad(CSng(Counter)))
SIN_LUT(Counter) = Sin(Deg2Rad(CSng(Counter))) * -1
Next Counter
End Sub
' Provided by Vincent DeCampo 12/28/03
'
' This function will calculate the linear distance between any 2 sets of coordinates.
' Useful for when you want to activate effects or other sprites based on proximity.
'
Public Function Distance(x1 As Single, y1 As Single, x2 As Single, y2 As Single) As Single
Distance = Sqr((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
End Function
' Provided by Vincent DeCampo 12/28/03
'
' This function will return the angle from origin 0,0 to X,Y
' Useful to figure out in which direction a set of coordinates
' is from another. By taking 2 sets of coordinates and normalizing them
' (ie: subtract X2-X1, Y2-Y1) will let you find the angle of any coordinate (X1,Y1)
' to any other coordinate. (X2,Y2)
'
Public Function OriginAngle(X As Single, Y As Single) As Single
Dim Quadrant As Single
Dim Angle As Single
Dim Hyp As Single
Dim Leg As Single
If (X = 0) And (Y = 0) Then Exit Function
Hyp = Distance(0, 0, X, Y)
Leg = Distance(X, 0, X, Y)
If Not (Hyp = 0) Then
If (X >= 0) And (Y <= 0) Then
OriginAngle = ArcSin((1) * Leg / Hyp)
ElseIf (X <= 0) And (Y <= 0) Then
OriginAngle = pi - ArcSin((1) * Leg / Hyp)
ElseIf (X <= 0) And (Y >= 0) Then
OriginAngle = ArcSin((1) * Leg / Hyp) + pi
ElseIf (X >= 0) And (Y >= 0) Then
OriginAngle = (2 * pi) - ArcSin((1) * Leg / Hyp)
End If
End If
End Function
' Provided by Vincent DeCampo 12/28/03
'
' This function converts Degrees to Radians
'
Public Function Deg2Rad(Deg As Single) As Single
Deg2Rad = 6.28 * (Deg / 360)
End Function
' Provided by Vincent DeCampo 12/28/03
'
' This function converts Radians to Degrees
'
Public Function Rad2Deg(Rad As Single) As Single
Rad2Deg = 360 * (Rad / (pi * 2))
End Function
' Provided by Vincent DeCampo 12/28/03
'
' This function calculates the ArcSin
'
Public Function ArcSin(arg As Single) As Single
If arg > 1 Or arg < -1 Then 'outside domain!
ArcSin = 0
Exit Function
End If
If arg = 1 Then 'div by 0 checks
ArcSin = pi / 2#
Exit Function
End If
If arg = -1 Then
ArcSin = pi / -2#
Else
ArcSin = Atn(arg / Sqr(1 - (arg ^ 2)))
End If
End Function
' Provided by Vincent DeCampo 12/28/03
'
' This function will take a set of coordinates and rotate them around
' an origin 0,0 by so many degrees and return a NewX, NewY set of coordinates.
' You can use the Normalizing technique mentioned earlier to convert any set of
' coordinates to origin 0,0.
'
Public Sub RotateCoords(X As Single, Y As Single, degRotation As Single, NewX As Single, NewY As Single)
Dim Radius As Single
Dim Angle As Single
Dim NewAngle As Single
Dim RotAngle As Single
If (degRotation > 359) Or (degRotation < -359) Then Exit Sub
Radius = Distance(0, 0, X, Y)
Angle = OriginAngle(X, Y)
RotAngle = Deg2Rad(degRotation)
NewAngle = Angle + RotAngle
NewX = Cos(NewAngle) * Radius
NewY = Sin(NewAngle) * Radius
End Sub