Java Jmonkey碰撞检测
我对Jmonkey编程不熟悉,我想问一个关于碰撞交互的问题,因为我的代码似乎发现了可能来自地形的碰撞,我不知道如何解决这个问题。我的目标是,如果玩家与敌人的鬼魂控件发生碰撞,并将消息显示为输出,那么他将成为第一个被检测到的人。我的代码显示持续冲突,然后它崩溃Java Jmonkey碰撞检测,java,collision,jmonkeyengine,Java,Collision,Jmonkeyengine,我对Jmonkey编程不熟悉,我想问一个关于碰撞交互的问题,因为我的代码似乎发现了可能来自地形的碰撞,我不知道如何解决这个问题。我的目标是,如果玩家与敌人的鬼魂控件发生碰撞,并将消息显示为输出,那么他将成为第一个被检测到的人。我的代码显示持续冲突,然后它崩溃 package test; //imports... public class test extends SimpleApplication implements ActionListener,PhysicsTickListener{
package test;
//imports...
public class test extends SimpleApplication
implements ActionListener,PhysicsTickListener{
private MotionPath path;
private MotionPath path2;
private MotionTrack motionTrack;
private MotionTrack motionTrack2;
private AnimChannel channel2;
private AnimControl control2;
private AnimControl control3;
private AnimChannel channel3;
private BulletAppState bulletAppState;
private RigidBodyControl landscape;
private CharacterControl player;
private Vector3f walkDirection = new Vector3f();
private boolean left = false, right = false, up = false, down = false;
private TerrainQuad terrain;
private Material mat_terrain;
private GhostControl ghost;
static test app;
Material matMarker;
public static void main(String[] args) {
app = new test();
app.start();
}
float displacement=60;
int score = 0;
int robotHealth=0;
Geometry mark;
Node shootables;
Node pickUpObject1;
BitmapText hudText;
@Override
public void simpleInitApp() {
createScene();
enemies();
pickUptype1();
initCrossHairs(); // a "+" in the middle of the screen to help aiming
initKeys(); // load custom key mappings
initMark(); // a red sphere to mark the hit
hudText = new BitmapText(guiFont, false);
hudText.setSize(guiFont.getCharSet().getRenderedSize()); // font size
hudText.setColor(ColorRGBA.Red); // font color
hudText.setLocalTranslation(600, 700, 0); // position
guiNode.attachChild(hudText);
DirectionalLight sun2 = new DirectionalLight();
sun2.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));
int width = settings.getWidth(); //width is the width of the gui
int height = settings.getHeight(); //height is the height of the gui
}
protected Geometry makeCube(String name, float x, float y, float z) {
Box box = new Box(new Vector3f(x, y, z), 3f, 3f, 3f);
Geometry cube = new Geometry(name, box);
Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
Texture tex_ml = assetManager.loadTexture("Interface/Logo/Monkey.jpg");
mat1.setTexture("ColorMap", tex_ml);
cube.setMaterial(mat1);
return cube;
}
private PhysicsSpace getPhysicsSpace() {
return bulletAppState.getPhysicsSpace();
}
/**
* This is the main event loop--walking happens here.
* We check in which direction the player is walking by interpreting
* the camera direction forward (camDir) and to the side (camLeft).
* The setWalkDirection() command is what lets a physics-controlled player walk.
* We also make sure here that the camera moves with player.
*/
@Override
public void simpleUpdate(float tpf) {
hudText.setText("SCORE \n" + " " + score);// the text
Vector3f camDir = cam.getDirection().clone().multLocal(0.6f);
Vector3f camLeft = cam.getLeft().clone().multLocal(0.4f);
walkDirection.set(0, 0, 0);
if (left) { walkDirection.addLocal(camLeft); }
if (right) { walkDirection.addLocal(camLeft.negate()); }
if (up) { walkDirection.addLocal(camDir); }
if (down) { walkDirection.addLocal(camDir.negate()); }
player.setWalkDirection(walkDirection);
cam.setLocation(player.getPhysicsLocation());
path.setCycle(true); // Make path a complete circuit
path2.setCycle(true);
motionTrack.setLoopMode(LoopMode.Loop);
motionTrack2.setLoopMode(LoopMode.Loop);
}
public Node robot(){
Node monster = (Node) assetManager.loadModel("Models/Oto/Oto.mesh.xml");
monster.scale(1.5f, 1.5f, 1.5f);
monster.rotate(0.0f, -3.0f, 0.0f);
// Create a appropriate physical shape for it
return monster;
}
public void createScene(){
/** Set up Physics */
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
//bulletAppState.getPhysicsSpace().enableDebug(assetManager);
flyCam.setMoveSpeed(100);
setUpKeys();
terrain = new TerrainQuad("my terrain", 65, 513, heightmap.getHeightMap());
/** 6. Add physics: */
// We set up collision detection for the scene by creating a
// compound collision shape and a static RigidBodyControl with mass zero.*/
CollisionShape terrainShape =
CollisionShapeFactory.createMeshShape((Node) terrain);
landscape = new RigidBodyControl(terrainShape, 0);
terrain.addControl(landscape);
CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
player = new CharacterControl(capsuleShape, 0.05f);
player.setJumpSpeed(20);
player.setFallSpeed(30);
player.setGravity(30);
player.setPhysicsLocation(new Vector3f(145f, -28f, 10f));
player.setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_01);
player.addCollideWithGroup(PhysicsCollisionObject.COLLISION_GROUP_01);
setUpLight();
rootNode.attachChild(SkyFactory.createSky( assetManager,
"Textures/Sky/Bright/BrightSky.dds", false));
}
public void enemies(){
shootables = new Node("Shootables");
rootNode.attachChild(shootables);
Node Robot1 = robot();
Node Robot2 = robot();
CapsuleCollisionShape capsule = new CapsuleCollisionShape(4f, 10f);
RigidBodyControl robot1Cap = new RigidBodyControl(capsule, 0.01f);
Robot1.addControl(robot1Cap);
getPhysicsSpace().add(robot1Cap);
bulletAppState.getPhysicsSpace().add(robot1Cap);
bulletAppState.getPhysicsSpace().enableDebug(assetManager);
robot1Cap.setMass(100f);
robot1Cap.setKinematic(true);
CapsuleCollisionShape capsule2 = new CapsuleCollisionShape(4f, 10f);
RigidBodyControl robot2Cap = new RigidBodyControl(capsule, 0.01f);
Robot2.addControl(robot2Cap);
getPhysicsSpace().add(robot2Cap);
bulletAppState.getPhysicsSpace().add(robot2Cap);
bulletAppState.getPhysicsSpace().enableDebug(assetManager);
robot2Cap.setMass(100f);
robot2Cap.setKinematic(true);
ghost = new GhostControl(
new BoxCollisionShape(new Vector3f(8f,8f,8f))); // a box-shaped ghost
Robot1.addControl(ghost);
ghost.setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_01);
ghost.setCollideWithGroups(PhysicsCollisionObject.COLLISION_GROUP_01);
getPhysicsSpace().add(ghost);
getPhysicsSpace().addTickListener(this);
control2 = Robot1.getControl(AnimControl.class);
channel2 = control2.createChannel();
channel2.setAnim("Walk");
control3 = Robot2.getControl(AnimControl.class);
channel3 = control3.createChannel();
channel3.setAnim("Walk");
path = new MotionPath();
path.addWayPoint(new Vector3f(500f,-83f,3f));
path.addWayPoint(new Vector3f(350f,-79f, 3f));
path.enableDebugShape(assetManager,rootNode);
// Initialize our motionTrack object
motionTrack = new MotionTrack(Robot1, path);
motionTrack.setDirectionType(MotionTrack.Direction.Path);
// Enable the motionTrack
motionTrack.setEnabled(true);
path2 = new MotionPath();
path2.addWayPoint(new Vector3f(180f,-50f,-100f));
path2.addWayPoint(new Vector3f(200f, -55f, -30f));
path2.enableDebugShape(assetManager,rootNode);
// Initialize our motionTrack object
motionTrack2 = new MotionTrack(Robot2, path2);
motionTrack2.setDirectionType(MotionTrack.Direction.Path);
// Enable the motionTrack
motionTrack2.setEnabled(true);
shootables.attachChild(Robot1);
shootables.attachChild(Robot2);
}
public void physicsTick(PhysicsSpace space, float f) {
if (ghost.getOverlappingObjects().size() > 0) {
final Vector3f bPoint = ghost.getPhysicsLocation();
try {
app.enqueue(new Callable<Boolean>() {
public Boolean call() throws Exception {
app.addMarker(bPoint);
return true;
}
});
} catch (Exception ex) {
}
}
}
public void pickUptype1(){
pickUpObject1 = new Node("pickUpObject1");
rootNode.attachChild(pickUpObject1);
Node cube1 = new Node();
cube1.attachChild(makeCube("the Deputy", 220f, -63f, -150f));
Node cube2 = new Node();
cube2.attachChild(makeCube("the Deputy2", 410f, -89f, -270f));
RigidBodyControl floor_phy = new RigidBodyControl(0.0f);
cube1.addControl(floor_phy);
RigidBodyControl floor_phy2 = new RigidBodyControl(0.0f);
cube2.addControl(floor_phy2);
bulletAppState.getPhysicsSpace().add(floor_phy);
bulletAppState.getPhysicsSpace().add(floor_phy2);
pickUpObject1.attachChild(cube1);
pickUpObject1.attachChild(cube2);
}
}
封装测试;
//进口。。。
公共类测试扩展了SimpleApplication
实现ActionListener、PhysicsTickListener{
私有运动路径;
私有运动路径路径2;
私人运动轨迹;
私人MotionTrack MotionTrack 2;
私人频道2;
私人控制2;
私人控制3;
私人频道3;
私有BulletAppState BulletAppState;
私人刚体控制景观;
私人角色控制玩家;
私有向量3f walkDirection=新向量3f();
私有布尔左=假,右=假,上=假,下=假;
私人地形四重地形;
私人材料材料材料;
私人幽灵控制幽灵;
静态测试应用程序;
材料标记;
公共静态void main(字符串[]args){
app=新测试();
app.start();
}
浮动位移=60;
智力得分=0;
int-robotHealth=0;
几何标志;
节点射击;
节点拾取对象1;
位图文本;
@凌驾
public void simpleInitApp(){
createScene();
敌人();
pickUptype1();
在屏幕中部帮助目标
initKeys();//加载自定义键映射
initMark();//标记命中的红色球体
hudText=新的位图文本(guiFont,false);
hudText.setSize(guiFont.getCharSet().getrenderdSize());//字体大小
hudText.setColor(ColorRGBA.Red);//字体颜色
hudText.setLocalTranslation(600700,0);//位置
guiNode.attachChild(文本);
DirectionalLight sun2=新的DirectionalLight();
sun2.设置方向(新矢量3F(-0.1f,-0.7f,-1.0f));
int width=settings.getWidth();//宽度是gui的宽度
int height=settings.getHeight();//height是gui的高度
}
受保护的几何体makeCube(字符串名称、浮点x、浮点y、浮点z){
长方体=新长方体(新矢量3f(x,y,z),3f,3f,3f);
几何体立方体=新几何体(名称、方框);
材料材料1=新材料(资产管理人,“通用/材料定义/杂项/无阴影.j3md”);
Texture tex_ml=assetManager.loadTexture(“Interface/Logo/Monkey.jpg”);
mat1.setTexture(“彩色贴图”,tex_ml);
立方体材料(mat1);
返回立方体;
}
私有物理空间getPhysicsSpace(){
返回bulletAppState.getPhysicsSpace();
}
/**
*这是主要的事件循环——行走发生在这里。
*我们通过解释来检查玩家朝哪个方向走
*摄像机方向向前(camDir)和向侧面(camLeft)。
*setWalkDirection()命令允许物理控制的玩家行走。
*在这里,我们也要确保相机和播放器一起移动。
*/
@凌驾
公共无效simpleUpdate(浮动tpf){
hudText.setText(“SCORE\n”+“”+SCORE);//文本
向量3f camDir=cam.getDirection().clone().multLocal(0.6f);
Vector3f camLeft=cam.getLeft().clone().multLocal(0.4f);
设置(0,0,0);
if(左){walkDirection.addLocal(camleet);}
if(right){walkDirection.addLocal(camlefit.negate());}
if(up){walkDirection.addLocal(camDir);}
if(down){walkDirection.addLocal(camDir.negate());}
player.setWalkDirection(walkDirection);
setLocation(player.getPhysicsLocation());
path.setCycle(true);//使路径成为完整的电路
路径2.设置周期(真);
motionTrack.setLoopMode(LoopMode.Loop);
motionTrack2.setLoopMode(LoopMode.Loop);
}
公共节点机器人(){
Node monster=(Node)assetManager.loadModel(“Models/Oto/Oto.mesh.xml”);
怪物比例(1.5f,1.5f,1.5f);
怪物。旋转(0.0f,-3.0f,0.0f);
//为其创建适当的物理形状
返回怪物;
}
公共场景(){
/**建立物理学*/
bulletAppState=新bulletAppState();
stateManager.attach(bulletAppState);
//bulletAppState.getPhysicsSpace().enableDebug(assetManager);
flyCam.设置移动速度(100);
设置键();
terrain=new TerrainQuad(“我的地形”,65513,heightmap.getHeightMap());
/**6.添加物理:*/
//我们通过创建一个
//复合碰撞形状和质量为零的静态刚体控制*/
碰撞状地形=
CollisionShapeFactory.createMeshShape((节点)地形);
景观=新的刚体控制(地形,0);
地形控制(景观);
囊泡碰撞形状囊泡形状=新囊泡碰撞形状(1.5f,6f,1);
player=新角色控件(capsuleShape,0.05f);
玩家设置跳跃速度(20);
player.setFallSpeed(30);
运动员。设置重力(30);
player.setPhysicsLocation(新矢量3F(145f,-28f,10f));
player.setCollisionGroup(physicCollisionObject.COLLISION\u GROUP\u 01);
player.addCollizeWithGroup(物理集合对象碰撞组01);
setUpLight();
rootNode.attachChild(SkyFactory.createSky)(assetManager,
“纹理/Sky/Bright/BrightSky.dds”,假);
}
公营部门(){
shootables=新节点(“shootables”);
rootNode.attachChild(shootables);
节点Robot1=robot();
节点Robot2=robot();
胶囊碰撞形状胶囊=新胶囊碰撞H
public Boolean call() throws Exception
/*
* adds an enemy and returns a reference to that enemy
*/
Spatial addEnemy()
{
//Characters are spatials typically in JMonkey
Spatial enemy=new Spatial();
CapsuleCollisionShape collisionShape=new CapsuleCollisionShape(4.0f, 10.0f);
CharacterControl characterControl = new CharacterControl(collisionShape, stepHeight);
enemy.addControl(characterControl);
getPhysicsSpaceState(characterControl);
shootables.attachChild(enemy);
}
CharacterControl
//to send keyboard input to the character controller to move it
KeyboardControl
GunControl
CharacterControl
//does path planning to get a route and steers character control along it
RouteController
GunControl
//AI to determine where/if I want to walk, what to shoot at, where to aim
BehaviourControl