注:本系列教程全部翻译完之后可能会以PDF的形式发布。
如果有什么错误可以到http://blog.csdn.net/kakashi8841留言或EMAIL:kakashi9bi@gmail.com给我。
jME版本 :jME_2.0.1_Stable
开发工具:MyEclipse8.5
开发环境:Window7/Vista
在这个向导中,我们将通过冲突检测(Collision Detection,也叫碰撞检测,碰撞检查,冲突检查等)保证我们呆在一个fence。我将通过为Collision Detection增加一个新的接口来实现。这种方式,我们有几种不同的Collision Detection系统。像反弹(bounce)墙和冲撞(crash)墙。
而最后,我不是会巫术的猴子(mojomonkey),所以这将和其它向导不同。
9.2、冲突(Collision)接口
首先,我们增加一个接口去知道我们需要注意哪里。所以我们需要知道player和我们寻找的目标。而Collision Detector需要知道下一步发生什么。我们也准备必要的例子用于产生更好的效果。
我们先创建一个类:CollisionDetection,里面有2个方法:
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();
}
nextMove现在只有2种类型:
public static final int MOVE_BOUNCE_OFF = 0;
public static final int MOVE_STOP = 1;
所以你现在可以输入这个如果你想要vehicle撞在fence后反弹的话:
增加一个类变量:
private CollisionDetection bounce;
在initGame(buildPlayer和buildEnvironment方法后)中加入:
bounce = new CollisionDetection(
player, walls, CollisionDetection. MOVE_BOUNCE_OFF
);
你现在看到那里有个node(walls)还没有被定义。我将在之后再做。因为你将一个新的bounce墙放入游戏中所需要做的就是,将墙(fence)attach到Node walls。
增加一个类变量:
private Node walls;
然后像这样:
private void buildEnvironment() {
//这将是所有walls的主要结点
walls = new Node("bouncing walls");
scene.attachChild(walls);
fence = new ForceFieldFence("forceFieldFence");
//我们将手工做一些调整去让它更好适应terrain
//首先我们将实体“模型”放大
fence.setLocalScale(5);
//现在,让我们移动fence到terrain的高度并有一点陷入它里面
fence.setLocalTranslation(
new Vector3f(25,tb.getHeight(25,25)+10,25)
);
//这里我们将它(fence)attach到walls Node
walls.attachChild(fence);
}
9.3、墙壁Detection System
这是我创建的一个小的detection system。它在游戏中将vehicle弹回。而另一个detection直接停止vehicle。
//这里处理你所有的冲突
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();
}
}
}
上面的particleGeom为类变量:
private ParticleSystem particleGeom;
为了有一个好的冲突,你需要火花。我们将使用ParticleSystem创建这些。我尝试几次得出这些数字让它看起来更好。
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();
}
分享到:
相关推荐
FlagRushSeries中文翻译(1-10) ...FlagRush9——墙壁检测系统(Detection System) FlagRush10——墙壁检测系统(Detection System) 如果确实没分下载,到我博客看看吧,这些文章那里都有。有分的就支持一下我咯。 ...
2.2、InitSystem 9 2.3、InitGame 13 2.4、Render和update 14 2.5、reinit和cleanup 15 2.6、总结 16 2.7、源码 16 3、 加载地形 20 3.1、创建一个heightmap 21 3.2、生成Terrain Mesh 21 3.3、生成Texture 22 3.4、...
jmonkeyengine是一个面向冒险的java开发人员的三维游戏引擎。它是开源的、跨平台的、尖端的。3.2.4是jmonkeyengine 3 sdk的最新稳定版本,是一个完整的游戏开发套件。我们将发布3.2.x更新,直到主要的3.3版本到来。
Jmonkeyengine 3.0 Beginners Guide
jMonkeyEngine(jME)是一个开源的Java 3D引擎,它的效果非常好,速度也不错
NULL 博文链接:https://hjk685.iteye.com/blog/1912303
jMonkeyEngine 3.0 Beginner's Guide
sdk, 在netbeans上,基于 jMonkeyEngine3 SDK jMonkeyEngine软件开发工具包( SDK ) 欢迎使用jMonkeyEngine软件开发工具包( SDK )的知识库。 这个SDK使用jMonkeyEngine简化了 3D 个应用程序的开发。 它包含开发完整...
共12章JME中文教程,演示代码经测试无错
jmonkeyengine, 在Java中,一个完整的3D 游戏开发 jMonkeyEngine jMonkeyEngine是一款用于冒险Java开发人员的3D 游戏引擎。 它是开放的,跨平台的,和cutting的。 3.2.0是 jMonkeyEngine 3 SDK的最新稳定版本,一个...
jmonkeyengine:纯Java编写的一个完整的3D游戏开发套件
NULL 博文链接:https://snake.iteye.com/blog/578982
JME学习文档 中文版 jMonkeyEngine 入门学习
jMonkeyEngine软件开发套件(SDK) 欢迎使用jMonkeyEngine软件开发套件(SDK)的存储库。 该SDK简化了使用jMonkeyEngine开发3D应用程序的过程。 它包含开发完整应用程序所需的一切。 我们的SDK(或jmonkeyplatform...
jMonkeyEngine是一个众所周知且广泛使用的免费开放源代码游戏引擎。 该演示应用程序展示了最新的jME版本jME 3的功能和某些功能。jMonkeyEngine使用OpenGL作为其图形库。 该软件包含的一些功能包括:粒子效果,水渲染...
赛车游戏 使用 JMonkeyEngine 创建的赛车游戏。 汽车的转向非常僵硬。 按住 F 将车轮摩擦设置为 0 并改为漂移。
Voider 该游戏正在创作中。 这样做的基本思想是创建一个刺客信条Unity副本,但差别很小。 它是用Java编写并在Blender中设计的。 目前,登录,加载屏幕,游戏gui和游戏开始部分完成。 主要类别尚未连接,因此您必须...