Page 1 of 1

Need help with math function

Posted: April 6th, 2007, 6:48 pm
by slam666
Hi,

I need some help with a math function to calculate the angle to a point (x,y)

Here is my function from EQ2 but it does not seem to work in VG. Any help will be appreciated.
If I call CalcTargetAngle(x, y) I get an angle but its not right.

Thanks.

==============================
Function CalculateAngle(dx, dy, dis)
Dim tmpFloat

'objDebug.Print "In CalculateAngle(" & dx & ", " & dy & ", " & dis & ")", 2
if dis=0 then
'objDebug.Print "***WARNING: dis=0 - can't calculate angle", 0
CalculateAngle=0
exit Function
end if

tmpFloat = ArcSin(Abs(dx) / Abs(dis))

if (dx >= 0) and (dy >= 0) then
' Do nothing to tmpFloat
end if
if (dx >= 0) and (dy < 0) then
tmpFloat = PI - tmpFloat
end if
if (dx < 0) and (dy < 0) then
tmpFloat = PI + tmpFloat
end if
if (dx <0>= 0) then
tmpFloat = (2 * PI) - tmpFloat
end if

CalculateAngle = Round(tmpFloat * (180 / PI))
end Function
===============================
Function CalcTargetAngle(x, y)
Dim dx, dy, dis

dx = (-x) - (-getPlayerX())
dy = (-y) - (-getPlayerY())

dis = Sqr(dx*dx + dy*dy)
'dis = Round(Sqr(dx*dx + dy*dy))

' Return Angle
CalcTargetAngle=CalculateAngle(dx, dy, dis)
end Function

Posted: April 6th, 2007, 7:54 pm
by gheezer
It looks like you need to add a 90 degree offset to the result. Also, this function returns the absolute angle, not the relative angle if thats what your looking for.

This is what I use to calculate absolute angle:

Code: Select all

Function CalcAbsAngleToXY(x, y)
    Dim mh
    Dim dx, dy
    Dim lAngle

    dx = CDbl(x - getPlayerX)
    dy = CDbl(y - getPlayerY)
 
    If dx = 0 Then
        If dy = 0 Then
            mh = 0
        ElseIf dy > 0 Then
            mh = PI/2.0 
        Else
            mh = PI*3.0/2.0 
        End If
    ElseIf dy = 0 Then
        If dx < 0 Then
            mh = 0
        Else
            mh = PI
        End If
    Else
        If dx < 0 Then
            mh = atn(dy/dx) + PI
        ElseIf dy < 0 Then
            mh = atn(dy/dx) + 2.0*PI 
        Else
            mh = atn(dy/dx) 
        End If            
    End If

    mh = ((mh * 180.0 / PI) + 90) Mod 360

    CalcAbsAngleToXY = mh
End Function

Posted: April 7th, 2007, 5:52 am
by slam666
Gheezer wrote:It looks like you need to add a 90 degree offset to the result. Also, this function returns the absolute angle, not the relative angle if thats what your looking for.

This is what I use to calculate absolute angle:

Code: Select all

Function CalcAbsAngleToXY(x, y)
    Dim mh
    Dim dx, dy
    Dim lAngle

    dx = CDbl(x - getPlayerX)
    dy = CDbl(y - getPlayerY)
 
    If dx = 0 Then
        If dy = 0 Then
            mh = 0
        ElseIf dy > 0 Then
            mh = PI/2.0 
        Else
            mh = PI*3.0/2.0 
        End If
    ElseIf dy = 0 Then
        If dx < 0 Then
            mh = 0
        Else
            mh = PI
        End If
    Else
        If dx < 0 Then
            mh = atn(dy/dx) + PI
        ElseIf dy < 0 Then
            mh = atn(dy/dx) + 2.0*PI 
        Else
            mh = atn(dy/dx) 
        End If            
    End If

    mh = ((mh * 180.0 / PI) + 90) Mod 360

    CalcAbsAngleToXY = mh
End Function
Thanks Gheezer, work like a charm.