# acos and the dotproduct - heeeelp!! :)

This topic is 4841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

okiedokie, i have 2 gamepieces on my screen, one at A(10,10) and one at B(233, 400), using (0,0) as the top left corner as the origin. The problem is to find the angle between em so the one at (10,10) can turn to face the other one (i.e. change its heading) Ax * Bx + Ay * By = the dot product from the dot product, you can get the angle, by getting the inverse cosine of it, so in c++ it would look like this : float AngleInRadians = acos(dotproduct); but it dosent work.... anyone here finish high school? :) -Jason

##### Share on other sites
take the relative distance vector: position1 - position2. dont forget to normalize it.

now you want the angle of this vector with what? the x-axis of your coordinate system i assume, which should be (1,0).

so the outcome of your dotproduct simply is the x-component of this vector.

take the acos of this, and youve got your anwer.

however, atan2() will probably be more conveniant in this case.

##### Share on other sites
So with the above numbers :

1) take the relative distance vector: position1 - position2
pos1 - pos 2 =
Ax - Bx = -223
Ay - By = -390
(-223, -390)
this is the magnitude of a vector i think

2) dont forget to normalize it.
normalize, meaning find the (x, y) that would make this magnitude == 1

length = sqrt(223^2 + 390^2)
= sqrt(49729 + 152100)
= 449.253

float normalizedX = -223 / 449.253
float normalizedY = -390 / 449.253

"now you want the angle of this vector with what? the x-axis of your coordinate system i assume, which should be (1,0)." - i think....

"so the outcome of your dotproduct simply is the x-component of this vector." - lost

"take the acos of this, and youve got your anwer." - So acos(normalizedX) would give the angle??

"however, atan2() will probably be more conveniant in this case." - What do the (atan, acos, asin)2 functions do differently?

not as lost now, just a deeper kind of lost :)

-Jason

##### Share on other sites
further investigation reveals :

normalizedX = -.49637

acos(normalizedX) = 2.0902

which is 119.759 degrees, if the above was radians

which makes me think angles are measured clockwise, with zero at the 12oclock position.

Am i right or way the hell lost?

##### Share on other sites
[snip]

Edit: I misunderstood what you are trying to do. Eelco is correct. Just take the difference between the two points and use atan2 to find the angle. You don't have to normalize in this case:
x = 233 - 10;y = 400 - 10;angle = atan2( y, x );

The result is in radians. The X axis has an angle of 0. A positive angle is a rotation in the same direction as rotating from the X axis to the Y axis.

##### Share on other sites
Quote:
 What do the (atan, acos, asin)2 functions do differently?

There is only atan2, and the advantage of it is that it returns a valid result even if the denominator is 0 (no divide-by-zero crashes). Basically, this returns the angle of a bi-dimensional vector and this is why there is no acos2 or asin2 - their parameters would not represent a vector.

(Edited)
Kyle

##### Share on other sites
" The X axis has an angle of 0. A positive angle is a rotation in the same direction as rotating from the X axis to the Y axis."

----------------90 deg here +y ------------
180 deg here-x-------- ---------+x0deg here
---------------270deg here -y ------------

like that?

What if I need zero degreez to be straight up, and degrees increases as you go counterclockwise?

##### Share on other sites
Turning things to face other things was discussed many times.
Recend discussion
direction vector it's normalized difference of positions, btw.
also you can do it w/o trig.

##### Share on other sites

This topic is 4841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628710
• Total Posts
2984334

• 23
• 11
• 10
• 13
• 14