Public Group

# 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.

## 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 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 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 on other sites
Quote:
 Original post by FeadurActually 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?

##### 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 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 on other sites
Quote:
 Original post by FeadurThanks 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 on other sites
Thanks jyk, i'm using that function now ;)

1. 1
Rutin
24
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 46
• 41
• 23
• 13
• ### Forum Statistics

• Total Topics
631746
• Total Posts
3002021
×