|
-
Jan 30th, 2003, 08:00 AM
#1
Thread Starter
Lively Member
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...
-
Jan 30th, 2003, 08:15 AM
#2
Hyperactive Member
angle = arctan (deltaY/deltaX)
where the deltas are the change in x & y.
(At least, if I understand the question correctly)
-
Jan 30th, 2003, 11:59 AM
#3
.. 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!
-
Jan 30th, 2003, 03:16 PM
#4
Fanatic Member
Here is the function I use, I wrote in QB and it can probably be done better.
VB Code:
FUNCTION Target (SourceX, SourceY, DestX, DestY)
DIM Vertical AS SINGLE
DIM Horizontal AS SINGLE
DIM Diagonal AS SINGLE
DIM Vector AS SINGLE
Horizontal = ABS(SourceX - DestX)
Vertical = ABS(SourceY - DestY)
IF Horizontal AND Vertical <> 0 THEN
Diagonal = SQR((Horizontal ^ 2) + (Vertical ^ 2))
Vector = (Arcsin(Vertical / Diagonal))
IF DestX > SourceX AND DestY > SourceY THEN Vector = 0 - Vector: Vector = Vector + 90
IF DestX > SourceX AND DestY < SourceY THEN Vector = Vector + 90
IF DestX < SourceX AND DestY > SourceY THEN Vector = Vector + 270
IF DestX < SourceX AND DestY < SourceY THEN Vector = 180 - Vector: Vector = Vector + 90
ELSE
IF Horizontal = 0 AND DestY > SourceY THEN Vector = 0
IF Horizontal = 0 AND DestY < SourceY THEN Vector = 180
IF Vertical = 0 AND DestX > SourceX THEN Vector = 90
IF Vertical = 0 AND DestX < SourceX THEN Vector = 270
IF Vertical = 0 AND Horizontal = 0 THEN Vector = 0: Target = 0: EXIT FUNCTION
END IF
DO UNTIL Vector <= 360 AND Vector > 0
IF Vector <= 0 THEN Vector = Vector + 360
IF Vector > 360 THEN Vector = Vector - 360
LOOP
Target = Vector
END FUNCTION
Don't pay attention to this signature, it's contradictory.
-
Jan 30th, 2003, 04:03 PM
#5
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:
Public Sub Bearing(X1, Y1, X2, Y2, Bearing)
'Input X1, Y1, X2, Y2
'Output Bearing (0-360)
'Calculates Bearing from X1,Y1 to X2,Y2 using 360 degree-system
Const PI = 3.141592654
Dim dx As Single
Dim dy As Single
dx = X2 - X1
dy = Y2 - Y1
If dy <> 0 Then
bearing = (Atn(dx / dy) * 180 / PI)
If X2 > X1 Then
If Y2 > Y1 Then
bearing = bearing
Else
bearing = 180 +bearing
End If
Else
If Y2 > Y1 Then
bearing = 360 + bearing
Else
bearing = 180 + bearing
End If
End If
Else
If X1 > X2 Then
bearing = 270
Else
bearing = 90
End If
End If
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!
-
Jan 30th, 2003, 08:39 PM
#6
Fanatic Member
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.
-
Jan 31st, 2003, 04:04 AM
#7
You are using
VB Code:
Vector = (Arcsin(Vertical / Diagonal))
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!
-
Jan 31st, 2003, 12:38 PM
#8
Fanatic Member
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.
-
Jan 31st, 2003, 04:47 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|