Hello, I’m trying to use a BoundingBox for collisions the same way I would use a ray, and I’m running into trouble… So I created a single class test case, and I’ve found I’m getting a null error when the terrain collides with the bounding box. Regular geometries are working, but collisions with any part of the terrain throws an error.
Here is my test case. I’m loading a simple scene that contains a terrain, and pressing space bar triggers a collision:
public class Main extends SimpleApplication {
private Vector3f spawn = new Vector3f(0,-7,0);
private Node terrain;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp() {
//load a test terrain
terrain = (Node) getAssetManager().loadModel("Scenes/newScene.j3o");
getRootNode().attachChild(terrain);
terrain.setLocalTranslation(spawn);
// add light and input
flyCam.setMoveSpeed(25);
DirectionalLight sun = new DirectionalLight();
sun.setDirection((new Vector3f(-0.5f, -0.5f, -0.5f)).normalizeLocal());
sun.setColor(ColorRGBA.White);
rootNode.addLight(sun);
inputManager.addMapping("Interact", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addListener(actionListener, "Interact");
}
@Override
public void simpleUpdate(float tpf) {
}
private ActionListener actionListener = new ActionListener() {
@Override
public void onAction(String binding, boolean keyPressed, float tpf) {
testCollisionAndOutput();
}
};
private void testCollisionAndOutput(){
BoundingBox bounds = new BoundingBox(spawn, 15f, 15f, 15f);
Ray ray = new Ray(spawn, new Vector3f(0, -1, 0));
CollisionResults results = new CollisionResults();
// rootNode.collideWith(ray, results); //rays work
terrain.collideWith(bounds, results); //bounding box doesnt :(
for(int i =0; i < results.size(); i ++){
System.out.println("Collision with: " + results.getCollision(i).getGeometry().getClass()); //Error is thrown on this line
}
}
}
And the error:
java.lang.NullPointerException
at mygame.Main.testCollisionAndOutput(Main.java:89)
at mygame.Main.access$000(Main.java:27)
at mygame.Main$1.onAction(Main.java:70)
at com.jme3.input.InputManager.invokeActions(InputManager.java:169)
at com.jme3.input.InputManager.onKeyEventQueued(InputManager.java:469)
at com.jme3.input.InputManager.processQueue(InputManager.java:862)
at com.jme3.input.InputManager.update(InputManager.java:914)
at com.jme3.app.LegacyApplication.update(LegacyApplication.java:725)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:227)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:197)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:232)
at java.lang.Thread.run(Thread.java:748)
Could this be a bug, or am I possibly trying to do this collision incorrectly?
In the past I have never needed to retrieve the CollisionResults list unless I was using a ray… and I’ve even had success using BoundingBoxes (or spheres) to intersect with a terrain using the BoundingVolume’s .intersect()
method many times…
But in this specific case I need to use .collideWith()
to retrieve the CollisionResults list, because I’m using all of the ‘hit’ triangles to reconstruct a decal mesh
(If it helps to give more context, I am (re)-working on a decal / texture projection system that is based on this article)
Thanks
Edit: I forgot to note, I’m running this on v3.2.1-stable-sdk1