Post Reply Home » Forums » Vanguard » VGExtreme General Discussion

Need help with math function : VGExtreme General Discussion

Posted: April 6th, 2007
User avatar
Total Posts:1184 Joined:2005
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

_________________
Slam666

Author of EQ2Ultrabot, NOW WITH CRAFTING!!!
Read this before asking for help
Posted: April 6th, 2007
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
User avatar
Total Posts:1184 Joined:2005
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.

_________________
Slam666

Author of EQ2Ultrabot, NOW WITH CRAFTING!!!
Read this before asking for help
Ready to join the community? Click here and see all of the benefits!
blue large dotWho is online
Users browsing this forum: No registered users and 4 guests
Post Reply