Tutorial Java Monkey Engine-Load shape and simple anim- PART-3

brum…brum…kembali dengan Java monkey engine dengan pokok bahasan load shape dan simple anim rotation on stage…langsung saja ke source codenya silahkan dipelajari sendiri…artikel ini sekaligus sebagai documentasi saya…:D

package MY_TUTORIAL_USING_JME_2;

import Tutorial_3.TestSharedMesh;
import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.SharedMesh;
import com.jme.scene.shape.Cylinder;
import com.jme.scene.shape.Disk;
import com.jme.scene.shape.Dome;
import com.jme.scene.shape.Pyramid;
import com.jme.scene.shape.RegularPolyhedron;
import com.jme.scene.shape.RoundedBox;
import com.jme.scene.shape.Sphere;
import com.jme.scene.shape.StripBox;
import com.jme.scene.shape.Teapot;
import com.jme.scene.shape.Torus;
import com.jme.scene.shape.Tube;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;

/**
 *
 * @author me
 */
public class Loaded extends SimpleGame{

    private Quaternion Quat = new Quaternion();
    private Quaternion Quat2 = new Quaternion();
    private Quaternion Quat3 = new Quaternion();
    private Quaternion Quat4 = new Quaternion();

    private float angle = 180;
    private float angle2 = 180;
    private float angle3 = 180;
    private float angle4 = 180;

    private Vector3f axis = new Vector3f(1, 1, 0.5f).normalizeLocal();
    private Vector3f axis2 = new Vector3f(0, 0, 0.5f).normalizeLocal();
    private Vector3f axis3 = new Vector3f(1, 0, 0.5f).normalizeLocal();
    private Vector3f axis4 = new Vector3f(1, 1, 0).normalizeLocal();

    Cylinder c;
    Pyramid a2;
    Tube a3;
    Torus a4;
    Teapot a5;
    Sphere a6;
    Disk a7;
    Dome a8;

    SharedMesh post1;
    SharedMesh post2;
    SharedMesh post3;
    SharedMesh post4;
    SharedMesh post5;
    SharedMesh post6;
    SharedMesh post7;
    SharedMesh post8;
    SharedMesh post9;
    SharedMesh post10;


    public static void main(String[] args) {
       Loaded app = new Loaded();
       app.setConfigShowMode(ConfigShowMode.AlwaysShow);
       app.start();
    }

    @Override
    protected void simpleUpdate() {
        if (timer.getTimePerFrame() < 50) {
            angle = angle + (timer.getTimePerFrame() * 10);
            if (angle >= 360) {
                angle = 0;
            }
        }
        // animasi miring
        Quat.fromAngleAxis(angle, axis);
        c.setLocalRotation(Quat);
        post1.setLocalRotation(Quat);

        // animasi vertical rotating
        if (timer.getTimePerFrame() < 50) {
            angle2 = angle2 + (timer.getTimePerFrame() * 5);
            if (angle2 >= 270) {
                angle2 = 0;
            }
        }
        Quat2.fromAngleAxis(angle2, axis2);
        post3.setLocalRotation(Quat2);
        
        //animasi kebalikan dari Quat
        if (tpf < 1) {
            angle3 = angle3 + (tpf * 1);
            if (angle3 >= 270) {
                angle3 = 0;
            }
        }
        Quat3.fromAngleAxis(angle3, axis3);
        post4.setLocalRotation(Quat3);

        //animasi 
        if (tpf < 1) {
            angle4 = angle4 + (tpf * 1);
            if (angle4 >= 270) {
                angle4 = 0;
            }
        }
        Quat4.fromAngleAxis(angle4, axis4);
        post5.setLocalRotation(Quat4);

        //untuk post selajutnya silahkan dicoba sendiri

    }


    @Override
    protected void simpleInitGame() {
        display.setTitle("Tutorial 2");

        //load texturer
        TextureState ts = display.getRenderer().createTextureState();
		ts.setEnabled(true);
		ts.setTexture(TextureManager.loadTexture(
				TestSharedMesh.class.getClassLoader().getResource(
						"jmetest/data/images/Monkey.jpg"),
				Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear));




        RoundedBox a = new RoundedBox("nomos", Vector3f.UNIT_X, Vector3f.UNIT_XYZ, Vector3f.UNIT_Z);
        a.setModelBound(new BoundingBox());
        a.updateModelBound();

        //new Pyramid("te", luas alas, tinggi);
        a2 = new Pyramid("te", 3f, 20f);
        a2.setModelBound(new BoundingBox());
        a2.updateModelBound();

        //Tube("tibe", diameter, lebar, tinggi)
        a3 = new Tube("tibe", 0.1f, 2f, 3f);
        a3.setModelBound(new BoundingBox());
        a3.updateModelBound();

        a4 = new Torus("TORUS", 100, 100, 0.1f, 0.9f);
        a4.setModelBound(new BoundingBox());
        a4.updateModelBound();

        a5 = new Teapot("Teapot");
        a5.setModelBound(new BoundingBox());
        a5.updateModelBound();

        a6 = new Sphere("SPHERE", 10, 10, 3f);
        a6.setModelBound(new BoundingBox());
        a6.updateModelBound();

        a7 = new Disk("disk", 30, 3, 3f);
        a7.setModelBound(new BoundingBox());
        a7.updateModelBound();
        
        a8 = new Dome("DOME", Vector3f.UNIT_X, 30, 30, 3f);
        a8.setModelBound(new BoundingBox());
        a8.updateModelBound();


        //bentuk cylinder vertical
        //Cylinder(string,,,diameter,tinggi);
        c= new Cylinder("nyos", 10, 50, 1, 10);
        Quat = new Quaternion();
        Quat.fromAngleAxis(FastMath.PI/2, new Vector3f(1,0,0));
        c.setLocalRotation(Quat);
        c.setModelBound(new BoundingBox());
        c.updateModelBound();

        Cylinder c2 = new Cylinder("nyos", 10, 10, 1,10);
        c2.setModelBound(new BoundingBox());
        c2.updateModelBound();

        post1 = new SharedMesh("post1", c);
        post1.setLocalTranslation(new Vector3f(0, 0, -15));
        post2 = new SharedMesh("post2", c2);
        post2.setLocalTranslation(new Vector3f(1,0.5f,0));
        post3 = new SharedMesh("post3",a);
        post3.setLocalTranslation(new Vector3f(3,0.5f,5));
        post4 = new SharedMesh("post4",a2);
        post4.setLocalTranslation(new Vector3f(6,0.5f,0));
        post5 = new SharedMesh("post5",a3);
        post5.setLocalTranslation(new Vector3f(9,0.5f,0));
        post6 = new SharedMesh("post6",a4);
        post6.setLocalTranslation(new Vector3f(12,0.5f,0));
        post7 = new SharedMesh("post6",a5);
        post7.setLocalTranslation(new Vector3f(15,0.5f,0));
        post8 = new SharedMesh("post6",a6);
        post8.setLocalTranslation(new Vector3f(18,0.5f,10));
        post9 = new SharedMesh("post6",a7);
        post9.setLocalTranslation(new Vector3f(30,0.5f,10));
        post10 = new SharedMesh("post6",a8);
        post10.setLocalTranslation(new Vector3f(35,0.5f,10));

        //memberi texture di sharedMesh...
        post1.setRenderState(ts);
        post4.setRenderState(ts);
        post5.setRenderState(ts);
        post6.setRenderState(ts);
        post7.setRenderState(ts);
        post8.setRenderState(ts);
        post9.setRenderState(ts);
        post10.setRenderState(ts);

        //menampilkan dilayar dan menambahkan pada Child
        Node nodes = new Node("port");
        nodes.attachChild(post1);
        //nodes.attachChild(post2);
        nodes.attachChild(post3);
        nodes.attachChild(post4);
        nodes.attachChild(post5);
        nodes.attachChild(post6);
        nodes.attachChild(post7);
        nodes.attachChild(post8);
        nodes.attachChild(post9);
        nodes.attachChild(post10);
        
        rootNode.attachChild(nodes);
    }

}

em untuk penjelasannya silahkan lihat documentnya diatas listingnya..:P
hasilnya setelah dicompile:

2 thoughts on “Tutorial Java Monkey Engine-Load shape and simple anim- PART-3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s