Help with collision detection in a maze

I’m trying to make a simple 3D maze out of boxes, but I’m having trouble keeping the player out of the boxes. I slightly modified the HelloCollisions tutorial code, and my code now looks like this:



import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;

import com.jme3.math.ColorRGBA;

import com.jme3.light.PointLight;

import com.jme3.bullet.BulletAppState;

import com.jme3.bullet.collision.shapes.BoxCollisionShape;

import com.jme3.bullet.collision.shapes.CompoundCollisionShape;

import com.jme3.bullet.util.CollisionShapeFactory;

import com.jme3.bullet.nodes.PhysicsCharacterNode;

import com.jme3.bullet.nodes.PhysicsNode;

import com.jme3.input.KeyInput;

import com.jme3.input.controls.ActionListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.scene.Node;



public class JME extends SimpleApplication implements ActionListener {



private float camHeight = 0f;

private BulletAppState bulletAppState;

private PhysicsCharacterNode player;

private Vector3f walkDirection = new Vector3f ();

private boolean right = false, left = false, up = false, down = false;



public static void main(String[] args){

JME app = new JME();

app.start();

}



@Override

public void simpleInitApp() {

bulletAppState = new BulletAppState();

stateManager.attach(bulletAppState);

flyCam.setMoveSpeed (.5f);

setupKeys ();



PhysicsNode wall;

CompoundCollisionShape wallShape;

player = new PhysicsCharacterNode(new BoxCollisionShape(new Vector3f (1.5f, 1.5f, 1.5f)), .05f);

player.setJumpSpeed(20);

player.setFallSpeed(30);

player.setGravity(30);

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

bulletAppState.getPhysicsSpace().add(player);

rootNode.attachChild (player);



Vector3f boxVector = new Vector3f (0,0,0);

Node boxes = new Node ();

Box b;

Geometry geom;

Material mat = new Material(assetManager, “Common/MatDefs/Misc/SimpleTextured.j3md”);

mat.setTexture(“m_ColorMap”, assetManager.loadTexture(“Textures/ColoredTex/Monkey.png”));



Box floorBox = new Box (new Vector3f (0,-.5f,0), -100f, 0f, 100f);

Geometry floorGeom = new Geometry (“Floor”, floorBox);

Material floorMat = new Material (assetManager, “Common/MatDefs/Misc/SolidColor.j3md”);

floorMat.setColor (“m_Color”, ColorRGBA.White);

floorGeom.setMaterial (floorMat);

PhysicsNode floor = new PhysicsNode (floorGeom, CollisionShapeFactory.createSingleBoxShape (floorGeom), 0);

floor.setKinematic (true);

rootNode.attachChild (floor);

bulletAppState.getPhysicsSpace().add(floor);



try {

BufferedImage image = ImageIO.read(new File(“test.bmp”));

System.out.println (“BEGIN READ IMAGE”);

for (int z=0; z < image.getHeight (); z++) {

for (int x=0; x < image.getWidth (); x++) {

if (image.getRGB (x, z) == -16777216) {

boxVector.x = x;

boxVector.z = z;

b = new Box (boxVector, .5f,.5f,.5f);

geom = new Geometry (“Box” + x + “,” + z, b);

geom.setMaterial (mat);

System.out.print (image.getRGB (x, z)+ “t”);

boxes.attachChild (geom);

}

}

System.out.println ();

}

System.out.println (“END READ IMAGE”);

}

catch (IOException exc) {}



wallShape = CollisionShapeFactory.createMeshCompoundShape (boxes);

wall = new PhysicsNode (boxes, wallShape, 0);

wall.setKinematic (true);

bulletAppState.getPhysicsSpace().add(wall);

rootNode.attachChild (wall);

}



private void setupKeys() {

inputManager.addMapping(“Lefts”, new KeyTrigger(KeyInput.KEY_A));

inputManager.addMapping(“Rights”, new KeyTrigger(KeyInput.KEY_D));

inputManager.addMapping(“Ups”, new KeyTrigger(KeyInput.KEY_W));

inputManager.addMapping(“Downs”, new KeyTrigger(KeyInput.KEY_S));

inputManager.addListener(this, “Lefts”);

inputManager.addListener(this, “Rights”);

inputManager.addListener(this, “Ups”);

inputManager.addListener(this, “Downs”);

}



public void onAction(String binding, boolean value, float tpf) {

if (binding.equals(“Lefts”)) {

if (value) { left = true; } else { left = false; }

} else if (binding.equals(“Rights”)) {

if (value) { right = true; } else { right = false; }

} else if (binding.equals(“Ups”)) {

if (value) { up = true; } else { up = false; }

} else if (binding.equals(“Downs”)) {

if (value) { down = true; } else { down = false; }

}

}



@Override

public void simpleUpdate (float tpf) {

Vector3f camDir = cam.getDirection().clone().multLocal(0.1f);

Vector3f camLeft = cam.getLeft().clone().multLocal(0.1f);

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.getLocalTranslation());



Vector3f camLocation = cam.getLocation ();

camLocation.y = camHeight;

cam.setLocation (camLocation);

}

}




If I set the extents on my BoxCollisionShape to be large (2 or so) then the player stays out of the boxes, but then they also can’t get into the maze! When I set the extents to something smaller like .5, then you can go right through all the boxes. Is there anyway to make it more “sensitive” so the extents can be small but still keep the player out of the boxes?



Thanks very much!

I guess theres something else wrong, try attachDebugShape(assetManager) to see the actual collision shapes.