Sign in to follow this  
CyanPrime

figureing out the x,y, and z rotation of a right triangle?

Recommended Posts

Form 2 vectors from one vertex to each of the other two. The dot product of those vectors is proportional to the cosine of the angle between them.

Repeat for each of the other 2 vertices.

You can also google for "triangle angles from vertices" and get over 600,000 hits to examine.

Share this post


Link to post
Share on other sites
My bad. It's 2 points, not a triangle >_<
Alright, so I got two vectors that each contain a x y and z. We'll call them a and b.

I create a new point in the middle of the two points.

Vector3 middle = new Vector3(//order x,y,z
(max.getX() - min.getX())/2,
(max.getY() - min.getY())/2,
(max.getZ() - min.getZ())/2
);

To get the y angle I use the formula Math.toDegrees(Math.atan2(middle.getX(), middle.getZ()))

Share this post


Link to post
Share on other sites
Quote:
Original post by CyanPrime
My bad. It's 2 points, not a triangle >_<
Alright, so I got two vectors that each contain a x y and z. We'll call them a and b.

I create a new point in the middle of the two points.

Vector3 middle = new Vector3(//order x,y,z
(max.getX() - min.getX())/2,
(max.getY() - min.getY())/2,
(max.getZ() - min.getZ())/2
);

To get the y angle I use the formula Math.toDegrees(Math.atan2(middle.getX(), middle.getZ()))

What do you mean by "y angle"?
We generally want "the angle between two vectors". If you want the angle between the vector that has the point A as its origin and the point B as its end, and the y axis, just do:

Vector3 A(...);
Vector3 B(...);
Vector3 v( substract( B, A ) );
v.normalize();
float cosine = dot_product( v, Vector3( 0, 1, 0 ) );
float angle = std::acos( cosine );


This is easy to grasp once you know what the dot product means, and what the component of a vector along an axis means(for example v.y).

P.S: the angle is not signed. It's a value between 0(radian) and pi(radians).
dot_product( v, v ) = squared_length( v ).

[edit]
Writing everything on paper, it's easy to grasp the formula for this special case.
The angle will be equal to:

float angle = acos( y / std::sqrt( dot_product( v, v ) );


If you want a signed angle, you can use trigonometry:
you have a right triangle, its hypothenuse being formed by v, and one of the adjacent side being formed by the projection of v into the y axis.
The tangent of the angle you're looking for is:

Vector3 A(...);
Vector3 B(...);
Vector3 v( substract( B, A ) );
float xz_projection = std::sqrt( std::( pow( v.x, 2 ) + pow( v.z, 2 ) ) );
float y_projection = v.y;
float tangent = xz_projection / y_projection;


So the angle is equal to:

float angle = std:atan2( xz_projection / y_projection );


The angle will be a value between -pi/2 and pi/2 radians.

P.S: it's always nice when an OP acknowledges a provided solution.

[Edited by - johnstanp on December 7, 2010 8:52:45 AM]

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