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

📄 canvas3dfortest.java~234~

📁 一个J2Me的赛车游戏
💻 JAVA~234~
字号:
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.m3g.*;
import java.io.*;
import javax.microedition.lcdui.*;

public class Canvas3Dfortest
    extends GameCanvas
    implements Runnable {
  private World world;

  private Tcamera cam;
  private Group level;

  private Pod pod1;
  private Pod pod2;
  private Hero hero;

  private Background bg;
  private Key key;

  //private Mesh mesh;
  private FPS mFps = new FPS(); // to obtain current frame rate

  // Global Graphics3D object
  private static Graphics3D g3d = Graphics3D.getInstance(); // Get the Graphics3D context
  private Graphics g = getGraphics();

  public static final int STRONG_RENDERING_HINTS = Graphics3D.ANTIALIAS |
      Graphics3D.TRUE_COLOR | Graphics3D.DITHER;
  public static int RENDERING_HINTS = STRONG_RENDERING_HINTS;

  Canvas3Dfortest() throws IOException {
    super(false);
    // We want a fullscreen canvas
    setFullScreenMode(true);
    // Load our world
    loadWorld();
    Global.costab();
    // Load our camera
    int width = getWidth();
    int height = getHeight();
    float[] heroposition = {
        0.0f, -0.0f};
    hero = new Hero(world, "c00.m3g", heroposition);
    cam = new Tcamera(world, hero.groupcamandcar,
                      (float) width / (float) height);
    //   distancecc = Distance();
    float[] pod1position = {
        0.0f, -20f};
    pod1 = new Pod(world, "c02.m3g", pod1position);
    float[] pod2position = {
        0.0f, 10f};
    pod2 = new Pod(world, "c08.m3g", pod2position);
    key = new Key();
    Thread runner = new Thread(this);
    runner.start();
  }

  private void loadWorld() {
    try {
      world = new World();

      Object3D[] buffer1 = Loader.load("/res/02.m3g");

      for (int i = 0; i < buffer1.length; i++) {
        if (buffer1[i] instanceof Group) {
          //  System.out.println("good");
          level = (Group) buffer1[i];
          break;
        }
      }

      buffer1 = null;
      world.addChild(level);

      level.setTranslation(790f, Global.groundtocam - 0.3f, -490f);
      level.postRotate( -90, 1.0f, 0.0f, 0.0f);
      // group.postRotate( 1, 0.0f, 1.0f, 0.0f);
      bg = new Background();
      bg.setColor(0xff6600);
      world.setBackground(bg);

      // mesh = (Mesh) level.find(57);
      // mesh.setRenderingEnable(false);
      //   int i = mesh.getVertexBuffer().getVertexCount();
      //  System.out.print(i);
    }
    catch (Exception e) {
      // ERROR!
      // System.out.println("Loading error!");
      Global.reportException(e);
    }

  }

  private void draw3d(Graphics g) {
    // Envelop all in a try/catch block just in case

    g.setClip(0, 0, getWidth(), getHeight());
    // First bind the graphics object. We use our pre-defined rendering hints.
    g3d.setViewport(0, 0, getWidth(), getHeight());
    g3d.bindTarget(g);
    g3d.getProperties();
    // Now, just render the world. Simple as pie!
    g3d.render(world);
    g3d.releaseTarget();

  }

  public void keyPressed(int i) {
    // System.out.println("press");
    key.keyPressed(i);

  }

  public void keyReleased(int i) {
    //  System.out.println("keyReleased");
    key.keyReleased(i);
  }

  public void CheckcartocarCollision() {
    float[] temp1 = new float[3];
    hero.groupcamandcar.getTranslation(temp1);
    temp1[0] = temp1[0] + 10 * (float) hero.camCosine;
    temp1[1] = 0;
    temp1[2] = temp1[2] + 10 * (float) hero.camSine;
    float[] temp2 = new float[3];
    pod1.pod.getTranslation(temp2);
    float distance = (float) Math.sqrt( (temp2[0] - temp1[0]) *
                                       (temp2[0] - temp1[0]) +
                                       (temp2[2] - temp1[2]) *
                                       (temp2[2] - temp1[2]));

    // System.out.println(distance);
    if (distance < (hero.radius + pod1.radius)) {
      // System.out.println("peng le peng le ");
      hero.groupcamandcar.setTranslation(hero.PreCarPosition[0],
                                         hero.PreCarPosition[1],
                                         hero.PreCarPosition[2]);
      hero.lastcarspeed = hero.carspeed;
      hero.carspeed = hero.carspeed / 3f;
      hero.mass = 1;
      pod1.mass = 2;
      pod1.hitbyother = true;
      pod1.Hspeed = 4;
      pod1.HrotY = hero.RotY;
    }
  }

  public void draw2d(Graphics g) {
    int textPosY = getHeight() - 30;
    mFps.calculateFps();
    g.setColor(0xFFFFFF);
    g.drawString(mFps.getFpsString(), 0, textPosY, 0); // frames per second

  }

  public void run() {
    // main process
    while (true) {
      hero.update(key);
      cam.update(key, (hero.carspeed - hero.lastcarspeed) * 5f);
      hero.lastcarspeed = hero.carspeed;
      CheckcartocarCollision();
      pod1.update();
      pod2.update();
      // Draw everything

      draw3d(g);
      draw2d(g);
      flushGraphics();

      try {
        Thread.sleep(1L);
        Thread.yield();
      }
      catch (Throwable t) {
        t.printStackTrace();
      }
    }

  }
}

⌨️ 快捷键说明

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