15. oktober 2003 - 08:56Der er
8 kommentarer og 1 løsning
Rotate a object at a certain cordinate with Java3D
Hello!
I have build a small app in Java3D. I try to develop my own navigation, (I dont want to use the KeyNavigation). So I start to develop a class that implements the KeyListener interface. This class shall in the end stear the camera view. It works when I move it in forward/backwards and to left/right.
I also want to be able to rotate the camera view about the Y-axis. To simplify and illustrate this question I rotate a Cube instead. But it do not work as I want.
If you try the code below you will see a big cube, start to back away from the cube by pressing the VK_DOWN-key. Back pretty far. Now push the VK_SPACE and the cube suddenly jumps to the 0,0,0 cordinate and push space even more and you will see that it rotate.
I want the cube to say at its orgin cordinate and to rotate at that point, not jump away.
So if any one could tell me to get the cube not to jump away, please let me know.
public class TransformGroupTesterApplet extends Applet { BranchGroup branchGroup; public void init() { setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas3D = new Canvas3D(config); add("Center", canvas3D); SimpleUniverse simpleUniverse = new SimpleUniverse(canvas3D);
branchGroup = new BranchGroup();
//Start point for the camera Transform3D transform3D = new Transform3D(); transform3D.set(new Vector3f(0.0f, 2.0f, 0.0f)); TransformGroup transformGroup = simpleUniverse.getViewingPlatform().getViewPlatformTransform(); transformGroup.setTransform(transform3D);
Then this would probably be just as good: else if( e.getKeyCode() == KeyEvent.VK_SPACE ) { //THE CUBE ROTATION THAT DO NOT WORK Transform3D transform3D = new Transform3D(); angle += 0.1f; transform3D.rotY(angle); transformGroup2.setTransform(transform3D); }
(I have not downloaded Java3D - no test has been carried out..)
I *think* that's because you initialize transform3D every time you handle the event. The transformation of the object is probably initialized to (0, 0, 0).
Perhaps you should try something like this: public class KeyListener3D implements KeyListener { TransformGroup transformGroup; TransformGroup transformGroup2; float x = 0.0f; float y = 2.0f; float z = 0.0f; private float angle = 0.0f; private Transform3D cubeTransform;
Just run it like a app since we got the main like this: public static void main(String[] args) { Frame frame = new MainFrame(new TransformGroupTesterApplet(), 600, 400); }
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.