Qbasicnews.com
Translating Angle into Xf and Yf? - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: QBasic (http://qbasicnews.com/newforum/forum-4.html)
+--- Forum: QB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-11.html)
+--- Thread: Translating Angle into Xf and Yf? (/thread-5571.html)



Translating Angle into Xf and Yf? - Z!re - 01-06-2005

Let's say I have an angle, and want to translate that into what I should add to a particles x and y

The way I used to do is:
Code:
ptcl.x = ptcl.x + xf
ptcl.y = ptcl.y + yf
Which works fine..
BUT
I'm making a particle engine, and i want to be able to specify by angle, instead of xf, yf:
Code:
angle = 167
ptcl.x = ptcl.x + Formula(angle)
ptcl.y = ptcl.y + Formula(angle)

Like:
0º, would become: xf = 0, yf = -2
45º, would become: xf = 1, yf = -1
90º, would become: xf = 2, yf = 0
270º, would become: xf = -2, yf = 0

etc, you get it (angles, and resulting xf, yf are just examples, doesent matter where 0º is)


Anyone know how to translate angle->(xf, yf).. and (xf, yf)->angle..?


Translating Angle into Xf and Yf? - Sterling Christensen - 01-06-2005

Code:
xf = magnitude * COS(angle)
yf = magnitude * SIN(angle)
Magnitude is how far along that angle it moved, angle is in radians.


Translating Angle into Xf and Yf? - l0wrd - 01-07-2005

Sterling is right. Those are the fomula, let me break it down for you.

In the two formula below R is the distance of the point from the origin (called radius, just like for a circle) and T is the angle (called theta) counter-clockwise from 0 on the x-axis.

Code:
X = r * Sin(T)
Y = r * Cos(T)

--
l0wrd, probably stalking you.


Translating Angle into Xf and Yf? - relsoft - 01-07-2005

Tot get the angle use:

Code:
a! = atan2(yd,xd)

Assuming you use FB.


Translating Angle into Xf and Yf? - Ralph - 01-30-2005

Finally, since BASIC, QuickBASIC and VB trig uses angles in radians, use somthing like this:
Code:
d2r = 3.141593/180 'where d2r means "degrees to radians multiplier"
.......
A = 90 'angle A in degrees
SineOfA = SIN(A*d2r)
By using the multiplier d2r and similar, you save a lot of writing and avoid a few errors.

You can also define a function, such as:
Code:
DEF FNsinD (A) = sin(A * 3.141593/189)
A = 90 'angle expressed in degrees
PRINT FNsinD(A) 'this will print the sine of 90 degrees, which = 1.



Translating Angle into Xf and Yf? - Z!re - 01-30-2005

Already knew that, but thanks anyways Big Grin