hi all
suppose i rotate a spatial, i’d like to restore spatial x y z axis ,but not the spatial, to initial location
if someone help me how to include an image i can show you what i mean
What do you mean? You are rotating the spatial a few times and after that you want to restore its original (local) rotation?
For that you could do
[java]
Quaternion orig = spatial.getLocalRotation().clone();
//rotate the spatial
…
//reset the rotation
spatial.setLocalRotation(orig);
[/java]
but with spatial.setLocalRotation(orig) the spatial rotate with its axis.
I just want to bring spatial axis x y z to origin position.
I mean i rotate the spatial and after that I want that the spatial remain in that position but axis x y z return to origin position.
What I want is to rotate the spatial not on its axis but on world axis
Hmm i think i understand now:
You want the local rotation to be set to (0,0,0), so you have to adjust the world rotation accordingly (NOT POSSIBLE), so that the spatial retains its rotation.
I really really hope that works, if not, someone more expirienced on this forums will have to help us
[java]
ArrayList<Quaternion> rotations = new ArrayList<Quaternion>();
Spatial originalSpatial = null;
Quaternion worldRotation = originalSpatial.getWorldRotation().clone();
Spatial sp = originalSpatial;
while (sp.getParent() != null) {
rotations.add(0, sp.getLocalRotation());
sp=sp.getParent();
}
for (Iterator<Quaternion> iter = rotations.iterator(); iter.hasNext(); ) {
Quaternion rotation = iter.next();
worldRotation.multLocal(rotation);
}
originalSpatial.setLocalRotation(Quaternion.ZERO);
sp.setLocalRotation(worldRotation);
[/java]
EDIT: removed originalSpatial.updateGeometricState();
ummm a real thanks for your reply but…
suppose you rotate the spatial pi/2 on the right and then pi/2 up.The spatial does not follow mouse drag direction.
The axis xyz have to return to origin position but not the spatial
i found that
[java]originalSpatial.setLocalRotation(Quaternion.DIRECTION_Z);[/java]
bring the spatial to original position but i want that just spatial axis x y z move to original position not the spatial
i want to reset x y z spatial axis.
[java]originalSpatial.getWorldRotation().set(Quaternion.DIRECTION_Z);[/java]
does not work as well.
If I try to detach the spatial from the rootnode and attach it again ,could not exist a smart work around?
If you modify the spatials local transforms, you have to update its geometric state first, before you can alter its world transforms in the same update loop
spatial.setLocalRotation(…);
…
rootNode.updateGeometricState();
spatial.getWorldRotation.set(…);
i hope this works
In my RawInputListener implementation I’ve done
[java] public void onMouseMotionEvent(MouseMotionEvent evt) {
if (buttonPressed) {
Quaternion qy = new Quaternion();
Quaternion qx = new Quaternion();
qx.fromAngleAxis(x0.01f, Vector3f.UNIT_Y);
qy.fromAngleAxis(-y0.01f, Vector3f.UNIT_X);
mySpatial.setLocalRotation(qy.multLocal(qx));
}
}
…
public void onMouseButtonEvent(MouseButtonEvent evt) {
buttonPressed = evt.isPressed();
if (evt.isReleased()) {
rootNode.updateGeometricState();
myspatial.getWorldRotation().set(Quaternion.DIRECTION_Z);
}
}
[/java]
So if I rotate the spatial down 90° and then rotate spatial right 90°, it rotate not in mouse drag direction but on screen normal axis!
I really need to reset world rotation axis without touch spatial axis
I dont get it
But im pretty sure you can not modify the world rotation directly (sry for posting wrong code): the world rotation is calculated after every update, based on ALL local rotations in the rootNode tree.
[java]
public void onMouseMotionEvent(MouseMotionEvent evt) {
if (buttonPressed) {
Quaternion q=new Quaternion();
q.fromAngles(-y0.01f, x0.01f, 0f);
mySpatial.setLocalRotation(mySpatial.getLocalRotation().mult(q));
}
}
[/java]
Would this rotate the spatial correctly?
Oh-my-god!!! Dilembo, delete that code now!!!
getXXX().set() = NO GO!
updateXXX() = NO GO!
normen sorry?
I prefer
[java]spatial.rotate(-y0.008f,x0.008f,0);[/java]
and a sort of spatial axis reset when mouse is released
[java]
public void onMouseButtonEvent(MouseButtonEvent evt) {
buttonPressed = evt.isPressed();
if (evt.isReleased()) {
// here the unknown spatial axis reset …???
}
}[/java]
Dont do things like getLocalTranslation().set() and dont call updateGeometricState, updateLogicState or any of the updateXXX methods.
Wouldnt you need updateGeometricState() if you modified the local transforms in order to get the “fresh” world transform in the same update method?
No
so normen please suggest us a solution
oh my god oh my god
Oh sorry my bad, i read an old issue
It was node.updateWorldVectors() back then!!
dilembo said:
so normen please suggest us a solution
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:math_for_dummies
already studied it’s not enough for my problem
nego said:
Oh sorry my bad, i read an old issue
It was node.updateWorldVectors() back then!!
node.updateWorldVectors()
does not exist in jme3
what do u mean nego?
You can find out the “local world location” between an a spatial and an arbitrary parent like this:
[java]
private static Transform getTransform(Spatial spat, Spatial parent) {
Transform shapeTransform = new Transform();
Node parentNode = spat.getParent();
Spatial currentSpatial = spat;
while (parentNode != null && parentNode != parent) {
shapeTransform.combineWithParent(currentSpatial.getLocalTransform());
currentSpatial = parentNode;
parentNode = currentSpatial.getParent();
}
return shapeTransform;
}
[/java]
Is that enough?