Hello,
Been stuck on this for the past couple days. I have a Node with a special model attached to it. I also have obstacle nodes containing box obstacles. I need to listen for collisions between the two. I followed the CollisionListenerTest and the jme tutorial. I played around with the CollisionListenerTest to make it look more like my game structure and it worked perfect. However my game does not seem to recognize the collisions. I receive no collision events in my game.
My player is moved programmatically and does not rely on physics for movement. The obstacles are static and cannot move.
[java]
//Load State
@Override
public void initialize(AppStateManager stateManager, Application app) {
this.app = (Main) app;
this.app.getStateManager().attach(bulletAppState);
this.rootNode = this.app.getRootNode();
this.assetManager = this.app.getAssetManager();
this.inputManager = this.app.getInputManager();
this.app.getStateManager().attach(this.app.inGameState);
bulletAppState = new BulletAppState();
bind(nifty, nifty.getCurrentScreen());
initWorldLighting();
app.getViewPort().addProcessor(fpp);
buildTunnel();
buildPlayer();
loadObstacles();
rootNode.addLight(sun);
rootNode.attachChild(obstacles);
rootNode.attachChild(tunnel);
rootNode.attachChild(player);
getPhysicsSpace().addCollisionListener(this.app.inGameState);
bulletAppState.getPhysicsSpace().enableDebug(app.getAssetManager());
nifty.gotoScreen("hud");
}
private void loadObstacles() {
obstacles = new Node("obstacles");
obstacleList = app.gameUpdater.getObstacles();
Vector3f vec = new Vector3f();
for (Obstacle o : obstacleList) {
Box b = new Box(o.getWidth(), o.getHeight(), o.getWidth());
vec.x = o.getPosition().x;
vec.y = o.getPosition().y;
vec.z = o.getPosition().z;
Geometry box = new Geometry("obstacle", b);
box.setLocalTranslation(vec);
Matrix4f mat = new Matrix4f(-o.getRight().x, -o.getRight().y, -o.getRight().z, 0.0f, //LEFT
o.getUp().x, o.getUp().y, o.getUp().z, 0.0f, //UP
o.getForward().x, o.getForward().y, o.getForward().z, 0.0f, //FORWARD
vec.x, vec.y, vec.z, 1.0f);
box.setLocalRotation(mat.toRotationMatrix());
Material boxMat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
boxMat.setBoolean("UseMaterialColors", true);
boxMat.setColor("Ambient", ColorRGBA.Green);
boxMat.setColor("Diffuse", ColorRGBA.Green);
box.setMaterial(boxMat);
obstacles.attachChild(box);
box.addControl(new RigidBodyControl(0));
bulletAppState.getPhysicsSpace().add(box);
}
}
public void buildPlayer() {
player = new Node("player");
Box boxMesh = new Box(1f, 1f, 1f);
Geometry boxGeo = new Geometry("box", boxMesh);
Material boxMat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
boxMat.setBoolean("UseMaterialColors", true);
boxMat.setColor("Ambient", ColorRGBA.Green);
boxMat.setColor("Diffuse", ColorRGBA.Green);
boxGeo.setMaterial(boxMat);
ParticleEmitter fire =
new ParticleEmitter("Emitter", ParticleMesh.Type.Triangle, 30);
Material mat_red = new Material(assetManager,
"Common/MatDefs/Misc/Particle.j3md");
mat_red.setTexture("Texture", assetManager.loadTexture(
"Effects/Explosion/flame.png"));
fire.setMaterial(mat_red);
fire.setImagesX(2);
fire.setImagesY(2); // 2x2 texture animation
fire.setEndColor(new ColorRGBA(0f, 0f, 1f, 1f)); // red
fire.setStartColor(new ColorRGBA(1f, 1f, .5f, 0.5f)); // yellow
fire.getParticleInfluencer().setInitialVelocity(new Vector3f(0, 0, -2));
fire.setStartSize(.3f);
fire.setEndSize(0.01f);
fire.setGravity(0, 0.2f, 0);
fire.setLowLife(.5f);
fire.setHighLife(2f);
fire.setParticlesPerSec(1000);
fire.getParticleInfluencer().setVelocityVariation(0.3f);
player.attachChild(fire);
player.attachChild(boxGeo);
RigidBodyControl control = new RigidBodyControl(0);
boxGeo.addControl(control);
bulletAppState.getPhysicsSpace().add(boxGeo);
}
protected PhysicsSpace getPhysicsSpace() {
return bulletAppState.getPhysicsSpace();
}[/java]
[java]
public class InGameState extends AbstractAppState implements PhysicsCollisionListener {
Geometry tunnel;
private Main app;
Node player;
Dynamics dyn;
private BulletAppState bulletAppState;
@Override
public void collision(PhysicsCollisionEvent event) {
System.out.println("there was an event");
if ("player".equals(event.getNodeA().getName()) || "player".equals(event.getNodeB().getName())) {
if ("obstacles".equals(event.getNodeA().getName()) || "obstacles".equals(event.getNodeB().getName())) {
System.out.println("Hit Box");
}
}
}
[/java]
Also the bulletAppState.getPhysicsSpace().enableDebug(assetManager); is depreciated and throws an exception now.
Thanks for any info.
[java][/java] [java][/java] [java][/java]