Hello Community,
I am having some trouble with transforms and hope someone can help me here. The issue I am facing is transforming GameObjects from one plane of reference into another. My planes are described by three Vector3 in world space: Origin, X-Axis and Y-Axis and I want to calculate a transform Matrix to get from one to the other.
I have tried getting there step by step. First translating the object and then aligning first the X-Axis and then the Y-Axis:
geo.transform.position += to.Origin - from.Origin;
float angleX = (float)(Math.Acos(Vector3.Dot(from.XVector, to.XVector)) / Math.PI * 180);
geo.transform.RotateAround(to.Origin, Vector3.Cross(from.XVector, to.XVector), angleX);
if(Vector3.Distance(to.XVector.normalized, from.XVector.normalized) > 0.000001f)
{
geo.transform.RotateAround(to.Origin, Vector3.Cross(from.XVector, to.XVector), -2 * angleX);
}
float angleY = (float)(Math.Acos(Vector3.Dot(from.YVector, to.YVector)) / Math.PI * 180);
geo.transform.RotateAround(to.Origin, to.XVector, angleY);
if (Vector3.Distance(to.YVector.normalized, from.YVector.normalized) > 0.000001f)
{
geo.transform.RotateAround(to.Origin, to.XVector, -2 * angleY);
}
This approach is missing some edge cases though it seems. Most of the time it Rotates the Objects right on top of each other, but sometimes, they are wildly sticking out in some random direction. Can someone shed a light on what I am missing? Or even better tell me how to get there in one step, calculating the rotation as one transform?
Cheers in advance,
Philipp
Im not completely sure I understand what it is you’re trying to do, but a simple way i know of to transform things from one frame of reference to another is to use a GameObject that represents the frame of reference (with the correct position, rotation) and use transform.TransformPoint(thePointToConvert).
Thanks for the quick reply @PraetorBlue . I am looking mostly for rotation. Maybe this sketch will make clear what I am trying to do. I have the planes attached to my GameObjects and want to transform them using the planes as a reference.
Something like this should work:
How did you end up with this setup? It would be a lot easier to work with if you just made a GameObject to represent the plane and gave its transform the appropriate position/rotation. Then you could just do something like this:
Transform A;
Transform B;
B.rotation = A.rotation;
B.position = A.position;
The issue is, that I have a lot of these planes on my GameObject, for different possible Connections. I could maybe have all of these as children of my Object and when I need one to do the transformation, I make it the parent of my Object. That could well work. I originally didn’t make each plane a GameObject with its own transform, because of the overhead associated with that. I could in the end have a few million of those…
I will try it out and see how it scales though. Thanks, @PraetorBlue !
If anyone has a mathematical solution to this, that could also be super helpful, in case it doesn’t scale as far as I need it to.
For a mathematical solution maybe look into Matrix4x4. You basically do something like this:
Matrix4x4 matrix = Matrix4x4.TRS(translation, rotation, scale);
// To transform a direction vector
var transformedDirection = matrix.MultiplyVector(myVector3Direction);
// To transform a position vector
var transformedPosition = matrix.MultiplyPoint3x4(myVector3Position);
But the inputs to TRS you will have to figure out from your object and the local planes etc…
The GameObject approach will be so much easier to work with in the editor though, so I would explore that first and see if it’s feasible.