Rotate Vector3 Manually?
Hi all, i need to find a way to reproduce unity3 rotation on a simply server that dosn't have fancy unity3 functionality.
So far i have come up with:
public static Vector3 Rotate(Vector3 x, float _x, float _y, float _z) { float angle = _z; x = new Vector3( x.x * Mathf.Cos(Mathf.Deg2Rad * angle) - x.y * Mathf.Sin(Mathf.Deg2Rad * angle), x.x * Mathf.Sin(Mathf.Deg2Rad * angle) + x.y * Mathf.Cos(Mathf.Deg2Rad * angle), x.z); angle = _y; x = new Vector3( x.x * Mathf.Cos(Mathf.Deg2Rad * angle) + x.z * Mathf.Sin(Mathf.Deg2Rad * angle), x.y, -x.x * Mathf.Sin(Mathf.Deg2Rad * angle) + x.z * Mathf.Cos(Mathf.Deg2Rad * angle)); angle = _x; x = new Vector3( x.x, x.y * Mathf.Cos(Mathf.Deg2Rad * angle) - x.z * Mathf.Sin(Mathf.Deg2Rad * angle), x.y * Mathf.Sin(Mathf.Deg2Rad * angle) + x.z * Mathf.Cos(Mathf.Deg2Rad * angle)); return x; }
However i can't reproduce rotation angles that i input from transform.rotation.eulerAngles, on some angles it works on some not, any idea why?
I try to reproduce unity3d rotation while i only have access to basic functions like sin and cos. Basically i can't use libraries here.
If you're working on some project that is so lame that you only have "access to sin and cos" .. forget the payment and move on to another job.
Answer by Bunny83 · Nov 30, 2015 at 05:52 PM
This one will exactly replicate the rotation that a Transform experiences with the given worldspace eulerangles:
public static Vector3 Rotate(Vector3 aVec, Vector3 aAngles)
{
aAngles *= Mathf.Deg2Rad;
float sx = Mathf.Sin(aAngles.x);
float cx = Mathf.Cos(aAngles.x);
float sy = Mathf.Sin(aAngles.y);
float cy = Mathf.Cos(aAngles.y);
float sz = Mathf.Sin(aAngles.z);
float cz = Mathf.Cos(aAngles.z);
aVec = new Vector3(aVec.x * cz - aVec.y * sz, aVec.x * sz + aVec.y * cz, aVec.z );
aVec = new Vector3(aVec.x , aVec.y * cx - aVec.z * sx, aVec.y * sx + aVec.z * cx );
aVec = new Vector3(aVec.x * cy + aVec.z * sy, aVec.y , -aVec.x * sy + aVec.z * cy);
return aVec;
}
So those two lines will result in the same vector:
Vector3 input; // some vector
Vector3 v1 = transform.TranslateDirection(input);
Vector3 v2 = Rotate(input, transform.eulerAngles);
As others have said the order in which you apply the rotations matters. Unity uses the local order Y - X - Z. The worldspace order is always the reverse, so Z - X - Y.
Answer by DavidBlanpied · Nov 30, 2015 at 03:31 PM
What you're trying to do is like rotating the bits in a bitmap on the surface of a cube in 3-space. It works if the cube's surface is aligned with the global X-Y plane, but not generally. General rotations require quaternions, to be generally useful your Rotate() function needs to use quaternion multiplication.
Try this experiment: rotate the vector (0,0,1) by (90°,45°,45°). 1st, rotate 45° about the Z axis. In Yaw/Pitch/Roll terms, a roll. Still at (0,0,1). 2nd, rotate 90° about the X-axis, a pitch. Now we're at (0,1,0). Last, rotate 45° about the Y axis, another roll. Still at (0,1,0). This might not be the anticipated result. So the question is what does a rotation by (90°,45°,45°) mean? Rotate 45° about an axis tilted (90°,45°,0)? By Yaw = 90°,Pitch = 45°, Roll = 45°? Rotate 1st about X then Y then Z? They all give different answers.
The transformation first Z then X then Y is the transform.rotation.eulerAngles() transform using degrees and is equivalent to
Quaternion.Euler(0, 0, z) * Quaternion.Euler(x, 0, 0) * Quaternion.Euler(0, y, 0)
so to duplicate transform.rotation.eulerAngles(), the Rotate() function in psudocode might look like
return x * Quaternion.Euler(0, 0, _z) * Quaternion.Euler(_x, 0, 0) * Quaternion.Euler(0, _y, 0);
Other transforms are useful, for a camera azimuth/altitude transformation try
transform.rotation = Quaternion.Euler(0, Az, 0) * Quaternion.Euler(-Alt, 0, 0);
or Yaw/Pitch/Roll:
transform.rotation = Quaternion.Euler(0, Yaw, 0) * Quaternion.Euler(Pitch, 0, 0) * Quaternion.Euler(0, 0, Roll);
Answer by Fattie · Nov 30, 2015 at 07:14 PM
The others have given you the full answer, but you will have to dramatically improve your understanding of quaternions before you can work in this realm.
Your answer

Follow this Question
Related Questions
[C# Scripting] Set the rotation of the player, when another script access it at the same time. 0 Answers
How to rotate parent only without affecting children(in editor) 2 Answers
Instantiated Object Has Wrong Rotation 0 Answers
Rotation and Gravity Relative to the Center of an Object 0 Answers
With VR character's rotation is applied twice or double 1 Answer