jMonkeyEngine译文 FlagRush9(2)——墙壁检测系统(Detection System)

注:本系列教程全部翻译完之后可能会以PDF的形式发布。

如果有什么错误可以到http://blog.csdn.net/kakashi8841留言或EMAILkakashi9bi@gmail.com给我。

 

jME版本 jME_2.0.1_Stable

开发工具:MyEclipse8.5

操作系统:Window7/Vista

9.5Lesson9.java

 

import java.io.IOException;

import java.net.URL;

import java.util.HashMap;

 

import javax.swing.ImageIcon;

 

 

import com.jme.app.BaseGame;

import com.jme.bounding.BoundingBox;

import com.jme.image.Texture;

import com.jme.input.ChaseCamera;

import com.jme.input.InputHandler;

import com.jme.input.KeyBindingManager;

import com.jme.input.KeyInput;

import com.jme.input.thirdperson.ThirdPersonMouseLook;

import com.jme.light.DirectionalLight;

import com.jme.math.FastMath;

import com.jme.math.Vector3f;

import com.jme.renderer.Camera;

import com.jme.renderer.ColorRGBA;

import com.jme.renderer.Renderer;

import com.jme.scene.Node;

import com.jme.scene.Skybox;

import com.jme.scene.state.CullState;

import com.jme.scene.state.LightState;

import com.jme.scene.state.TextureState;

import com.jme.scene.state.ZBufferState;

import com.jme.system.DisplaySystem;

import com.jme.system.JmeException;

import com.jme.util.TextureManager;

import com.jme.util.Timer;

import com.jme.util.export.binary.BinaryImporter;

import com.jmex.terrain.TerrainBlock;

import com.jmex.terrain.util.MidPointHeightMap;

import com.jmex.terrain.util.ProceduralTextureGenerator;

 

public class Lesson9 extends BaseGame{

 

    private int width,height;

    private int freq,depth;

    private boolean fullscreen;

   

    //我们的camera对象,用于观看scene

    private Camera cam;

   

    protected Timer timer;

    private Node scene;

    private TextureState ts;

   

    private TerrainBlock tb;

   

    private ForceFieldFence fence;

    private Skybox skybox;

   

    private Vehicle player;

    private ChaseCamera chaser;

   

    private InputHandler input;

   

    //保存terrain的任何一个给出点的法向

    private Vector3f normal = new Vector3f();

    private float agl;

    private Flag flag;

   

    private CollisionDetection bounce;

    private Node walls;

   

    public static void main(String[] args) {

       Lesson9 app = new Lesson9();

       java.net.URL url = app.getClass().getClassLoader().getResource("res/logo.png");

       app.setConfigShowMode(ConfigShowMode.AlwaysShow,url);

       app.start();

    }

 

    /*

     * 清除texture

     */

    protected void cleanup() {

       ts.deleteAll();

    }

 

    protected void initGame() {

       display.setTitle("Flag Rush");

       scene = new Node("Scene Graph Node");

      

       ZBufferState buf = display.getRenderer().createZBufferState();

       buf.setEnabled(true);

       buf.setFunction(ZBufferState.TestFunction.LessThanOrEqualTo);

       scene.setRenderState(buf);

      

       buildTerrain();

       buildFlag();

       buildLighting();

       buildEnvironment();

       createSkybox();

       buildPlayer();

       buildChaseCamera();

       buildInput();

      

       bounce = new CollisionDetection(

              player, walls, CollisionDetection.MOVE_BOUNCE_OFF

       );

      

        CullState cs = display.getRenderer().createCullState();

       cs.setCullFace(CullState.Face.Back);

       scene.setRenderState(cs);

      

       //更新scene用于渲染

       scene.updateGeometricState(0.0f, true);

       scene.updateRenderState();

    }

 

    private void buildFlag() {

       flag = new Flag(tb);

       scene.attachChild(flag);

       flag.placeFlag();

    }

 

    private void buildInput() {

       input = new FlagRushInputHandler(

              player,

              settings.getRenderer()

       );

    }

 

    private void buildChaseCamera() {

       HashMap<String, Object> props = new HashMap<String, Object>();

       props.put(ThirdPersonMouseLook.PROP_MAXROLLOUT, "6");

       props.put(ThirdPersonMouseLook.PROP_MINROLLOUT, "3");

       props.put(

              ThirdPersonMouseLook.PROP_MAXASCENT,

              ""+45*FastMath.DEG_TO_RAD

       );

       props.put(

              ChaseCamera.PROP_INITIALSPHERECOORDS,

              new Vector3f(5,0,30*FastMath.DEG_TO_RAD)

       );

       chaser = new ChaseCamera(cam, player, props);

       chaser.setMaxDistance(8);

       chaser.setMinDistance(2);

    }

 

    private void buildPlayer() {

       Node model = null;

       URL maxFile = Lesson9.class.getClassLoader()

        .getResource("res/bike.jme");

       try {

           model = (Node)BinaryImporter.getInstance().load(

                  maxFile.openStream()

           );

           model.setModelBound(new BoundingBox());

           model.updateModelBound();

           model.setLocalScale(0.0025f);

        } catch (IOException e1) {

           e1.printStackTrace();

       }

 

        //设置Vehicle的属性(这些数字能被认为是单元/ Unit/S

        player = new Vehicle("Player Node",model);

        player.setAcceleration(15);

        player.setBraking(25);

        player.setTurnSpeed(2.5f);

        player.setWeight(25);

        player.setMaxSpeed(25);

        player.setMinSpeed(15);

       

        player.setLocalTranslation(new Vector3f(100,0, 100));

        player.updateWorldBound();

        player.setRenderQueueMode(Renderer.QUEUE_OPAQUE);

       

        scene.attachChild(player);

        scene.updateGeometricState(0, true);

       

        agl = ((BoundingBox)(player.getWorldBound())).yExtent;

    }

 

    private void createSkybox() {

       skybox = new Skybox("skybox",10,10,10);

       Texture north = TextureManager.loadTexture(

              Lesson9.class.getClassLoader()

                  .getResource("res/texture/north.jpg"),

              Texture.MinificationFilter.BilinearNearestMipMap,

              Texture.MagnificationFilter.Bilinear

       );

       Texture south = TextureManager.loadTexture(

              Lesson9.class.getClassLoader()

                  .getResource("res/texture/south.jpg"),

              Texture.MinificationFilter.BilinearNearestMipMap,

              Texture.MagnificationFilter.Bilinear

       );

       Texture east = TextureManager.loadTexture(

              Lesson9.class.getClassLoader()

                  .getResource("res/texture/east.jpg"),

              Texture.MinificationFilter.BilinearNearestMipMap,

              Texture.MagnificationFilter.Bilinear

       );

       Texture west = TextureManager.loadTexture(

              Lesson9.class.getClassLoader()

                  .getResource("res/texture/west.jpg"),

              Texture.MinificationFilter.BilinearNearestMipMap,

              Texture.MagnificationFilter.Bilinear

       );

       Texture up = TextureManager.loadTexture(

              Lesson9.class.getClassLoader()

                  .getResource("res/texture/top.jpg"),

              Texture.MinificationFilter.BilinearNearestMipMap,

              Texture.MagnificationFilter.Bilinear

       );

       Texture down = TextureManager.loadTexture(

              Lesson9.class.getClassLoader()

                  .getResource("res/texture/bottom.jpg"),

              Texture.MinificationFilter.BilinearNearestMipMap,

              Texture.MagnificationFilter.Bilinear

       );

      

       skybox.setTexture(Skybox.Face.North, north);

       skybox.setTexture(Skybox.Face.West, west);

       skybox.setTexture(Skybox.Face.South, south);

       skybox.setTexture(Skybox.Face.East, east);

       skybox.setTexture(Skybox.Face.Up, up);

       skybox.setTexture(Skybox.Face.Down, down);

       skybox.preloadTextures();

      

       scene.attachChild(skybox);

    }

 

    private void buildEnvironment() {

       //这将是所有walls的主要结点

       walls = new Node("bouncing walls");

       scene.attachChild(walls);

      

       fence = new ForceFieldFence("forceFieldFence");

 

       //我们将手工做一些调整去让它更好适应terrain

       //首先我们将实体模型放大

       fence.setLocalScale(5);

       //现在,让我们移动fenceterrain的高度并有一点陷入它里面

       fence.setLocalTranslation(

           new Vector3f(25,tb.getHeight(25,25)+10,25)   

       );

      

       //这里我们将它(fenceattachwalls Node

       walls.attachChild(fence);

    }

 

    private void buildLighting() {

       /* 设置一个基础、默认灯光 */

       DirectionalLight light = new DirectionalLight();

       light.setDiffuse(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));

       light.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f));

       light.setDirection(new Vector3f(1, -1, 0));

       light.setEnabled(true);

      

       LightState lightState = display.getRenderer().createLightState();

       lightState.setEnabled(true);

       lightState.attach(light);

      

       scene.setRenderState(lightState);

    }

 

    /**

     * 创建heightmapterrainBlock

     */

    private void buildTerrain() {

       //生成随机地形数据

       MidPointHeightMap heightMap = new MidPointHeightMap(64,1f);

      

       //缩放数据

       Vector3f terrainScale = new Vector3f(4, .0575f, 4);

      

       //创建一个terrain block

       tb = new TerrainBlock(

              "terrain",

              heightMap.getSize(),

              terrainScale,

              heightMap.getHeightMap(),

              new Vector3f(0, 0, 0)

       );

       tb.setModelBound(new BoundingBox());

       tb.updateModelBound();

      

       //通过三个纹理生成地形纹理

       ProceduralTextureGenerator pt =

           new ProceduralTextureGenerator(heightMap);

      

       pt.addTexture(

              new ImageIcon(

                     getClass().getClassLoader()

                         .getResource("res/grassb.png")

              ),

              -128, 0, 128

       );

       pt.addTexture(

              new ImageIcon(

                     getClass().getClassLoader()

                         .getResource("res/dirt.jpg")

              ),

              0, 128, 256

       );

       pt.addTexture(

              new ImageIcon(

                     getClass().getClassLoader()

                         .getResource("res/highest.jpg")

              ),

              128, 256, 384

       );

       pt.createTexture(32);

      

       //将纹理赋予地形

       ts = display.getRenderer().createTextureState();

       Texture t1 = TextureManager.loadTexture(

              pt.getImageIcon().getImage(),

              Texture.MinificationFilter.Trilinear,

              Texture.MagnificationFilter.Bilinear,

              true

       );

       ts.setTexture(t1, 0);

      

      

       //加载细节纹理并为2terraintexture设置组合模型

        Texture t2 = TextureManager.loadTexture(

                Lesson9.class.getClassLoader()

                  .getResource("res/Detail.jpg"),

                Texture.MinificationFilter.Trilinear,

                Texture.MagnificationFilter.Bilinear

        );

        ts.setTexture(t2, 1);

       

        t2.setWrap(Texture.WrapMode.Repeat);

 

        t1.setApply(Texture.ApplyMode.Combine);

        t1.setCombineFuncRGB(Texture.CombinerFunctionRGB.Modulate);

        t1.setCombineSrc0RGB(Texture.CombinerSource.CurrentTexture);

        t1.setCombineOp0RGB(Texture.CombinerOperandRGB.SourceColor);

        t1.setCombineSrc1RGB(Texture.CombinerSource.PrimaryColor);

        t1.setCombineOp1RGB(Texture.CombinerOperandRGB.SourceColor);

 

        t2.setApply(Texture.ApplyMode.Combine);

        t2.setCombineFuncRGB(Texture.CombinerFunctionRGB.AddSigned);

        t2.setCombineSrc0RGB(Texture.CombinerSource.CurrentTexture);

        t2.setCombineOp0RGB(Texture.CombinerOperandRGB.SourceColor);

        t2.setCombineSrc1RGB(Texture.CombinerSource.Previous);

        t2.setCombineOp1RGB(Texture.CombinerOperandRGB.SourceColor);

 

 

       tb.setRenderState(ts);

       tb.setDetailTexture(1, 16);

 

        tb.setRenderQueueMode(Renderer.QUEUE_OPAQUE);

       scene.attachChild(tb);

    }

 

    protected void initSystem() {

       //保存属性信息

       width      = settings.getWidth();

       height     = settings.getHeight();

       depth      = settings.getDepth();

       freq       = settings.getFrequency();

       fullscreen    = settings.isFullscreen();

      

       try{

           display = DisplaySystem.getDisplaySystem(

                  settings.getRenderer()

           );

           display.createWindow(

                  width, height, depth, freq, fullscreen

           );

           cam = display.getRenderer().createCamera(width, height);

       }catch(JmeException e){

           e.printStackTrace();

           System.exit(-1);

       }

      

       //设置背景为黑色

       display.getRenderer().setBackgroundColor(ColorRGBA.black);

      

       //初始化摄像机

       cam.setFrustumPerspective(

              45.0f,

              (float)width/(float)height,

              1f,

              5000f

       );

       Vector3f loc = new Vector3f(200f,1000f,200f);

       cam.setLocation(loc);

//     Vector3f left = new Vector3f(-0.5f,0.0f,0.5f);

//     Vector3f up = new Vector3f(0.0f,1.0f,0.0f);

//     Vector3f dir = new Vector3f(-0.5f,0.0f,-0.5f);

//     //将摄像机移到正确位置和方向

//     cam.setFrame(loc, left, up, dir);

      

       //我们改变自己的摄像机位置和视锥的标志

       cam.update();

      

       //获取一个高分辨率用于FPS更新

       timer = Timer.getTimer();

      

       display.getRenderer().setCamera(cam);

       KeyBindingManager.getKeyBindingManager().set(

              "exit",

              KeyInput.KEY_ESCAPE

       );

    }

 

    /*

     * 如果分辨率改变将被调用

     */

    protected void reinit() {

       display.recreateWindow(width, height, depth, freq, fullscreen);

    }

 

    /*

     * 绘制场景图

     */

    protected void render(float interpolation) {

       //清除屏幕

       display.getRenderer().clearBuffers();

       display.getRenderer().draw(scene);

    }

 

    /*

     * update期间,我们只需寻找Escape按钮

     * 并更新timer去获取帧率

     */

    protected void update(float interpolation) {

       //更新timer去获取帧率

       timer.update();

       interpolation = timer.getTimePerFrame();

      

       input.update(interpolation);

       chaser.update(interpolation);

       flag.update(interpolation);

       fence.update(interpolation);

      

       bounce.processCollisions();

      

       //我们想让skybox一直在我们的视野内,所以让它和camera一起移动

       skybox.setLocalTranslation(cam.getLocation());

      

       //我们不想chase camera走到世界下面,因此让它一直在水平面上2个单元。

       if(cam.getLocation().y < (tb.getHeight(cam.getLocation())+2)) {

           cam.getLocation().y = tb.getHeight(cam.getLocation()) + 2;

           cam.update();

       }

      

       //确保当玩家离开平面时我们不会坠落。

       //当我们增加冲突时,fence将做它自己的工作并保持玩家在里面。

       float characterMinHeight =

           tb.getHeight(player.getLocalTranslation()) + agl;

       if(

              !Float.isInfinite(characterMinHeight) &&

              !Float.isNaN(characterMinHeight)

       )

           player.getLocalTranslation().y = characterMinHeight;

      

      

       //获取terrain在我们当前位置的normal

       //我们然后将它应用到player上面。

       tb.getSurfaceNormal(

              player.getLocalTranslation(),

              normal

       );

       if(normal != null) {

          player.rotateUpTo(normal);

       }

 

      

       //Escape被按下时,我们退出游戏

       if(KeyBindingManager.getKeyBindingManager()

              .isValidCommand("exit")

       ){

           finished = true;

       }

      

       //由于我们改变了场景(移动skybox),我们需要更新scene graph

       scene.updateGeometricState(interpolation, true);

    }

}

9.6CollisionDetection.java

import com.jme.bounding.BoundingBox;

import com.jme.image.Texture;

import com.jme.math.FastMath;

import com.jme.math.Vector3f;

import com.jme.renderer.ColorRGBA;

import com.jme.scene.Controller;

import com.jme.scene.Node;

import com.jme.scene.Spatial;

import com.jme.scene.state.BlendState;

import com.jme.scene.state.RenderState;

import com.jme.scene.state.TextureState;

import com.jme.system.DisplaySystem;

import com.jme.util.TextureManager;

import com.jmex.effects.particles.ParticleFactory;

import com.jmex.effects.particles.ParticleSystem;

import com.jmex.effects.particles.SimpleParticleInfluenceFactory;

 

public class CollisionDetection {

   

    private Vehicle player;

    private Node target;

    private int nextMove;

    private float zExtent;

   

    public static final int MOVE_BOUNCE_OFF = 0;

    public static final int MOVE_STOP = 1;

   

    private ParticleSystem particleGeom;

 

    public CollisionDetection(

           Vehicle player, Node target, int nextMove){

       this.player = player;

       this.target = target;

       this.nextMove = nextMove;

       this.zExtent = ((BoundingBox)(player.getWorldBound())).zExtent;

       createParticles();

    }

   

    public CollisionDetection(

           Vehicle player, Spatial target, int nextMove){

       this.player = player;

       this.target = (Node) target;

       this.nextMove = nextMove;

       this.zExtent = ((BoundingBox)(player.getWorldBound())).zExtent;

       createParticles();

    }

   

    //这里处理你所有的冲突

    public void processCollisions(){

       if(nextMove == MOVE_STOP){

           if(player.hasCollision(target,false)){

              if(

                     player.getFrontwheel()

                         .hasCollision(target, false)

              ){

                  player.setMaxSpeed(0);

                  player.setMinSpeed(15);

              }else if(

                     player.getBackwheel()

                         .hasCollision(target, false)

              ){

                  player.setMaxSpeed(25);

                  player.setMinSpeed(0);

              }

           }else{

              player.setMaxSpeed(25);

              player.setMinSpeed(15);

           }

       }else if(nextMove == MOVE_BOUNCE_OFF){

           if(player.hasCollision(target,false))

              if(

                     player.getFrontwheel()

                     .hasCollision(target, false)

              ){

                  player.setVelocity(

                         -Math.abs(player.getVelocity())

                  );

                  particleGeom.setLocalTranslation(

                         player.getFrontwheel()

                            .getLocalTranslation().x,

                         player.getFrontwheel()

                            .getLocalTranslation().y*0.0025f,

                         zExtent

                  );

                  particleGeom.forceRespawn();

              }else if(

                     player.getBackwheel()

                     .hasCollision(target, false)

              ){

                  player.setVelocity(

                         Math.abs(player.getVelocity())

                  );

                  particleGeom.setLocalTranslation(

                         player.getFrontwheel()

                            .getLocalTranslation().x,

                         player.getFrontwheel()

                            .getLocalTranslation().y*0.0025f,

                         -zExtent

                  );

                  particleGeom.forceRespawn();

              }

       }

    }

 

    private void createParticles() {

       particleGeom = ParticleFactory.buildParticles("collsion", 300);

    particleGeom.addInfluence(

            SimpleParticleInfluenceFactory

               .createBasicGravity(

                      new Vector3f(0, -0.01f, 0), true

               )

    );

    particleGeom.setEmissionDirection(

            new Vector3f(0.0f, 1.0f, 0.0f)

    );

    particleGeom.setMaximumAngle(360f * FastMath.DEG_TO_RAD);

    particleGeom.setMinimumLifeTime(2000.0f);

    particleGeom.setMaximumLifeTime(4000.0f);

    particleGeom.setInitialVelocity(.004f);

    particleGeom.recreate(FastMath.nextRandomInt(6, 20));

    particleGeom.setStartSize(.05f);

    particleGeom.setEndSize(.02f);

    particleGeom.setStartColor(

            new ColorRGBA(1.0f, 0.796875f, 0.1992f, 1.0f)

    );

    particleGeom.setEndColor(

            new ColorRGBA(1.0f, 1.0f, 0.5976f, 1.0f)

    );

    particleGeom.getParticleController().setRepeatType(

            Controller.RT_CLAMP

    );

 

    BlendState as = (BlendState) particleGeom.getRenderState(

                      RenderState.StateType.Blend

                   );

    if (as == null) {

        as = DisplaySystem.getDisplaySystem().getRenderer()

             .createBlendState();

        as.setBlendEnabled(true);

        as.setSourceFunction(BlendState.SourceFunction.SourceAlpha);

        as.setTestEnabled(true);

        as.setTestFunction(BlendState.TestFunction.GreaterThan);

        particleGeom.setRenderState(as);

        particleGeom.updateRenderState();

    }

    as.setDestinationFunction(BlendState.DestinationFunction.One);

    TextureState ts = DisplaySystem.getDisplaySystem()

        .getRenderer().createTextureState();

    ts.setTexture(

            TextureManager.loadTexture(

                   CollisionDetection.class.getClassLoader()

                      .getResource(

                          "res/flaresmall.jpg"

                   ),

                   Texture.MinificationFilter.BilinearNearestMipMap,

                   Texture.MagnificationFilter.Bilinear

            )

    );

    particleGeom.setRenderState(ts);

 

    player.attachChild(particleGeom);

    particleGeom.updateRenderState();

    }

}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值