locked
Facing Direction RRS feed

  • Question

  • How do I calculate the direction an object is facing in degrees, if object A is at x1, y1 and facing object B which is at x2, y2?
    Friday, May 8, 2009 7:40 PM

Answers

  • That didn't work, but I figured it out in the end, this worked for me perfectly.

    x2 = x2 - x1
    y2 = y2 - y1
    Dim p As Single = Math.Round((180 / Math.PI) * Math.Atan2(X, Y))
    If p < 0 Then
        p = 180 + Math.Abs(180 - Math.Abs(p))
    End If
    • Marked as answer by Rehabilitated Saturday, May 9, 2009 10:16 AM
    Saturday, May 9, 2009 10:16 AM

All replies

  • Try this out.

            Const RADIANS_TO_DEGREES As Double = 180 / Math.PI
    
            Dim pt1 As PointF = New PointF(5, 5)
            Dim pt2 As PointF = New PointF(0, 10)
            Dim vector As SizeF = New SizeF(pt2.X - pt1.X, pt2.Y - pt1.Y)
            Dim dir As Single = CSng(Math.Atan2(vector.Height, vector.Width) * RADIANS_TO_DEGREES)
            dir = 90 - dir  'Make relative to direction facing.
            If dir < 0 Then 'Convert negative direction to equivalent in [0,360).
                dir += 360
            End If
    
            'Prints: Bearing 315 degrees.
            Debug.WriteLine(String.Format("Bearing {0} degrees.", dir))
    Friday, May 8, 2009 11:13 PM
  • That didn't work, but I figured it out in the end, this worked for me perfectly.

    x2 = x2 - x1
    y2 = y2 - y1
    Dim p As Single = Math.Round((180 / Math.PI) * Math.Atan2(X, Y))
    If p < 0 Then
        p = 180 + Math.Abs(180 - Math.Abs(p))
    End If
    • Marked as answer by Rehabilitated Saturday, May 9, 2009 10:16 AM
    Saturday, May 9, 2009 10:16 AM