Skip to content

Added convenience method worldToLocal(Quaternion) to spatial. - #2559

Merged
yaRnMcDonuts merged 7 commits into
jMonkeyEngine:masterfrom
zzuegg:worldToLocal
Nov 25, 2025
Merged

Added convenience method worldToLocal(Quaternion) to spatial. #2559
yaRnMcDonuts merged 7 commits into
jMonkeyEngine:masterfrom
zzuegg:worldToLocal

Conversation

@zzuegg

@zzuegg zzuegg commented Sep 19, 2025

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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

zzuegg commented Sep 20, 2025

Copy link
Copy Markdown
Member Author

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

@codex128 codex128 left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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);
            }
        }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I could use temp vars and use the existing math functions. I probably prefer that. Considering that this code is probably never executed in the "hot loop"

…mized quaternion handling and removed redundant comments.
@yaRnMcDonuts
yaRnMcDonuts merged commit a63ea7b into jMonkeyEngine:master Nov 25, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants