Transforming vectors between relative coordinate systems

I’m doing a bunch of dynamic mesh generations at runtime and often this geometry is relative to some other geometry, for example, I draw a quad at some position/angle in the world and then draw some additional geometry relative to the quad.

What I’m doing right now is that I find the plane of the reference geometry, translate and rotate it so that its origin is at zero, then generate my relative vertices and reverse that translation and rotation to place the vertices in the world coordinate system. I’m doing this with a bunch of manual calcs of normals, cross products and vector rotations around several axis.

This works, but feels unnecessarily cumbersome.I know nodes have local and global coordinate systems, but this is all one mesh being constructed, so i just have sets of vertices that I need to transform back and forth. I thought Transform3D might be the thing, but while I think I know how to construct the Basis from the relative coordinate system I derive, I cannot figure out how to use a transform to transform/rotate vertices, they seem to just be things that you set on Node3D.

Any pointers, not even godot specific on resources for learning how to do this type of 3D math would be appreciated.

You could parent your mesh to the Node that you want the mesh to be relative to, create the mesh and then once you want to put it back to the world coordinate system - reparent it to the current scene’s root node. There is a keep_global_transform argument that you can make use of in the reparent() method.

Turns out that Transform3D does exactly what I need. My scenario just wasn’t one covered in any of the math docs. Rather than there being a method (as I had expected), all transforms are done via operators, which auto-complete unfortunately did not help me discover.

In particular the *(right: Transform3D) docs finally enlightened me:

This is the operation performed between parent and child Node3Ds.

Extrapolating from that *(right: Vector3) is what I need, even it’s less descriptively documented as:

Transforms (multiplies) the Vector3 by this transformation matrix

// Given triangle p0, p1, p2
var y = (p3 - origin).Cross(p2 - p0);
var x = (p2 - origin);
var z = x.Cross(y);
var localBasis = new Basis(x.Normalized(), y.Normalized(), z.Normalized());
var localTransform = new Transform3D(localBasis, p0);

// transform triangle to be located at 0,0,0 with first edge along X
var g0 = p0 * localTransform;
var g1 = p1 * localTransform;
var g2 = p2 * localTransform;

// transform any point px relative to the project triangle back to the original
// coordinate space:
var px_local = localTransform * px;
1 Like