Skip to content

Commit a63ea7b

Browse files
authored
Merge pull request #2559 from zzuegg/worldToLocal
Added convenience method worldToLocal(Quaternion) to spatial.
2 parents 101b786 + 2eb11cf commit a63ea7b

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

jme3-core/src/main/java/com/jme3/scene/Spatial.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,28 @@ public Vector3f worldToLocal(final Vector3f in, final Vector3f store) {
998998
return worldTransform.transformInverseVector(in, store);
999999
}
10001000

1001+
/**
1002+
* Transforms the given quaternion from world space to local space relative to this object's transform.
1003+
*
1004+
* @param in the input quaternion in world space that needs to be transformed
1005+
* @param store an optional Quaternion to store the result; if null, a new Quaternion will be created
1006+
* @return the transformed quaternion in local space, either stored in the provided Quaternion or a new one
1007+
*/
1008+
public Quaternion worldToLocal(final Quaternion in, Quaternion store){
1009+
checkDoTransformUpdate();
1010+
if(store == null){
1011+
store=new Quaternion(in);
1012+
}else{
1013+
store.set(in);
1014+
}
1015+
TempVars tempVars = TempVars.get();
1016+
Quaternion worldRotation = tempVars.quat1.set(getWorldRotation());
1017+
worldRotation.inverseLocal();
1018+
store.multLocal(worldRotation);
1019+
tempVars.release();
1020+
return store;
1021+
}
1022+
10011023
/**
10021024
* <code>getParent</code> retrieves this node's parent. If the parent is
10031025
* null this is the root node.

jme3-core/src/test/java/com/jme3/scene/SpatialTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
*/
3232
package com.jme3.scene;
3333

34+
import com.jme3.math.FastMath;
35+
import com.jme3.math.Quaternion;
36+
import com.jme3.math.Vector3f;
3437
import com.jme3.scene.control.UpdateControl;
3538
import org.junit.Assert;
3639
import org.junit.Test;
@@ -119,4 +122,33 @@ public void testAddControlAt() {
119122
Assert.assertEquals(testSpatial, control1.getSpatial());
120123
Assert.assertEquals(testSpatial, control2.getSpatial());
121124
}
125+
126+
@Test
127+
public void testTransferToOtherNode(){
128+
Node nodeA = new Node("nodeA");
129+
Node nodeB = new Node("nodeB");
130+
Node testNode=new Node("testNode");
131+
nodeA.setLocalTranslation(-1,0,0);
132+
nodeB.setLocalTranslation(1,0,0);
133+
nodeB.rotate(0,90* FastMath.DEG_TO_RAD,0);
134+
testNode.setLocalTranslation(1,0,0);
135+
nodeA.attachChild(testNode);
136+
Vector3f worldTranslation = testNode.getWorldTranslation().clone();
137+
Quaternion worldRotation = testNode.getWorldRotation().clone();
138+
139+
Assert.assertTrue(worldTranslation.isSimilar(testNode.getWorldTranslation(),1e-6f));
140+
Assert.assertTrue(worldRotation.isSimilar(testNode.getWorldRotation(),1e-6f));
141+
142+
nodeB.attachChild(testNode);
143+
144+
Assert.assertFalse(worldTranslation.isSimilar(testNode.getWorldTranslation(),1e-6f));
145+
Assert.assertFalse(worldRotation.isSimilar(testNode.getWorldRotation(),1e-6f));
146+
147+
testNode.setLocalTranslation(nodeB.worldToLocal(worldTranslation,null));
148+
Assert.assertTrue(worldTranslation.isSimilar(testNode.getWorldTranslation(),1e-6f));
149+
150+
testNode.setLocalRotation(nodeB.worldToLocal(worldRotation,null));
151+
System.out.println(testNode.getWorldRotation());
152+
Assert.assertTrue(worldRotation.isSimilar(testNode.getWorldRotation(),1e-6f));
153+
}
122154
}

0 commit comments

Comments
 (0)