Sign in to follow this  
Feadur

Direction angle

Recommended Posts

Sorry for such a newbie question, but, given a StartPosition (Vector3) and a EndPosition (Vector3), I have a MoveVector (Vector3). How do I find the angle that my object is facing?. i.e.: Start (10,0,10) End (1,0,5) Then... Move (9,0,5) Which angle is Move creating??

Share this post


Link to post
Share on other sites
An angle relative to what? If you mean the X,Y,Z axises (?) then you just use trig. If you mean a plane, find the angle between your vector and the plane normal; to do that, calculate the dot product (using quaternions) and solve for it
A*B = ||A||*||B||cos(angle between A and B)

Share this post


Link to post
Share on other sites
Actually it's easier than that. The object is moving always along the (0,1,0) plane,so i need to rotate the object so that it's facing to a point in the same plane.

Share this post


Link to post
Share on other sites
Quote:
Original post by Feadur
Actually it's easier than that. The object is moving always along the (0,1,0) plane,so i need to rotate the object so that it's facing to a point in the same plane.
(0,1,0) is not a plane. Describing a plane mathematically generally requires four values. Three values would indicate you're describing a vector. Do you mean that the object is only varying by the y coordinate?
That still doesn't answer which plane you want to find the angle with, so I'm afriad I can't help you (yet).

Share this post


Link to post
Share on other sites
I think he mean the plane normal, meaning, it would be the XZ plane.

If that is the case, you can use something like atan(Z/X) to find the counter-clockwise angle from the x axis (looking from the top). Be careful of when the X is equal to zero and when you are outside the (-90:90) degree range!

Share this post


Link to post
Share on other sites
Thanks a lot Koobazaur, I have this working now :)


public void LookAt(Vector3 point)
{
Vector3 move = point - this.position;

this.rotation.Y = (float)Math.Atan(move.X / move.Z);

if (move.Z < 0) this.rotation.Y += (float)Math.PI;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Feadur
Thanks a lot Koobazaur, I have this working now :)


public void LookAt(Vector3 point)
{
Vector3 move = point - this.position;

this.rotation.Y = (float)Math.Atan(move.X / move.Z);

if (move.Z < 0) this.rotation.Y += (float)Math.PI;
}
I suggest you make the following changes to the above code excerpt:
public void  LookAt(Vector3 point)
{
Vector3 move = point - this.position;

this.rotation.Y = (float)Math.Atan2(move.X, move.Z);

if (move.Z < 0) this.rotation.Y += (float)Math.PI;
}
This will solve the 'divide by zero' problem mentioned earlier, and also eliminate the need to compute the quadrant manually.

This is of course assuming you have an 'atan2' function available.

Share this post


Link to post
Share on other sites

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