3
\$\begingroup\$

I have a model instance of a character, and a model instance of a sword weapon. THe character has a node on his right hand so he can "carry" the sword. Right now I use this code so the sword's transform follows that of the node.

weaponModelInstance.transform.set(modelInstance.transform).mul(weaponAttachmentNode.globalTransform);
weaponModelInstance.transform.rotate(Vector3.Z, -90); // adjustment for blender coordinates
world.modelBatch.render(weaponModelInstance, world.environment);

this works, except that the model instance gets scaled to the scale of the node, which i do not want, i want the weapon to keep its scale. I've tried doing the following code to fix this but I end up getting weird (and wrong) rotations for the sword

weaponModelInstance.transform.set(modelInstance.transform).mul(weaponAttachmentNode.globalTransform);
weaponModelInstance.transform.set(
        weaponModelInstance.transform.getTranslation(new Vector3()),
        weaponModelInstance.transform.getRotation(new Quaternion()),
        new Vector3(1, 1, 1)
);
weaponModelInstance.transform.rotate(Vector3.Z, -90);
world.modelBatch.render(weaponModelInstance, world.environment);

do anyone know how to properly copy the world translation and rotation from the node, but not its scale?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

You will need to normalize the rotation matrix to remove its scale.

This can be done by normalizing all 3 rows of the transform basis matrix.

new btMatrix3x3(
  transform.getBasis().getRow(0).normalize()
, transform.getBasis().getRow(1).normalize()
, transform.getBasis().getRow(2).normalize()
);

or

new btMatrix3x3(
  transform.getBasis()[0].normalize()
, transform.getBasis()[1].normalize()
, transform.getBasis()[2].normalize()
);

This new matrix contains only the rotation without scaling.

If you want to extract the scaling instead:

new btVector3(
  transform.getBasis()[0].length()
, transform.getBasis()[1].length()
, transform.getBasis()[2].length()
);
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Alternatively, you could use libgdx's Matrix4#getRotation(Quaternion, boolean), the boolean argument being a "normalizeRotation" flag that, when set to true, tells libgdx to perform this operation for you. \$\endgroup\$
    – Chris Bode
    May 2, 2015 at 21:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .