how to change the matrix multiplication order for scale, translate, and rotate

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By johnandrusek

Hi this is a bit of a contrived question but I have a need to demonstrate what happens when you change the order of the matrix multiplication for translations, scaling and rotation.
More specifically, is there a way to do the rotations and translations with just a matrix. (ie
create a transform matrix that is the result of a matrix multiplication of the translate matix and the rotate matrix). In this wy I can force the order.

Thanks Regards, John

:bust_in_silhouette: Reply From: Zylann

You can use the Transform type and explicitely apply a decomposed transformation to it in the order you want.

var trans = spatial_node.transform

# Order these as you wish
trans = trans.rotated(axis, angle)
trans = trans.scaled(scale)
trans = trans.translated(offset)

spatial_node.transform = trans

However there is no setting in Godot to force ALL transforms to work by default in a particular order.

Hi Zylann,
thank you for your answer it. I think I am messing something up though. I tried the following:

public override void _Process(float delta)
{
        Vector3 deltaT = this.applyTransaltion(delta);
         Transform t = this.GetGlobalTransform();
         t = t.Translated(deltaT * delta);
         t = t.Rotated(new Vector3(1.0f, 0.0f,0.0f), Mathf.Deg2Rad(this.xRotation * delta));
         
       this.SetTransform(t);

}

where deltaT is a Vector3 that has the delta’s for each of x y and z. I would expect that if I change the order of the rotate and transform it should result in different behaviour.

public override void _Process(float delta)
{
        Vector3 deltaT = this.applyTransaltion(delta);
         Transform t = this.GetGlobalTransform();
         t = t.Rotated(new Vector3(1.0f, 0.0f,0.0f), Mathf.Deg2Rad(this.xRotation * delta));
         t = t.Translated(deltaT * delta);
         
       this.SetTransform(t);

}

(ie one should result in the object kind of orbiting around the original origin, the other should result in the more desirable behaviour of an object rotating and moving in the right direction). So I must be doing something wrong.

The object I am doing this on is an instance of navigation which has a child of NavigationMeshInstance which has a child of MeshInstance.

Any ideas what I am doing wrong?

johnandrusek | 2019-09-20 13:22

Wether you use world space or local space, you should get and set the transform in the same space. The GlobalTransform property is world space, theTransform property is local space. Here I see you get the GlobalTransform property, but you set the Transform. If the parent of your navigation node doesn’t have an identity transform, this code will behave inconsistently.

Another case where your both codes will do the same result, is if your translation is along the X axis. In that case, the order has no effect since you rotate around X and translate along X, one doesn’t influence the outcome of the other. Depends what your applyTransaltion does.

Zylann | 2019-09-20 17:35

Hi Zylann,
opps the global transform dealy was a typo (I was trying on both global and local). Sorry about that. The code looks like this:

public override void _Process(float delta)
{
Vector3 deltaT = this.applyTransaltion(delta);
Transform t = this.GetTransform();
t = t.Translated(deltaT * delta);
t = t.Rotated(new Vector3(1.0f, 0.0f,0.0f), Mathf.Deg2Rad(this.xRotation * delta));

   this.SetTransform(t);

}

so for sure the get and set should be in the same either global or local.
You point out something that might be the cause of my pain. The point about the identity transform. Would you achieve that by doing something like this. So something like:

var identityBasis = Basis.Identity;

Transform t = this.GetTransform();
t.basis = identityBasis;
t = t.Translated(deltaT * delta);
t = t.Rotated(new Vector3(1.0f, 0.0f,0.0f), Mathf.Deg2Rad(this.xRotation * delta));

this.SetTransform(t);

Does that make sense

Again thank you so much for you responses really appreciate it

johnandrusek | 2019-09-20 18:22