Search code examples
javanullpointerexceptionjmonkeyengine

JMonkeyEngine-Geometry intersection NullPointerException


I'm new to 3d engines in general, and I'm getting this NullPointerException when I try to collide a Geometry and a BoundingVolume object.

Here's how I declare my objects (sorry, it's rather messy at the moment)

    public void simpleInitApp() {



    Quad q= new Quad(100, 100);
    Dome mesh = new Dome(Vector3f.ZERO, 2, 32, 1f,false);

     geom = new Geometry("Cylinder", mesh); //declared elsewhere

     g3 = new Geometry("lel", q);
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.setColor("Color", ColorRGBA.Blue);
    Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat2.setColor("Color", ColorRGBA.Red);
    geom.setMaterial(mat);

    g3.setMaterial(mat2);

    rootNode.attachChild(geom);
    rootNode.attachChild(g3);

and here's my update loop

public void simpleUpdate(float tpf) {

    // System.out.println("hi");
    BoundingVolume b = g3.getWorldBound(); //should give boundingvolume of the quad

    System.out.println(b.getVolume()); //just to test if this works
    CollisionResults r2 = new CollisionResults(); //declare and initialize the collisionresults
    geom.collideWith(b, r2); //collide
    System.out.println(r2.size()); //this returns a value, usually between 0-2


    for(CollisionResult x:r2){


       System.out.println("x = "+ x.getContactPoint().getX()); 
  /*and oddly enough, i get a NullPointerException here even though the collision appeared successful - this never prints anything either so it's not going out of bounds or anything*/


    }


}

tl;dr-get a NullPointerException when I try to print off the coordinates of each CollisionResult from the intersection of a BoundingVolume and a Geometry

Neither the JMonkey Forums nor the JMonkey docs seem to be of any assistance. Would any of you be able to help? Thanks in advance.


Solution

  • Your models not attached to JBullet physics. try sg like this:

    BulletAppState buleltAppState;
    
    public void simpleInitApp() {
    
        Quad q= new Quad(100, 100);
        Dome mesh = new Dome(Vector3f.ZERO, 2, 32, 1f,false);
    
        geom = new Geometry("Cylinder", mesh); //declared elsewhere
    
        g3 = new Geometry("lel", q);
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setColor("Color", ColorRGBA.Blue);
        Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat2.setColor("Color", ColorRGBA.Red);
        geom.setMaterial(mat);
    
        g3.setMaterial(mat2);
    
        bulletAppState = new BulletAppState();
        stateManager.attach(bulletAppState);
        bulletAppState.getPhysicsSpace().attachChild(geom);
        bulletAppState.getPhysicsSpace().attachChild(g3);
    
        rootNode.attachChild(geom);
        rootNode.attachChild(g3);
    }
    

    after that you can check collision!