Jump to content
  • Advertisement
Sign in to follow this  
Dryak

Angle Between Two 3D Points

This topic is 3347 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

Change the point 0,0,0 to 0,0,5 for example, and the function where you divide by the lengths should do the trick. What you need for this is basically one point that the person is currently facing, and a point that he should be facing, and that will give you the angle between them. You are really using vectors though, but it's the same thing if the person is standing at 0,0,0. Otherwise you would take the angle between facingPoint-position and newFacingPoint-position.
If you have the current facing as an angle and not a point or vector, then you can instead use the atan2 function on the vector to the new facing-point, to calculate the angle which is facing that one, and subtract the old angle from that to know how much to turn.

Share this post


Link to post
Share on other sites
Advertisement
Ok I see what your saying.

So how would I code the angle between two vectors?

Like a vector pointing from 0,0,0 towards 0,5,0 with the magnitude of 5 and a vector point from 0,0,0 toward 5,5,5 with the magnitude of 15.

Share this post


Link to post
Share on other sites
The math you're doing looks correct to me. You're essentially using the law of cosines (A . B = |A||B|cos(angle)) to find the angle between the two vectors. I'm sort of a beginner with c++ so I can't really help you there. Although you should check to see if a value of (0, 0, 0) is passed in or else you'll be dividing by zero.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dryak
... and a vector point from 0,0,0 toward 5,5,5 with the magnitude of 15.

I don't know how you reached that conclusion, but √75 != 15.

Share this post


Link to post
Share on other sites
As already stated in the above answers, in the formula
a . b == |a| * |b| * cos( <a,b> )
both a and b denote direction vectors but not position vectors.

A direction in the above sense can be computed as the difference between two points, e.g. as difference from the position of an observer to its point of interest. Or a direction is given by its own, e.g. the view direction of a person.

As can be seen from the formula above, as soon as any of the both direction vectors has a length of 0, then the dot-product results in 0, making the cosine of the angle meaningless. Hence, both vectors need to have a non-zero length for the OPs purpose. Further, the cosine will be weighted by the both lengthes, so the lenghtes have to be normalized for the OPs purpose (was also already mentioned).

Furthermore, the formula works for all dimensions and computes (after re-arrangement) the angle betwen the both vectors on the (hyper-)plane where both vectors are lying in. So, if you want to know the angle not between the vectors itself but e.g. between them how they would appear on the horizon plane (i.e. the "horizontal angle"), then you have to project the vectors onto that plane first.


Example 1: Angle directly between
a := (0,5,0) - (0,0,0) = (0,5,0)
and
b := (5,5,5) - (0,0,0) = (5,5,5)

Since
|a| = sqrt(0 + 5*5 + 0) = 5 != 0
and
|b| = sqrt(5*5 + 5*5 + 5*5) = sqrt(75) != 0
both vectors have a length different from 0, so that the computation
( a . b ) / ( |a| * |b| ) = cos( <a,b> )
can be done (w/o division by zero). So
<a,b> = acos( ( 0*5 + 5*5 + 0*5 ) / ( 5 * sqrt(75) ) = acos( 5 / sqrt(75) ) = 0.955 rad or 54,7 degree

Example 2: The viewing direction is
a := (0,0,1)
just with a y==0, and the point of interest is in direction
b' := (5,5,5) - (0,0,0) = (5,5,5)
but only the horizontal angle is of interest (e.g. the observer should not change the pitch of the head). Hence the projection onto the x,z-plane
b := b' * ex + b' * ez = (5,0,5)

Since
|a| = 1 != 0
and
|b| = sqrt(50) != 0
both vectors have a length different from 0, so that a computation like shown above can be done:
<a,b> = acos( 5 / sqrt(50) ) = 0.785 rad or 45 degree

In fact, the above 3D problem is reduced to a 2D problem happening onto the said x,z-plane. Because this, the same result would come out for
a2D := (0,1)
and
b2D := (5,5)


Hopefully this recap has shed some light onto the problem.

Share this post


Link to post
Share on other sites
That did help but I think my main problem is that I don't quite understand vectors.

I know they have direction and magnitude (length?) but I'm not sure how it show which direction it has or what vector denotation is like.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!