javajava-3d

Java3D - Is it possible to have a viewingPlatform that tracks a moving object


I have a very simple Java3D application, it contains two cubes, of which one of them orbits the other.

Like so:

enter image description here

As you can see, the default position for the viewing platform is from a 'kind of' birds eye view.

The code that looks like so

TransformGroup cameraTG = u.getViewingPlatform().
                   getViewPlatformTransform();
Vector3f translate = new Vector3f(); 
Transform3D T3D = new Transform3D();
translate.set( 0.0f, 0.0f, 20.0f);
T3D.setTranslation(translate);
cameraTG.setTransform(T3D);

My question is, is it possible to set the viewing platform to track the larger cube (the cube that rotates around the smaller square). Or more straight forward, is it possible for the viewing platform to rotate around a given body?

More information: My goal is to have a miniature solar system, containing the Sun, Earth and moon. And I would like to view it from the point of view of the earth (almost like a view from the ISS)

enter image description here

Any help or pointers would be fantastic. Please feel free to ask for more information if needed.


Solution

  • You have a nice example here http://java3d.nl/Tutorials/Java/Java3d/Controlthecamera_12.php

    The original http://java3d.nl website is no longer available, but you can still use the cached version from the Internet Archive: https://web.archive.org/web/20131218022035/http://java3d.nl/Tutorials/Java/Java3d/Controlthecamera_12.php

             this.camera = this.universe.getViewingPlatform().getViewPlatformTransform();
    
             //Add things to the universe
             this.root = new BranchGroup();
             this.root.addChild(new ColorCube(0.2));
             this.universe.addBranchGraph(root);
    

    My idea is like this:

    BranchGroup b=new BranchGroup();
    b.addChild(cube);
    b.addChild(camera);
    

    then in a loop where you rotate the block:

    while(true) {
      ... b.getChild()......... etc
      apply transform
    }
    

    or more specifically

    for(j=0; j<group.numChildren(); j++) {
        Node ni=group.getChild(j);
        if(ni instanceof TransformGroup) {
          Transform3D tdi=new Transform3D();
          TransformGroup tgi=(TransformGroup)group.getChild(j);
          tgi.getTransform(tdi);
          Transform3D rotation = new Transform3D();
          rotation.rotX(Math.toRadians(0.001*i));
          tdi.mul(rotation);
          tgi.setTransform(tdi);
        }
    }