- Home /
How to rotate Vector3 to EulerAngle?
I have a plane, defined by three points. For example:
O = Vector3.zero
A = Vector3(3,15,5)
B = Vector3(2,4,10)
I need to find fourth point var D : Vector3, knowing that point lies in the same plane, distance from A is 10 units and angle between my new point D and A from B (angle DBA) is 40 Euler degrees (in the direction of O).
Answer by robertbu · Feb 20, 2013 at 09:18 PM
Not really a Unity question, but here is an outline of one method:
From the triangle ABD, you have two sides and an angle, so you can get angle BAD.
Use A as an origin, create a vector AB normalized * 10
To get an axis, use A as an origin, and create an axis by AB cross AO
Use Angle Axis rotation with the calculated axis and calculated angle
Add back A to get point
I know all angles in my triangle, so I didn't need to take step 1. So I:
var vectorAD = vectorAB;
var rotationAxis = Vector3.Cross(vectorAB, vectorAO);
// $$anonymous$$agic goes here
VectorAD.ToAngleAxis(40, rotationAxis);
var rayAD = new Ray (A, VectorAD);
var D = rayAD.GetPoint(10);
Something like that? But of course, ToAngleAxis is not a member of UnityEngine.Vector3, so what should I do With VectorAD?
Use a Quaternion for an angle/axis rotation. Here is what I was thinking sketched in code (totally untested and assumes you know the angle):
var v3PointA : Vector3 = Vector3(3,15,5);
var v3PointB : Vector3 = Vector3(2,4,10);
var v3PointO : Vector3 = Vector3(0,0,0);
// Need the normal to the plane. Since
// v3PointO is at the origin, we can do
var v3Axis = Vector3.Cross(v3PointA, v3PointB);
var q = Quaternion.AngleAxis(angle, v3Axis);
var v3VectorAD = (v3PointB - v3PointA).normalized * 10.0;
v3VectorAD = q * V3VectorAD;
var v3PointD = v3VectorAD + v3PointA;
Thank you! Just tested, it works. Summarize:
you can just multiply Quaternion by Vector3. So, to rotate Vector3 you need to multiply Quaternion with it.
To get Vector, perpendicular to plane with two Vectors, you need to multiply (Vector3.Cross) these two.
Note: I believe in a Quaternion/vector multiplication the Quaternion must come first. I'm glad it worked. Sometimes stuff my head without testing takes a bit of fussing.
Your answer
Follow this Question
Related Questions
Use Unity's transform API to manipulate points? 0 Answers
How do you perform multiple rotations on the same object? - (in a single frame) 0 Answers
Rotating a direction Vector3 by Quaternion 2 Answers
How to achieve a rotation on "batch quads" ? 0 Answers
Rotate Vector3 array around a point 1 Answer