Hi,
I know it is another thread about rotation, but I am a bit confused here about Quaternions.
I have 4 cubes, one is the master, the others rotx,roty,rotz, I want when the user press space each cube should represent the respective rotation from the master cube.
I tried some code but I got very odd result, somehow, the master cube got rotated even if there is no code for it, and geomx cube got no rotation at all !
Code:
public class Main extends SimpleApplication {
Geometry geom1,geomX,geomY,geomZ; final float speed = 0.001f;
public static void main(String[] args) { Main app = new Main(); app.start(); }
@Override public void simpleInitApp() {
flyCam.setEnabled(false); Box b = new Box(1, 1, 1);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.getAdditionalRenderState().setWireframe(true);
mat.setColor("Color", ColorRGBA.White);
geom1 = new Geometry("Box", b); geom1.setLocalTranslation(0,2,0);
geomX = new Geometry("Box", b); geomX.setLocalTranslation(-3.5f,-1,0);
geomY = new Geometry("Box", b); geomY.setLocalTranslation(0,-1,0);
geomZ = new Geometry("Box", b); geomZ.setLocalTranslation(3.5f,-1,0);
geom1.setMaterial(mat); geomX.setMaterial(mat); geomY.setMaterial(mat); geomZ.setMaterial(mat);
rootNode.attachChild(geom1); rootNode.attachChild(geomX); rootNode.attachChild(geomY); rootNode.attachChild(geomZ);
inputManager.addMapping("RotateXup", new KeyTrigger(KeyInput.KEY_Q)); inputManager.addMapping("RotateXdown", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("RotateYup", new KeyTrigger(KeyInput.KEY_A)); inputManager.addMapping("RotateYdown", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("RotateZup", new KeyTrigger(KeyInput.KEY_Z)); inputManager.addMapping("RotateZdown", new KeyTrigger(KeyInput.KEY_X));
inputManager.addMapping("RefreshRot", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addListener(analogListener,"RotateXup", "RotateYup", "RotateZup","RotateXdown","RotateYdown","RotateZdown","RefreshRot");
}
private AnalogListener analogListener = new AnalogListener() {
public void onAnalog(String name, float value, float tpf) {
if(name.equals("RotateXup")) geom1.rotate(speed, 0, 0); if(name.equals("RotateXdown")) geom1.rotate(-speed, 0, 0);
if(name.equals("RotateYup")) geom1.rotate(0, speed, 0); if(name.equals("RotateYdown")) geom1.rotate(0, -speed, 0);
if(name.equals("RotateZup")) geom1.rotate(0, 0, speed); if(name.equals("RotateZdown")) geom1.rotate(0, 0, -speed);
if(name.equals("RefreshRot")) {
Quaternion origem = geom1.getLocalRotation();
origem.set(origem.getX(), 0, 0, 0);
geomX.setLocalRotation(origem);
}
}
};
Any help is appreciated !
Tx,
wagfeliz