Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this