Search code examples
javajava-3d

Java3D universe size is too small to fit object


The default universe bounds are x=[-1 meter,1 meter], y=[-1,1] and z=[-1,1]. When objects intersect these bounds they are rendered only partially. How can I set another universe size?


Solution

  • Well actually, the Default Universe technically is unlimited, it is a virtual universe after all.

    This is how the Java3D universe works:

    enter image description here

    Source:

    So the universe is not too small, your object is just simply too big for the view you are trying to get. Image standing right next to a massive building, theres no way you can see the whole thing at once, but if you step back you can all of it from a distance and it appears smaller. So, you either need to move the object or move your view. Here is how you would move your object:

    TransformGroup moveGroup = new TransformGroup();
    Transform3D move = new Transform3D();
    move.setTranslation(new Vector3f(0.0f, 0.0f, -10.0f));
    //^^ set the Vector3f to where you want the object to be
    moveGroup.setTransform(move);
    moveGroup.addChild(YOUR_OBJECT);
    

    You can also change where your viewing platform is viewing from. Here is how you would do that:

    public class MoveEye extends Applet
    {
        public MoveEye()
        {
            setLayout(new BorderLayout());
            GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
            Canvas3D canvas = new Canvas3D(config);
            add("Center", canvas);
    
            BranchGroup content = getScene();
            content.compile();
    
            SimpleUniverse universe = new SimpleUniverse(canvas);
            Transform3D move = lookTowardsOriginFrom(new Point3d(0, 0, -3));
            universe.getViewingPlatform().getViewPlatformTransform().setTransform(move);
            universe.addBranchGraph(content);
        }
    
        public BranchGroup getScene()
        {
            BranchGroup group = new BranchGroup();
    
            group.addChild(new ColorCube(.5));
    
            return group;
        }
    
        public Transform3D lookTowardsOriginFrom(Point3d point)
        {
            Transform3D move = new Transform3D();
            Vector3d up = new Vector3d(point.x, point.y + 1, point.z);
            move.lookAt(point, new Point3d(0.0d, 0.0d, 0.0d), up);
    
            return move;
        }
    
        public static void main(String args[])
        {
            Frame frame = new MainFrame(new MoveEye(), 256, 256);
        }
    }
    

    It's probably good practice to just move the View instead of moving the object but in this case you can do either i suppose, i hope this helped!