Java3D rotate the angle of camera view?
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 stear the camera view. It works when I move it in forward/backwards and to left/right.
Now I also want to rotate the view to left or right in a angle. But the anvas just turn black.
So if any one could take a look and see if you can understand where the problem is, I would be most thankfull.
Best Regards
Fredrik
My code for this is below:
Main-class
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();
branchGroup.addChild(addTransformGroup());
//This code is for the view
Transform3D transform3D = new Transform3D();
transform3D.set(new Vector3f(0.0f, 2.0f, 0.0f));
TransformGroup transformGroup = simpleUniverse.getViewingPlatform().getViewPlatformTransform();
transformGroup.setTransform(transform3D);
//Here I add my own navigation class
canvas3D.addKeyListener( new KeyListener3D(transformGroup) );
simpleUniverse.addBranchGraph(branchGroup);
}
public TransformGroupLandSurface addTransformGroup()
{
TransformGroupLandSurface transformGroupLandSurface = new TransformGroupLandSurface(this);
return transformGroupLandSurface;
}
public static void main(String[] args)
{
Frame frame = new MainFrame(new TransformGroupTesterApplet(), 600, 400);
}
}
The navigation class:
package java3d;
import java.awt.event.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class KeyListener3D implements KeyListener
{
TransformGroup transformGroup;
float x = 0.0f;
float y = 2.0f;
float z = 0.0f;
private double angle = 0.0;
public KeyListener3D(TransformGroup tfg)
{
transformGroup = tfg;
}
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 )
{
//THIS IS THE CODE FOR THE ROTATION THAT WANT WORK FOR ME!!
Transform3D transform3D = new Transform3D();
//transformGroup.getTransform( transform3D );
transform3D.set(new Vector3f(x, y, z));
angle = angle - 0.01;
transform3D.rotX(angle);
transformGroup.setTransform(transform3D);
}
}
public void keyReleased(KeyEvent e)
{
}
}