VB - Face target given its x and y menu

Shout-Out

User Tag List

Results 1 to 15 of 15
  1. #1
    erix920's Avatar Private
    Reputation
    4
    Join Date
    Mar 2011
    Posts
    11
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    VB - Face target given its x and y

    I've seen a few people asking if there's any source code to face a target. I think there's some sources out there but all are built around other libraries. The code I wrote just needs you to edit a few things (3 lines) so that it knows your player's x,y and rotation. I'm using this code in my upcoming bot and it's used for auto attack and it works so well I couldn't keep my hard work all bottled up.

    I hope someone can find a use for it.

    Code:
    #Region " Change Angle "
    
        ''' <summary>
        ''' Finds what quadrant of the unit circle the given coordinates are in and returns the proper angle in radians
        ''' </summary>
        ''' <param name="x"></param>
        ''' <param name="y"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Shared Function Find_Quadrant(ByVal x As Single, ByVal y As Single)
            If x > 0 And y > 0 Then
                Return 1.5 * Math.PI
            ElseIf x < 0 And y > 0 Then
                Return 0
            ElseIf x < 0 And y < 0 Then
                Return Math.PI / 2
            Else
                Return Math.PI
            End If
        End Function
    
        ''' <summary>
        ''' !!!!!!!!!!!!!!DO NOT EDIT THIS!!!!!!!!!!!!!!
        ''' This function changes the player's facing value by holding down the turnleft or turnright key until the player is facing the target. 
        ''' </summary>
        ''' <param name="object_x">Object's X Coordinate</param>
        ''' <param name="object_y">Object's Y Coordinate</param>
        ''' <remarks></remarks>
        Private Sub Change_Angle(ByRef inst As InstanceStruct, ByVal object_x As Single, ByVal object_y As Single)
            Dim player_x As Single = inst.p_BRObjectManager.Me.X
            Dim player_y As Single = inst.p_BRObjectManager.Me.Y
            Dim x3, y3 As Single
            Dim differenceX As Single = object_x - player_x
            Dim differenceY As Single = object_y - player_y
            Dim quadrant, sec, quadint As Single
            Dim kb_delay As Integer
            Dim kb_speed As Integer
            Dim player_Facing As Single = inst.p_BRObjectManager.Me.Facing
    
            SystemParametersInfo(SPI_GETKEYBOARDDELAY, 0, kb_delay, 0)
            SystemParametersInfo(SPI_GETKEYBOARDSPEED, 0, kb_speed, 0)
    
            If differenceX > 0 And differenceY > 0 Then
                quadrant = 1.5 * Math.PI
                sec = Math.PI
                quadint = 1
                x3 = object_x
                y3 = player_y
            ElseIf differenceX < 0 And differenceY > 0 Then
                quadrant = 0
                quadint = 2
                sec = Math.PI / 2
                x3 = player_x
                y3 = object_y
            ElseIf differenceX < 0 And differenceY < 0 Then
                quadrant = Math.PI / 2
                sec = Math.PI
                quadint = 3
                x3 = object_x
                y3 = player_y
            Else
                quadrant = Math.PI
                sec = Math.PI / 1.5
                quadint = 4
                x3 = player_x
                y3 = object_y
            End If
    
            Dim dPlayer2Target As Single = Math.Sqrt(differenceX ^ 2 + differenceY ^ 2)
            Dim dThird2Target As Single = Math.Sqrt((object_x - x3) ^ 2 + (object_y - y3) ^ 2)
    
            Dim quad_ang As Single = Find_Quadrant(object_x - player_x, object_y - player_y)
            Dim total_ang As Single = Math.Abs(Math.Asin(dThird2Target / dPlayer2Target))
    
            If quadint = 2 Then
                sec = (quadrant + total_ang) - (player_Facing - (Math.PI / 2)) - 0.15
                If player_Facing > 0 And player_Facing <= Math.PI / 2 Then
                    If sec > Math.PI Then ' This will give us the smallest turning angle
                        sec = 2 * Math.PI - sec
                        sec = sec / -1
                        sec = sec
                    End If
                ElseIf player_Facing > Math.PI / 2 And player_Facing <= Math.PI Then
                    sec = sec + 0.3
                ElseIf player_Facing > Math.PI And player_Facing <= Math.PI * 1.5 Then
                    sec = sec + 0.3
                ElseIf player_Facing > Math.PI * 1.5 And player_Facing <= Math.PI * 2 Then
                    If Math.Abs(sec) > Math.PI Then ' This will give us the smallest turning angle  -- or atleast I hope
                        sec = 2 * Math.PI - Math.Abs(sec)
                        'sec = sec / -1
                        'sec = sec
                    End If
                End If
            ElseIf quadint = 3 Then
                If player_Facing > 0 And player_Facing <= Math.PI / 2 Then
                    sec = (quadrant + total_ang) - (player_Facing - (Math.PI / 2)) - 0.15
                ElseIf player_Facing > Math.PI And player_Facing <= Math.PI * 1.5 Then
                    sec = (quadrant + total_ang) - (player_Facing - Math.PI / 2)
                    If sec > 0 Then
                        sec = sec - 0.15
                    Else
                        sec = sec + 0.15
                    End If
                ElseIf player_Facing > Math.PI * 1.5 And player_Facing <= Math.PI * 2 Then
                    sec = (quadrant + total_ang) - (player_Facing - Math.PI / 2) + 0.15
                Else
                    sec = (quadrant + total_ang) - (player_Facing - (Math.PI / 2)) - 0.15
                End If
            ElseIf quadint = 4 Then
                If player_Facing > 0 And player_Facing <= Math.PI / 2 Then
                    sec = (quadrant + total_ang) - (player_Facing - Math.PI / 2) - 0.15
                ElseIf player_Facing > Math.PI * 1.5 And player_Facing <= Math.PI * 2 Then
                    sec = (quadrant + total_ang) - (player_Facing - Math.PI / 2) + 0.15
                Else
                    sec = (quadrant + total_ang) - (player_Facing - (Math.PI / 2)) - 0.15
                End If
            ElseIf quadint = 1 Then
                sec = (quadrant + total_ang) - (player_Facing - (Math.PI / 2)) - 0.15
                If player_Facing > 0 And player_Facing <= Math.PI / 2 Then
                    If sec > Math.PI Then ' This will give us the smallest turning angle
                        sec = 2 * Math.PI - sec
                        sec = sec / -1
                        sec = sec
                    End If
                    If sec > 0 Then
                        sec = sec - 0.15
                    Else
                        sec = sec + 0.15
                    End If
                ElseIf player_Facing > Math.PI / 2 And player_Facing <= Math.PI Then
                    If sec > Math.PI Then ' This will give us the smallest turning angle
                        sec = 2 * Math.PI - sec
                        sec = sec / -1
                        sec = sec + 0.3
                    End If
                ElseIf player_Facing > Math.PI And player_Facing <= Math.PI * 1.5 Then
                    If sec > Math.PI Then ' This will give us the smallest turning angle
                        sec = 2 * Math.PI - sec
                        sec = sec / -1
                        sec = sec + 0.3
                    End If
                End If
            End If
            Dim changeAngle As Single = player_Facing - ((sec) - total_ang + quadrant)
    
            If sec > 0 Then
                'MsgBox("Move left: " & sec)
                Dim lastAngle, firstAngle, additive As Single
                Do Until Math.Abs(sec) < additive
                    firstAngle = inst.p_BRObjectManager.Me.Facing
    
                    For Each key As Integer In thisInst.p_WowBindings.KEY_TurnLeft
                        SendMessage(inst.p_WowProcess.MainWindowHandle, WM_KEYDOWN, key, 0)
                    Next
    
                    Threading.Thread.Sleep(kb_delay + kb_speed)
                    lastAngle = inst.p_BRObjectManager.Me.Facing
                    If lastAngle < firstAngle Then
                        lastAngle += 2 * Math.PI
                    End If
                    additive += (lastAngle - firstAngle)
    
                    If halt = True Then
                        Exit Do
                    End If
                Loop
    
                For Each key As Integer In thisInst.p_WowBindings.KEY_TurnLeft
                    SendMessage(inst.p_WowProcess.MainWindowHandle, WM_KEYUP, key, 0)
                Next
            Else
                'MsgBox("Move right: " & sec)
                Dim lastAngle, firstAngle, additive As Single
                Do Until Math.Abs(sec) < additive
                    firstAngle = inst.p_BRObjectManager.Me.Facing
    
                    For Each key As Integer In thisInst.p_WowBindings.KEY_TurnRight
                        SendMessage(inst.p_WowProcess.MainWindowHandle, WM_KEYDOWN, key, 0)
                    Next
    
                    Threading.Thread.Sleep(kb_delay + kb_speed)
                    lastAngle = inst.p_BRObjectManager.Me.Facing
                    If lastAngle > firstAngle Then
                        lastAngle += 2 * Math.PI
                    End If
                    additive += (firstAngle - lastAngle)
    
                    If halt = True Then
                        Exit Do
                    End If
                Loop
    
                For Each key As Integer In thisInst.p_WowBindings.KEY_TurnRight
                    SendMessage(inst.p_WowProcess.MainWindowHandle, WM_KEYUP, key, 0)
                Next
    
            End If
            inst.next_Object.angleChanged = True
        End Sub
    #End Region

    VB - Face target given its x and y
  2. #2
    MaiN's Avatar Elite User
    Reputation
    335
    Join Date
    Sep 2006
    Posts
    1,047
    Thanks G/R
    0/10
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    float facingRequired = (float)Math.Atan2(targetY - myY, targetX - myX);
    There is no need to figure out the quadrants etc. yourself. Most libraries have atan2 built in.
    [16:15:41] Cypher: caus the CPU is a dick
    [16:16:07] kynox: CPU is mad
    [16:16:15] Cypher: CPU is all like
    [16:16:16] Cypher: whatever, i do what i want

  3. #3
    erix920's Avatar Private
    Reputation
    4
    Join Date
    Mar 2011
    Posts
    11
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now I have to see if the values are the same. I'll let you know how mad I am after.

  4. #4
    streppel's Avatar Active Member
    Reputation
    78
    Join Date
    Mar 2007
    Posts
    196
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    they are for sure,atan2 has the quadrant handling in by default. you can check msdn for reference on what the returnvalues of the function represent
    on some games you have to adjust this the return value a little(like making it go from 0 to 2pi or from 0 to 1 etc) but i guess in wow it works straight away

  5. #5
    erix920's Avatar Private
    Reputation
    4
    Join Date
    Mar 2011
    Posts
    11
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Didn't know there was a function that did the same thing as my 4 lines did.
    Last edited by erix920; 06-28-2011 at 04:12 PM. Reason: i checked msdn

  6. #6
    EmilyStrange's Avatar Active Member
    Reputation
    34
    Join Date
    Jul 2009
    Posts
    125
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by erix920 View Post
    Atan2 is just a replacement for the find_quadrant function though correct?
    Trigonometry Education FTW

    http://www.amazon.com/Fundamentals-M.../dp/0131687425

    http://www.amazon.com/Beginning-Math.../dp/0735713901

    http://www.amazon.com/exec/obidos/ASIN/0123742978

  7. #7
    erix920's Avatar Private
    Reputation
    4
    Join Date
    Mar 2011
    Posts
    11
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the wildly pointless post.

    I'm pretty sure writing out a function that does the job shows more understanding than using math.atan2.

    Edit: Those amazon books are too expensive anyway
    Last edited by erix920; 06-28-2011 at 04:18 PM.

  8. #8
    Azzie2k8's Avatar Member
    Reputation
    11
    Join Date
    Apr 2009
    Posts
    190
    Thanks G/R
    0/0
    Trade Feedback
    1 (100%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by erix920 View Post
    I'm pretty sure writing out a function that does the job shows more understanding than using math.atan2.
    I'm not sure about that...

  9. #9
    EmilyStrange's Avatar Active Member
    Reputation
    34
    Join Date
    Jul 2009
    Posts
    125
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by erix920 View Post
    I'm pretty sure writing out a function that does the job shows more understanding than using math.atan2.
    Yes and no.

    In the same way that the mental exercise of writing your own compilers and interpreters is useful for understanding, but doing so due to ignorance of other solutions, especially of such an obvious piece of functionality, is just willful. You clearly demonstrate this willfulness in the re-invention of Atan2 and the claim that knowledge is expensive, hence you will be doomed to repeat this cycle in the future.

  10. #10
    erix920's Avatar Private
    Reputation
    4
    Join Date
    Mar 2011
    Posts
    11
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your post are always so detailed EmilyStrange.

    Regardless of my apparent ignorance of using frameworked functions, I still donate this code to anyone who doesn't want to spend the time figuring it out.

    Maybe they can reimplement using math.atan2 to clean things up.

  11. #11
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1358
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by EmilyStrange View Post
    Yes and no.

    In the same way that the mental exercise of writing your own compilers and interpreters is useful for understanding, but doing so due to ignorance of other solutions, especially of such an obvious piece of functionality, is just willful. You clearly demonstrate this willfulness in the re-invention of Atan2 and the claim that knowledge is expensive, hence you will be doomed to repeat this cycle in the future.
    Perfect explanation. I'm always entertained whenever I point out that someone has reinvented a standard library function in their code (and done it in an inferior fashion usually) and they reply by saying it makes them "less noob" or some crap like that. Great for learning, not so much for actually writing production code.

    @erix920: I'm not saying that's what you did in this particular case. It's just what happens more often than not.

  12. #12
    EmilyStrange's Avatar Active Member
    Reputation
    34
    Join Date
    Jul 2009
    Posts
    125
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Cypher View Post
    Perfect explanation. I'm always entertained whenever I point out that someone has reinvented a standard library function in their code (and done it in an inferior fashion usually) and they reply by saying it makes them "less noob" or some crap like that. Great for learning, not so much for actually writing production code.

    @erix920: I'm not saying that's what you did in this particular case. It's just what happens more often than not.
    Who are you and what have you done with Cypher's body?

  13. #13
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1358
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by EmilyStrange View Post
    Who are you and what have you done with Cypher's body?
    I think someone put benzodiazepines in my breakfast.

  14. #14
    123jokesonme's Avatar Corporal
    Reputation
    1
    Join Date
    Jun 2011
    Posts
    20
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Cypher View Post
    I think someone put benzodiazepines in my breakfast.
    haha i think "someone" forced me to take these to xanax bars to! =]

  15. #15
    erix920's Avatar Private
    Reputation
    4
    Join Date
    Mar 2011
    Posts
    11
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree with you all, using library code is better than expanding it and making it less efficient (or the same as). In my case it probably makes it confusing.

    Anyway if anyone has any questions about the missing API just ask.

Similar Threads

  1. [Release] 1.12.1.5875 face target fail
    By wei3470231 in forum WoW Memory Editing
    Replies: 3
    Last Post: 05-12-2016, 10:20 AM
  2. how to always face target? Lazybot + PQR
    By jaeson in forum WoW Bots Questions & Requests
    Replies: 0
    Last Post: 07-23-2012, 04:09 PM
  3. addon to face target?
    By v3ntrix in forum WoW Bots Questions & Requests
    Replies: 0
    Last Post: 06-11-2010, 03:53 AM
  4. Replies: 12
    Last Post: 03-16-2008, 12:32 AM
  5. Its back and better than before!! [Z O]
    By 2dgreengiant in forum Gaming Chat
    Replies: 35
    Last Post: 08-26-2007, 10:49 AM
All times are GMT -5. The time now is 08:19 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search