Skip to content

Conversation

zzuegg
Copy link
Member

@zzuegg zzuegg commented Sep 19, 2025

This PR adds the convenience method worldToLocal for Quaternions, i added a test that shows the usage when transferring a spatial to a different Node.

@codex128 codex128 self-requested a review September 19, 2025 13:47
Assert.assertEquals(worldTranslation,testNode.getWorldTranslation());

testNode.setLocalRotation(nodeB.worldToLocal(worldRotation,null));
Assert.assertEquals(worldRotation,testNode.getWorldRotation());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using isSimilar for equality checks so it doesn't fail on round-off error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point

@yaRnMcDonuts yaRnMcDonuts added this to the v3.9.0 milestone Sep 20, 2025
@zzuegg
Copy link
Member Author

zzuegg commented Sep 20, 2025

I am going reimplement to get rid of the unneccesary allocation.

Copy link
Contributor

@codex128 codex128 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how quaternions work, so take this with a grain of salt.


//Second option is to normalize manually
float norm = rotation.norm();
store.multLocal(rotation.getX()*-norm, rotation.getY()*-norm,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right to me if you're trying to normalize and invert the rotation. From looking at Quaternion, it seems like norm should be changed like this:

norm = FastMath.invSqrt(norm) / norm;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally i agree, using jme math i have not yet managed to create a non normalized quaternion.

        for (int i = 0; i < 1000000; i++) {
            Quaternion quaternion=new Quaternion().fromAngles(FastMath.nextRandomFloat()*-360,FastMath.nextRandomFloat()*360,FastMath.nextRandomFloat()*360);
            Quaternion inverse = quaternion.inverse();
            float norm = quaternion.norm();
            //norm = FastMath.invSqrt(norm)/norm;
            System.out.println(norm);
            quaternion.set(quaternion.getX()*-norm, quaternion.getY()*-norm, quaternion.getZ()*-norm, quaternion.getW()*norm);
            if(!quaternion.isSimilar(inverse,0.000001f)){
                System.out.println("Fail");
                System.out.println(quaternion);
                System.out.println(inverse);
                System.exit(1);
            }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants