Results 1 to 9 of 9

Thread: working out an angle from XY

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2001
    Location
    UK - Liverpool
    Posts
    113

    working out an angle from XY

    For some game i'm creating, im trying to work out the angle the object is moving towards. Using the current X and Y and the old X an Y values, how could i get this?

    thanks
    My software never has bugs. It just develops random features...

  2. #2
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    350
    angle = arctan (deltaY/deltaX)

    where the deltas are the change in x & y.

    (At least, if I understand the question correctly)
    .

  3. #3
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    .. but don't forget to cover for situation when deltax=0 !

    I know, I'm smartassing....
    but I had to work around such a thing just yesterday, the error was clearly found, but nothing could be done to the executable. So I had to make sure the situation didn't occur from the user side. Two hours of work, just because those authors were so st....!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    Here is the function I use, I wrote in QB and it can probably be done better.

    VB Code:
    1. FUNCTION Target (SourceX, SourceY, DestX, DestY)
    2. DIM Vertical AS SINGLE
    3. DIM Horizontal AS SINGLE
    4. DIM Diagonal AS SINGLE
    5. DIM Vector AS SINGLE
    6. Horizontal = ABS(SourceX - DestX)
    7. Vertical = ABS(SourceY - DestY)
    8. IF Horizontal AND Vertical <> 0 THEN
    9.  Diagonal = SQR((Horizontal ^ 2) + (Vertical ^ 2))
    10.  Vector = (Arcsin(Vertical / Diagonal))
    11.  IF DestX > SourceX AND DestY > SourceY THEN Vector = 0 - Vector: Vector = Vector + 90
    12.  IF DestX > SourceX AND DestY < SourceY THEN Vector = Vector + 90
    13.  IF DestX < SourceX AND DestY > SourceY THEN Vector = Vector + 270
    14.  IF DestX < SourceX AND DestY < SourceY THEN Vector = 180 - Vector: Vector = Vector + 90
    15. ELSE
    16.  IF Horizontal = 0 AND DestY > SourceY THEN Vector = 0
    17.  IF Horizontal = 0 AND DestY < SourceY THEN Vector = 180
    18.  IF Vertical = 0 AND DestX > SourceX THEN Vector = 90
    19.  IF Vertical = 0 AND DestX < SourceX THEN Vector = 270
    20.  IF Vertical = 0 AND Horizontal = 0 THEN Vector = 0: Target = 0: EXIT FUNCTION
    21. END IF
    22. DO UNTIL Vector <= 360 AND Vector > 0
    23.  IF Vector <= 0 THEN Vector = Vector + 360
    24.  IF Vector > 360 THEN Vector = Vector - 360
    25. LOOP
    26. Target = Vector
    27. END FUNCTION
    Don't pay attention to this signature, it's contradictory.

  5. #5
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Izt won't work since any Trig formula in VB will use/give radians NOT degrees!
    Also keep in mind not everybody uses angels from 0 to 359, with 0 is North and 90 is East.
    some are using a 0 that goes to the East and count than towards North !!
    Believe me, I have to work with some of them, WEIRD!!!
    I'm using this:
    VB Code:
    1. Public Sub Bearing(X1, Y1, X2, Y2, Bearing)
    2. 'Input X1, Y1, X2, Y2
    3. 'Output Bearing (0-360)
    4. 'Calculates Bearing from X1,Y1 to X2,Y2 using 360 degree-system
    5. Const PI = 3.141592654
    6. Dim dx As Single
    7. Dim dy As Single
    8. dx = X2 - X1
    9. dy = Y2 - Y1
    10. If dy <> 0 Then
    11.         bearing = (Atn(dx / dy) * 180 / PI)
    12.         If X2 > X1 Then
    13.                 If Y2 > Y1 Then
    14.                         bearing = bearing
    15.                 Else
    16.                        bearing = 180 +bearing
    17.                 End If
    18.         Else
    19.                 If Y2 > Y1 Then
    20.                         bearing = 360 + bearing
    21.                 Else
    22.                         bearing = 180 + bearing
    23.                 End If
    24.         End If
    25. Else
    26.         If X1 > X2 Then
    27.                 bearing = 270
    28.         Else
    29.                 bearing = 90
    30.         End If
    31. End If
    32. End Sub
    Last edited by opus; Jan 30th, 2003 at 04:08 PM.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  6. #6
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    Actually I put Rad() and Deg() functions in all my programs, using them everytime I call a sin/cos.. although I suppose it would make more sense to only call once
    Don't pay attention to this signature, it's contradictory.

  7. #7
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    You are using
    VB Code:
    1. Vector = (Arcsin(Vertical / Diagonal))
    2. IF DestX > SourceX AND DestY > SourceY THEN Vector = 0 - Vector: Vector = Vector + 90
    So Vector is in radiants, and the following line uses Vector as it would be in Degrees!!!!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  8. #8
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    My arcsin function (since Basics don't include them ) has the deg() built into it.
    Don't pay attention to this signature, it's contradictory.

  9. #9
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Good idea, and yes I did miss the point that you were using a not built-in Function (ARCSIN).
    Shouldn't you have been telling that?
    Note:
    After doing the testing on a simulation software for three years now, I've probably seen all kinds of false angle display's because of a mixed use of degrees and radians.
    I hate those.........
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width