Search code examples
javaviewcameraanglejava-3d

How do I rotate the camera/viewPlatform in java 3d


I just started java3D and I can move the camera up/down/left/right/forwards/backwards, but I can't seem to figure out how to make the camera angle change, for example, change from looking forward to looking left. Could someone give me an example? Also, should I be putting getViewingPlatform().getViewPlatformTransform().setTransform(test.position); in my main method, or should I do something else?

I've tried just using the mouse and rotating the view OrbitBehavior orbit = new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ROTATE);

orbit.setSchedulingBounds(new BoundingSphere());
orbit.setRotXFactor(2);
orbit.setRotYFactor(2);

But it doesnt work when I add getViewingPlatform().getViewPlatformTransform().setTransform(test.position); to the while loop in the main method. The viewing angle repeatedly gets reset.


Solution

  • Instead of steering the viewer (camera) using angles, you can steer it by defining a 3D "gazePoint" (the point attention is focussed on). To keep the camera from tilting sideways, define upDir = [ 0 , 1, 0 ] (J3D assumes +y-axis is up).

    Then, modify the viewingTransform that lives in simpleUniverse.

    Here is example code from my app:

    // ***********declare these variables in class where canvas3D lives *****
    //J3D stuff
    public InteractiveCanvas3D  canvas3D;
    public SimpleUniverse       simpleUniv;
    private Transform3D         viewingTransform;
    private TransformGroup viewingTransformGroup;
    
    public  static Point3d viewersLocation;
    public  static Point3d gazePoint;  //point viewer is looking at
    
    // and initialize as follows:
    
    viewingTransformGroup = simpleUniv.getViewingPlatform().getViewPlatformTransform();
    viewingTransform = new Transform3D();
    
    
    //called to update viewer's location and gaze:
    // *********************** UpdateViewerGeometryJ3D 
    public void UpdateViewerGeometryJ3D()  {
        Point3d eye = viewersLocation;  
        Point3d center = gazePoint;
        Vector3d up = upDir;
        viewingTransform.lookAt(eye, center, up);
        viewingTransform.invert();
        viewingTransformGroup.setTransform(viewingTransform);
    

    I forget why the viewing transform has to be inverted. Hope this helps.