⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 m3gcanvas.java

📁 3D手机游戏开发实例源代码
💻 JAVA
字号:



import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;
import javax.microedition.lcdui.game.*;

public class M3GCanvas extends Canvas implements Runnable
{
  private Command exitCmd; 

  private Graphics3D g3d;
  //private Graphics g2d = getGraphics();
  private World world;

  // world elements
  private MobileCamera mobCam;
  private Group cameraGroup;
  private Mesh boat;
  // timing information
  private int appTime = 0;
  private int nextTimeToAnimate;
  
  public M3GCanvas() 
  { 

    setFullScreenMode(false);


    g3d = Graphics3D.getInstance();
    world = new World();   // create world graph
    mobCam = new MobileCamera( getWidth(), getHeight());

    world.addChild( mobCam.getCameraGroup() );
    world.setActiveCamera( mobCam.getCamera() ); 
    Light light = new Light();  // default white, directional light
    light.setMode(Light.AMBIENT);
    light.setIntensity(100.0f);  // make it a bit brighter
    light.setOrientation(-45.0f, 1.0f, 0, 0); // pointing down and into world
                  // -45
    world.addChild(light);
    Background backGnd = new Background();
    backGnd.setColor(0x00bffe); // a light blue background
    world.setBackground(backGnd);
 	  Object3D[] parts = null;
    try {
      parts = Loader.load("/boats.m3g");
    } 
    catch (Exception e) 
    {  e.printStackTrace();  }
 	  Group root = (Group) parts[0];
    boat = (Mesh) root.find(13);   // use Group with user ID id
    boat.setTranslation(0, 0.5f, 0); 
    boat.setScale(0.05f, 0.05f,0.05f);
    setUpAnimation();
    root.removeChild(boat);  
    world.addChild(boat);
    
    
    parts = null;
    try {
      parts = Loader.load("/water.m3g");
    } 
    catch (Exception e) 
    {  e.printStackTrace();  }
 	  root = (Group) parts[0];
    Mesh water = (Mesh) root.find(114);   // use Group with user ID id
    water.setTranslation(0, 0, 0); 
 //  mesh.setScale(0.05f, 0.05f,0.05f);
    root.removeChild(water);  
    world.addChild(water);
    nextTimeToAnimate = world.animate(appTime);
    Thread t = new Thread(this);
    t.start();
  }  // end of LoaderCanvas()

  protected void keyPressed(int keyCode)
  { int gameAction = getGameAction(keyCode);
    mobCam.pressedKey(gameAction);

  }

  protected void keyReleased(int keyCode)
  { int gameAction = getGameAction(keyCode);
    mobCam.releasedKey(gameAction);
  }
  protected void paint(Graphics g2d)
  {
  	   try{    

            g3d.bindTarget(g2d); // Binds the given Graphics or mutable Image2D as the rendering target of this Graphics3D
            g3d.render(world);
  
        }finally{
            g3d.releaseTarget();
        }
  	
  }
  private void setUpAnimation()
  {
    // creation animation controller 
    AnimationController animController = new AnimationController();
    animController.setActiveInterval(0, 1500);
    // animController.setSpeed(2.0f, 0);	

    // creation translation animation track 
    KeyframeSequence transKS = translateFrames();
    AnimationTrack transTrack = new AnimationTrack(transKS, AnimationTrack.TRANSLATION); 
    transTrack.setController(animController);
    boat.addAnimationTrack(transTrack);  // will affect transRotGroup

    // creation rotation animation track 
    KeyframeSequence rotKS = rotationFrames();
    AnimationTrack rotTrack = 
           new AnimationTrack(rotKS, AnimationTrack.ORIENTATION); 
    rotTrack.setController(animController);
    boat.addAnimationTrack(rotTrack);   // will affect transRotGroup
  }  // end of setUpAnimation()


  private KeyframeSequence translateFrames()
  /* One complete circular translation is carried out over 8 
     frames, then repeated. Each sequence taking 80 time units.

     The coordinates describe a circle of radius 0.5f, centered
     on the origin on the XZ plane. 
     The starting point is at -0.5f on the x-axis, which is on
     the left of the origin.
  */
  {
    KeyframeSequence ks = new KeyframeSequence(8, 3, KeyframeSequence.SPLINE);
        /* Use a spline to interpolated between the points, so the 
           movement is in a curve (actually a circle) */

    // move clockwise in a circle;
    // each frame is separated by 10 sequence time units
 /*   ks.setKeyframe(0, 0, new float[] { -0.5f, 0.5f, 0.0f });
    ks.setKeyframe(1, 10, new float[] { -1.4144f, 0.5f, 1.4144f });
    ks.setKeyframe(2, 20, new float[] { 0.0f, 0.5f, 0.5f });
    ks.setKeyframe(3, 30, new float[] { 1.4144f, 0.5f, 1.4144f });
    ks.setKeyframe(4, 40, new float[] { 0.5f, 0.5f, 0.0f });
    ks.setKeyframe(5, 50, new float[] { 1.4144f, 0.5f, -1.4144f });
    ks.setKeyframe(6, 60, new float[] { 0.0f, 0.5f, -0.5f });
    ks.setKeyframe(7, 70, new float[] { -1.4144f, 0.5f, -1.4144f });*/
    
    ks.setKeyframe(0, 0, new float[] { -2.0f, 0.5f, 0.0f });
    ks.setKeyframe(1, 10, new float[] { -1.4144f, 0.5f, 1.4144f });
    ks.setKeyframe(2, 20, new float[] { 0.0f, 0.5f, 2.0f });
    ks.setKeyframe(3, 30, new float[] { 1.4144f, 0.5f, 1.4144f });
    ks.setKeyframe(4, 40, new float[] { 2.0f, 0.5f, 0.0f });
    ks.setKeyframe(5, 50, new float[] { 1.4144f, 0.5f, -1.4144f });
    ks.setKeyframe(6, 60, new float[] { 0.0f, 0.5f, -2.0f });
    ks.setKeyframe(7, 70, new float[] { -1.4144f, 0.5f, -1.4144f });
    ks.setDuration(80);   // one cycle takes 80 sequence time units
    ks.setValidRange(0, 7);
    ks.setRepeatMode(KeyframeSequence.LOOP);

    return ks;
  }  // end of translateFrames()



  private KeyframeSequence rotationFrames()
  {
    KeyframeSequence ks = new KeyframeSequence(8, 4, KeyframeSequence.SLERP);

  /*  ks.setKeyframe(0, 0, rotYQuat(0));
    ks.setKeyframe(1, 10, rotYQuat(45));
    ks.setKeyframe(2, 20, rotYQuat(90));
    ks.setKeyframe(3, 30, rotYQuat(135));
    ks.setKeyframe(4, 40, rotYQuat(180));
    ks.setKeyframe(5, 50, rotYQuat(225));
    ks.setKeyframe(6, 60, rotYQuat(270));
    ks.setKeyframe(7, 70, rotYQuat(315));*/
    ks.setKeyframe(0, 0, rotYQuat(180));
    ks.setKeyframe(1, 10, rotYQuat(225));
    ks.setKeyframe(2, 20, rotYQuat(270));
    ks.setKeyframe(3, 30, rotYQuat(315));
    ks.setKeyframe(4, 40, rotYQuat(360));
    ks.setKeyframe(5, 50, rotYQuat(45));
    ks.setKeyframe(6, 60, rotYQuat(90));
    ks.setKeyframe(7, 70, rotYQuat(135));
    ks.setDuration(80);   // one cycle takes 80 sequence time units
    ks.setValidRange(0, 7);
    ks.setRepeatMode(KeyframeSequence.LOOP);

    return ks;
  }  // end of rotationFrames()


  private float[] rotYQuat(double angle)
  /* Calculate the quaternion for a clockwise rotation of 
     angle degress about the y-axis. */
  {
    double radianAngle = Math.toRadians(angle)/2.0;
    float[] quat = new float[4];
    quat[0] = 0.0f;    // i coefficient
    quat[1] = (float) Math.sin(radianAngle);   // j coef
    quat[2] = 0.0f;    // k coef
    quat[3] = (float) Math.cos(radianAngle);   // scalar component a
    return quat;
  }  // end of rotYQuaternion()


    public void run() {

        while(true){
        mobCam.update();
        appTime++;
    if (appTime >= nextTimeToAnimate) {
      nextTimeToAnimate = world.animate(appTime) + appTime;
      // System.out.println("nextTimeToAnimate: " + nextTimeToAnimate);
      repaint();
    }
           // flushGraphics();
        }
    }





} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -