- Home /
Matrix rotation of a Vector3 around three planes of rotation
Hi all,
I have a Vector3 instance (v3_orig) that represents a point where a line segment has passed through a plane. In the application, the plane can have arbitrary rotation values (0 < x < 180, 0 < y <180, 0 < z < 180). After determining the point where the line segment crosses the plane, I want to rotate the point from its initial intersection position to a new position so that the point would be at the same position on the plane if the plane orientation had been normalized (Quaternion.identity). I do not want to use Transforms, as the Vector3 is part of a larger mesh object, and I simply want to rotate the vertex position.
I have solutions that work if the initial plane is only rotated in two of three axes (XY, YZ, or ZX) but I can't get it to work if all three axes have a value != 0.
Here is the original code (c#):
Vector3 v3_orig = Vector3(4,3,5) // some point that intersects with plane
Quaternion q = Quaternion.identity;
q.eulerAngles = new Vector3(-20, -20, 40); // Corrected angles to virtually rotate plane
Matrix4x4 m = Matrix4x4.TRS(Vector3.zero, q , Vector3.one);
Vector3 v3_final = m.MultiplyPoint3x4(v3_orig);
I have tried several variants on this theme and will post if anyone thinks they can give me a hand with this problem. Would also be happy to post a few images that help explain the problem if my explanation is not adequate.
Thank you.
I +1'ed it again for providing clear description text and for formatting code properly. I do not think this question deserves to be downvoted.
Hi, I'm new to this forum and certainly didn't want to break with the etiquette of the space. Just so I'm clear, when you ask me to provide cross-links are you referring to other related posts within unityAnswers or more of my own material? In what was likely also a bad move on my part, I threw this question up in UnityAnswers and on the Forum as I was pretty desperate to get an answer today (deadline loo$$anonymous$$g).
This is not Unity forum-specific etiquette. Imagine somebody reads a question of yours in one place, and answers it, despite it being answered somewhere else. If you're going to clutter up the internet with duplication, please try to be make others aware of the mess. Please value the time of others.
Answer by MCorrin · Sep 08, 2012 at 10:32 PM
Brian Stone posted this answer on the forum and it addresses my problem perfectly.
http://forum.unity3d.com/threads/150444-Matrix-rotation-of-a-Vector3-around-three-planes-of-rotation
Here is what he wrote:
If you have a matrix or quaternion which represents the orientation of the plane, then all you need to do is calculate the Inverse of that transform and multiply it to the point. This will transform the point to the World's identity orientation (X:<1,0,0>, Y<0,1,0>, Z<0,0,1>), because any matrix multiplied by its inverse is the identity matrix*.
Matrix4x4 M; // set this to the plane's orientation
Vector3 v3_orig; // some point that intersects with the plane
Matrix4x4 Mi = M.inverse;
Vector3 v3_final = Mi * v3_orig;
implies that the matrix is square and non-singular.
And to demo with Quaternion:
Quaternion qi = Quaternion.Inverse(plane.transform.rotation);
Vector3 v3_final = qi * v3_orig;
That's the same as the answer I posted. There is literally zero difference.
Hi Christian, Small but subtle difference was that the Quaternion.Inverse call. I couldn't get the code to compile without an error when I used '-plane.transform.rotation'. Thanks though! $$anonymous$$ichael I hadn't seen your other correction below. Sorry. I appreciate it though!
No worries. I've deleted my answer again, as I see no reason to have 2 identical answers to the same question.