总的来说,我是 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/