Skip to content

Commit 234545d

Browse files
committed
Added worldToLocal method to Spatial for quaternion transformations and corresponding test
1 parent 8cc3086 commit 234545d

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,22 @@ public Vector3f worldToLocal(final Vector3f in, final Vector3f store) {
986986
return worldTransform.transformInverseVector(in, store);
987987
}
988988

989+
/**
990+
* Transforms the given quaternion from world space to local space relative to this object's transform.
991+
*
992+
* @param in the input quaternion in world space that needs to be transformed
993+
* @param store an optional Quaternion to store the result; if null, a new Quaternion will be created
994+
* @return the transformed quaternion in local space, either stored in the provided Quaternion or a new one
995+
*/
996+
public Quaternion worldToLocal(final Quaternion in, Quaternion store){
997+
checkDoTransformUpdate();
998+
if(store == null){
999+
store=new Quaternion(in);
1000+
}
1001+
store.multLocal(worldTransform.getRotation().inverse());
1002+
return store;
1003+
}
1004+
9891005
/**
9901006
* <code>getParent</code> retrieves this node's parent. If the parent is
9911007
* null this is the root node.

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

Lines changed: 31 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,32 @@ 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.assertEquals(worldTranslation,testNode.getWorldTranslation());
140+
Assert.assertEquals(worldRotation,testNode.getWorldRotation());
141+
142+
nodeB.attachChild(testNode);
143+
144+
Assert.assertNotEquals(worldTranslation,testNode.getWorldTranslation());
145+
Assert.assertNotEquals(worldRotation,testNode.getWorldRotation());
146+
147+
testNode.setLocalTranslation(nodeB.worldToLocal(worldTranslation,null));
148+
Assert.assertEquals(worldTranslation,testNode.getWorldTranslation());
149+
150+
testNode.setLocalRotation(nodeB.worldToLocal(worldRotation,null));
151+
Assert.assertEquals(worldRotation,testNode.getWorldRotation());
152+
}
122153
}

0 commit comments

Comments
 (0)