Hello guys, I need some help with a math/logic problem.
I have a plane defined by its center (world coordinates), forward and up direction, and a point in a random location on that plane. I’d like to calculate the ‘local’ X and Y coordinates of that point relative to the plane center.
This is something that will run multiple times per frame so I cannot do something like instantiate a gameobject plane and a gameobject cube in theyr respective positions, rotate the gameobject plane to match the imaginary plane direction, make the cube a child of the plane and extract the X and Y values, and destroy these 2 gameobjects.
You could have a single reference GameObject (no need to create and destroy new ones, nor to create child objects), and move that Transform around to match your plane, and use .InverseTransformPoint. Something like:
This is just plain linear algebra. So given a 3d point that lies inside our plane, we can simply subtract the plane origin from that point to get a relative vector from our origin to that point in the plane. Now you just have to calculate the dot product between each of your basis vectors and your relative direction vector to get the coordinates within that plane. Of course the basis vectors should to be normalized but it’s actually not required since they actually form a 2d coordinate space on their own.
So here’s an example:
// given information
Vector3 origin;
Vector3 planeXDir;
Vector3 planeYDir;
Vector3 pointOnPlane;
Vector3 v = pointOnPlane - origin;
Vector2 positionInPlane = new Vector2(Vector3.Dot(planeXDir, v), Vector3.Dot(planeYDir, v));
That’s exactly what a 2x3 projection matrix would do. I would highly recommend to watch the Essence of linear algebra series by 3b1b. If you are not familiar with linear algebra I would recommend to start the series at the beginning. However the important part relevant here is chapter 9 about the dot product and duality.