Jump to content
  • Advertisement
Sign in to follow this  
Feadur

Direction angle

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

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!