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