Aug 20, 2014

22. Eight Color Cubes


In this application, we have an eight-color cube rotating around its vertical axis.




We always have to create app, an instance of our extended SimpleApplication class. It then has to be started.


    // *** 1. Start (Extended SimpleApplication)
    public static void main(String[] args) {
        JMonkey2 app = new JMonkey2();
        app.start();
    }
    // *** 1. End



In the simpleInitApp() override, which is called by start(), we first create arrays to hold the shapes, geometries, and materials.


        // *** 2. Start (8 box, 8 geom, 8 mat)
        Box[] box = new Box[8];
        Geometry[] geom = new Geometry[8];
        Material[] mat = new Material[8];
        // *** 2. End



Then, we use constructors to set the box shapes, geometries and materials. The materials are unshaded (no color needed for visualization). This also means that they will not show 3D effects such as being lighter near light source, etc. All 8 materials are however different colors.


        // *** 3. Start (8 random color cubes)
        for (int i= 0; i<8; i++) {
            box[i] = new Box(.5f,.5f,.5f);
            geom[i] = new Geometry("Box" + i,box[i]);
            mat[i] = new Material(assetManager,
                    "Common/MatDefs/Misc/Unshaded.j3md");
            mat[i].setColor("Color", ColorRGBA.randomColor());
            geom[i].setMaterial(mat[i]);
        }
        // *** 3. End



The constructor creates the 8 boxes, centered at the origin. We have to translate them, so they do not overlap. Since there are 3 axes, we need 2 to power of 3 translations (2^3 = 8).


        // *** 4. Start (Translate to unique positions)
        geom[0].setLocalTranslation(0.5f,0.5f,0.5f);
        geom[1].setLocalTranslation(0.5f,0.5f,-0.5f);
        geom[2].setLocalTranslation(0.5f,-0.5f,0.5f);
        geom[3].setLocalTranslation(0.5f,-0.5f,-0.5f);
        geom[4].setLocalTranslation(-0.5f,0.5f,0.5f);
        geom[5].setLocalTranslation(-0.5f,0.5f,-0.5f);
        geom[6].setLocalTranslation(-0.5f,-0.5f,0.5f);
        geom[7].setLocalTranslation(-0.5f,-0.5f,-0.5f);
        // *** 4. End



The eight-cubes are attached to rootNode. This will make them part of the scene (scene graph).


        // *** 5. Start (Attach all 8 to rootNode)
        for (int i=0; i<8; i++) {
            rootNode.attachChild(geom[i]);
        }
        // *** 5. End



During each update at a rate of frames per second the entire-cube is rotated, in the vertical axis (y).


        // *** 6. Start (rotate by 90 deg/sec)
        rootNode.rotate(0f, 90*tpf*FastMath.DEG_TO_RAD, 0f);
        // *** 6. End



// JMonkey2.java
package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;

public class JMonkey2 extends SimpleApplication {
    // *** 1. Start (Extended SimpleApplication)
    public static void main(String[] args) {
        JMonkey2 app = new JMonkey2();
        app.start();
    }
    // *** 1. End

    @Override
    public void simpleInitApp() {
        // *** 2. Start (8 box, 8 geom, 8 mat)
        Box[] box = new Box[8];
        Geometry[] geom = new Geometry[8];
        Material[] mat = new Material[8];
        // *** 2. End
        // *** 3. Start (8 random color cubes)
        for (int i= 0; i<8; i++) {
            box[i] = new Box(.5f,.5f,.5f);
            geom[i] = new Geometry("Box" + i,box[i]);
            mat[i] = new Material(assetManager,
                    "Common/MatDefs/Misc/Unshaded.j3md");
            mat[i].setColor("Color", ColorRGBA.randomColor());
            geom[i].setMaterial(mat[i]);
        }
        // *** 3. End
        // *** 4. Start (Translate to unique positions)
        geom[0].setLocalTranslation(0.5f,0.5f,0.5f);
        geom[1].setLocalTranslation(0.5f,0.5f,-0.5f);
        geom[2].setLocalTranslation(0.5f,-0.5f,0.5f);
        geom[3].setLocalTranslation(0.5f,-0.5f,-0.5f);
        geom[4].setLocalTranslation(-0.5f,0.5f,0.5f);
        geom[5].setLocalTranslation(-0.5f,0.5f,-0.5f);
        geom[6].setLocalTranslation(-0.5f,-0.5f,0.5f);
        geom[7].setLocalTranslation(-0.5f,-0.5f,-0.5f);
        // *** 4. End
        // *** 5. Start (Attach all 8 to rootNode)
        for (int i=0; i<8; i++) {
            rootNode.attachChild(geom[i]);
        }
        // *** 5. End
    }

    @Override
    public void simpleUpdate(float tpf) {
        // *** 6. Start (rotate by 90 deg/sec)
        rootNode.rotate(0f, 90*tpf*FastMath.DEG_TO_RAD, 0f);
        // *** 6. End
    }
}


Output: (motion due to pressing Q - camera up and Z -camera down)



No comments:

Post a Comment