dcsimg
Rotate a cube in Java3D
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Fredrik_Andersson
Posted On:   Wednesday, October 15, 2003 03:13 AM

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 a   More>>

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.





Best Regards
Fredrik





You will only need the classes below for this!






import java.applet.*;
import java.awt.*;
import java.awt.Frame;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.behaviors.keyboard.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.*;

import java.util.*;


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);

//The cube
TransformGroup cube = new TransformGroup();
Transform3D cubetransform3D = new Transform3D();
cubetransform3D.set(new Vector3f(0.0f, 2.0f, -2.0f));
cube.setTransform(cubetransform3D);
cube.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
branchGroup.addChild(cube);
cube.addChild(new ColorCube(0.4));

canvas3D.addKeyListener( new KeyListener3D(transformGroup, cube) );

simpleUniverse.addBranchGraph(branchGroup);
}

public static void main(String[] args)
{
Frame frame = new MainFrame(new TransformGroupTesterApplet(), 600, 400);
}
}






//The KeyListener






import java.awt.event.*;
import javax.media.j3d.*;
import javax.vecmath.*;

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;

public KeyListener3D(TransformGroup tfg, TransformGroup tfg2)
{
transformGroup = tfg;
transformGroup2 = tfg2;
}

public void keyTyped(KeyEvent e)
{
}

public void keyPressed(KeyEvent e)
{
if( e.getKeyCode() == KeyEvent.VK_UP )
{
Transform3D transform3D = new Transform3D();
z = z - 0.1f;
transform3D.set(new Vector3f(x, y, z));
transformGroup.setTransform(transform3D);
}
else if( e.getKeyCode() == KeyEvent.VK_DOWN )
{
Transform3D transform3D = new Transform3D();
z = z + 0.1f;
transform3D.set(new Vector3f(x, y, z));
transformGroup.setTransform(transform3D);
}
else if( e.getKeyCode() == KeyEvent.VK_LEFT )
{
Transform3D transform3D = new Transform3D();
x = x - 0.1f;
transform3D.set(new Vector3f(x, y, z));
transformGroup.setTransform(transform3D);
}
else if( e.getKeyCode() == KeyEvent.VK_RIGHT )
{
Transform3D transform3D = new Transform3D();
x = x + 0.1f;
transform3D.set(new Vector3f(x, y, z));
transformGroup.setTransform(transform3D);
}
else if( e.getKeyCode() == KeyEvent.VK_SPACE )
{
//THE CUBE ROTATION THAT DO NOT WORK
Transform3D transform3D = new Transform3D();
transform3D.set(new Vector3f(0.0f, 4.0f, -2.0f));
angle += 0.1f;
transform3D.rotY(angle);
transformGroup2.setTransform(transform3D);

}
}

public void keyReleased(KeyEvent e)
{
}
}

   <<Less
About | Sitemap | Contact