搜索
首页数据库mysql教程jMonkeyEngine译文 FlagRush9(2)墙壁检测系统(Detection Sy

9.5 、 Lesson9.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.inp

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;

 

publicclass Lesson9 extends BaseGame{

 

    privateintwidth,height;

    privateintfreq,depth;

    privatebooleanfullscreen;

   

    //我们的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();

    privatefloatagl;

    private Flag flag;

   

    private CollisionDetection bounce;

    private Node walls;

   

    publicstaticvoid 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

     */

    protectedvoid cleanup() {

       ts.deleteAll();

    }

 

    protectedvoid 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();

    }

 

    privatevoid buildFlag() {

       flag = new Flag(tb);

       scene.attachChild(flag);

       flag.placeFlag();

    }

 

    privatevoid buildInput() {

       input = new FlagRushInputHandler(

              player,

              settings.getRenderer()

       );

    }

 

    privatevoid buildChaseCamera() {

       HashMap props = new HashMap();

       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);

    }

 

    privatevoid 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;

    }

 

    privatevoid 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);

    }

 

    privatevoid 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);

    }

 

    privatevoid 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

     */

    privatevoid 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);

    }

 

    protectedvoid 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

       );

    }

 

    /*

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

     */

    protectedvoid reinit() {

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

    }

 

    /*

     * 绘制场景图

     */

    protectedvoid render(float interpolation) {

       //清除屏幕

       display.getRenderer().clearBuffers();

       display.getRenderer().draw(scene);

    }

 

    /*

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

     * 并更新timer去获取帧率

     */

    protectedvoid 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;

 

publicclass CollisionDetection {

   

    private Vehicle player;

    private Node target;

    privateintnextMove;

    privatefloatzExtent;

   

    publicstaticfinalintMOVE_BOUNCE_OFF = 0;

    publicstaticfinalintMOVE_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 play

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
如何使用Alter Table语句在MySQL中更改表?如何使用Alter Table语句在MySQL中更改表?Mar 19, 2025 pm 03:51 PM

本文讨论了使用MySQL的Alter Table语句修改表,包括添加/删除列,重命名表/列以及更改列数据类型。

如何为MySQL连接配置SSL/TLS加密?如何为MySQL连接配置SSL/TLS加密?Mar 18, 2025 pm 12:01 PM

文章讨论了为MySQL配置SSL/TLS加密,包括证书生成和验证。主要问题是使用自签名证书的安全含义。[角色计数:159]

您如何处理MySQL中的大型数据集?您如何处理MySQL中的大型数据集?Mar 21, 2025 pm 12:15 PM

文章讨论了处理MySQL中大型数据集的策略,包括分区,碎片,索引和查询优化。

哪些流行的MySQL GUI工具(例如MySQL Workbench,PhpMyAdmin)是什么?哪些流行的MySQL GUI工具(例如MySQL Workbench,PhpMyAdmin)是什么?Mar 21, 2025 pm 06:28 PM

文章讨论了流行的MySQL GUI工具,例如MySQL Workbench和PhpMyAdmin,比较了它们对初学者和高级用户的功能和适合性。[159个字符]

如何使用Drop Table语句将表放入MySQL中?如何使用Drop Table语句将表放入MySQL中?Mar 19, 2025 pm 03:52 PM

本文讨论了使用Drop Table语句在MySQL中放下表,并强调了预防措施和风险。它强调,没有备份,该动作是不可逆转的,详细介绍了恢复方法和潜在的生产环境危害。

您如何用外国钥匙代表关系?您如何用外国钥匙代表关系?Mar 19, 2025 pm 03:48 PM

文章讨论了使用外国密钥来代表数据库中的关系,重点是最佳实践,数据完整性和避免的常见陷阱。

如何在JSON列上创建索引?如何在JSON列上创建索引?Mar 21, 2025 pm 12:13 PM

本文讨论了在PostgreSQL,MySQL和MongoDB等各个数据库中的JSON列上创建索引,以增强查询性能。它解释了索引特定的JSON路径的语法和好处,并列出了支持的数据库系统。

如何保护MySQL免受常见漏洞(SQL注入,蛮力攻击)?如何保护MySQL免受常见漏洞(SQL注入,蛮力攻击)?Mar 18, 2025 pm 12:00 PM

文章讨论了使用准备好的语句,输入验证和强密码策略确保针对SQL注入和蛮力攻击的MySQL。(159个字符)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境