Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » getting the angle if i have the cos value

Credits go to A J, Evert, and X-G for helping out!
This thread is locked; no one can reply to it. rss feed Print
getting the angle if i have the cos value
Global Warming
Member #4,264
January 2004
avatar

hi,
so i got something like this

cos alfa = x

x is some value that is known. what do i need to do to get that alfa angle? do i have to create some lookup table in the program and compare the value or maybe there's some easier method using some formula or sth? damn my maths ;e

greets

A J
Member #3,025
December 2002
avatar

libc has some cos() acos() be careful, you may need to convert from radians to degrees.

___________________________
The more you talk, the more AJ is right. - ML

Evert
Member #794
November 2000
avatar

arccos.
Beware though: the cosine is a periodic function and each value appears twice in one period. That means that if you invert it, you may not end up with the same angle you started with. In particular, the result is always between 0 and pi (if memory serves).

Global Warming
Member #4,264
January 2004
avatar

ok so i wrote a function to return the sum of vectors. will it work? ;]

1typedef struct
2{
3 float angle;
4 float value;
5} ob_vect;
6 
7ob_vect ob_vector_sum( ob_vect vect1, ob_vect vect2 )
8{
9 ob_vect result;
10
11 float x1,x2,y1,y2,i,a,b,c;
12
13 x1 = sin( vect1.angle ) * vect1.value;
14 y1 = cos( vect1.angle ) * vect1.value;
15
16 x2 = sin( vect2.angle ) * vect2.value;
17 y2 = cos( vect2.angle ) * vect2.value;
18
19 result.value = sqrt( pow(x1+x2,2) + pow(y1+y2,2) );
20
21 a = y1+y2;
22 b = x1+x2;
23 c = sqrt( b*b + a*a );
24
25 i = b / c;
26
27 result.angle = acos( i );
28
29 return result;
30}

thanks ;]

Evert
Member #794
November 2000
avatar

No, because you should be using atan2() te get the full 2pi angle in that case.
Actually, if you're using vectors, you should probably store their carthesian components (x and y) rather than their curvilinear components (r and phi).

Global Warming
Member #4,264
January 2004
avatar

Quote:

Actually, if you're using vectors, you should probably store their carthesian components

why should i? ;] i think it actually doesn't matter if i store x,y or angle & length. i used the second method because it seems to me that it fits better to my project ;]

i modified the function so it doesn't use acos but atan. looks like it's ok now. what do you think?

1ob_vect ob_vector_sum( ob_vect vect1, ob_vect vect2 )
2{
3 ob_vect result;
4
5 float x1, x2;
6 float y1, y2;
7 float a, b, i;
8
9 x1 = sin( vect1.angle ) * vect1.value;
10 y1 = cos( vect1.angle ) * vect1.value;
11
12 x2 = sin( vect2.angle ) * vect2.value;
13 y2 = cos( vect2.angle ) * vect2.value;
14
15 result.value = sqrt( pow( x1 + x2, 2 ) + pow( y1+y2, 2 ) );
16
17 a = y1 + y2;
18 b = x1 + x2;
19
20 i = a / b;
21
22 result.angle = atan( i );
23
24 return result;
25}

X-G
Member #856
December 2000
avatar

Quote:

i think it actually doesn't matter if i store x,y or angle & length

Click me. Now that we're through with that...

Storing Cartesian components lets you add and subtract vectors in a way that doesn't involve several slow trigonometric functions that are inaccurate to boot. That's probably the main reason why it's a horribly bad idea to represent vectors using polar coordinates.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Evert
Member #794
November 2000
avatar

Quote:

what do you think?

I still think you should use atan2, because you'll need the full 2pi range.
I also think your coordinate axes are either swapped, or rotated over pi/2. It's conventional to measure angles relative to the horizontal axis (so phi=0 will give you a vector along the x axis).

And I still think you're probably better off storing cartesian components.

Global Warming
Member #4,264
January 2004
avatar

Maybe you're right... But in my project I often need to update just the angle or the lenght of the vector. So I will need to use those 'slow trig functions', maybe not in ob_sum_of_vectors() but in: ob_vector_get_angle(), ob_vector_set_angle(), ob_vector_add_angle(), ob_vector_get_value(), ob_vector_set_value(), ob_vector_add_value() etc. ;)

EDIT: Maybe I'll just use those fixed point funtions from allegro and there will be no problem with speed? ;p

1ob_vect ob_vector_sum( ob_vect vect1, ob_vect vect2 )
2{
3 ob_vect result;
4
5 fixed x1,x2,y1,y2,a,b;
6 float af,bf;
7
8 x1 = fixmul( fixsin( ftofix( vect1.angle ) ), ftofix( vect1.value ) );
9 y1 = fixmul( fixcos( ftofix( vect1.angle ) ), ftofix( vect1.value ) );
10
11 x2 = fixmul( fixsin( ftofix( vect2.angle ) ), ftofix( vect2.value ) );
12 y2 = fixmul( fixcos( ftofix( vect2.angle ) ), ftofix( vect2.value ) );
13 
14 a = y1+y2; af = fixtof(a);
15 b = x1+x2; bf = fixtof(b);
16
17 result.value = sqrt( bf*bf + af*af );
18 result.angle = fixtof( fixatan2( a, b ) );
19
20 return result;
21}

X-G
Member #856
December 2000
avatar

Floats are just as fast as fixeds on any modern CPU. And using trigonometry once or twice per tick, to get angle or magnitude, is much faster than using them hundreds of times per tick for every time you want to manipulate a vector.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Global Warming
Member #4,264
January 2004
avatar

Hmm. But if I use fixed from Allegro which use lookup tables than it won't be a problem. After all I just wanted to know if my vector sum function will work. ;]

X-G
Member #856
December 2000
avatar

Yes it will. Or are you seriously suggesting that doing 1-2 operations is as fast as doing tens or hundreds of the same operation?

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Global Warming
Member #4,264
January 2004
avatar

Yeah that's obvious that it's slower and if it'll work too slow I'll change it. But just help me correct my function because looks like it works bad and everything seems OK to me (maybe it's some other mistake in my code but please check it).

EDIT: Never mind, all works fine. ;) Thx for help.

Go to: