java - JMonkeyEngine-几何交集 NullPointerException

标签 java nullpointerexception jmonkeyengine

总的来说,我是 3d 引擎的新手,当我尝试碰撞 Geometry 和 BoundingVolume 对象时,我收到了 NullPointerException。

这是我声明对象的方式(抱歉,目前比较困惑)

    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);

这是我的更新循环

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-当我尝试从 BoundingVolume 和 Geometry 的交集打印每个 CollisionResult 的坐标时,出现 NullPointerException

JMonkey 论坛和 JMonkey 文档似乎都没有任何帮助。你们中有人能帮忙吗?提前致谢。

最佳答案

您的模型未附加到 JBullet 物理。 尝试像这样的 sg:

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);
}

之后你可以检查碰撞!

关于java - JMonkeyEngine-几何交集 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26111291/

相关文章:

java - 空指针异常原因不明

java - NullPointerException - Location & LocationManager - 双

java - 速度测试应用程序 下载 上传 Ping

java - STS 未拾取 log4j2.xml

java - ant:将子目录中的资源包含到 war 文件

java - JME : Import a Cinema 4d Model with texture to jMonkey Projekt

java - Nifty GUI "onClickRepeat"无法在 JME3 中的 "controlDefinition"中工作

java - 重试打开目录中的文件

java - 关于try/catch语句的简单问题

java - "Lambda expressions are not supported in -source 7, use -source 8 to enable lambda expressions"