[Solved] using CollisionShapeFactory.createMeshShape gives NullPointerException

so… i’ve been with this problem for a while now…

if i use “CollisionShapeFactory.createMeshShape” the application crashes giving me a null pointer exception…



nothing i do or try seems to fix it… though if i use “CollisionShapeFactory.createDynamicMeshShape” it works fine, but it gives me a hull shape which is not what i want…



here’s all the relevant code, as simplified as posible… “THIS” is a Geometry which then gets placed into a node and then into the rootnode.



Mesh mapmesh = new Mesh();

mapmesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(v3));

mapmesh.setBuffer(Type.Normal, 3, BufferUtils.createFloatBuffer(n3));

mapmesh.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(v2));

// mapmesh.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indx));

mapmesh.setBuffer(Type.Color, 4, BufferUtils.createFloatBuffer(colorArray));



mapmesh.updateBound();

//mapmesh.setDynamic();

mapmesh.setStatic();



mapmesh.createCollisionData();





this.setMesh(mapmesh);

this.setName(name);

this.setMaterial(mat1);



this.setBoundRefresh();

this.updateModelBound();

this.updateGeometricState();

//System.out.println(this.getModelBound());

this.updateWorldBound();



CollisionShape test;

test = CollisionShapeFactory.createMeshShape(this); <— TerrainChunk.java:418 (this is the line it crashes on, just in case)



RigidBodyControl floor_phy;

floor_phy = new RigidBodyControl(test, 0);



//i also used to do: RigidBodyControl floor_phy = new RigidBodyControl(CollisionShapeFactory.createMeshShape(this), 0);

//and it crashes just the same…



this.addControl(floor_phy);

bulletAppState.getPhysicsSpace().add(floor_phy);





31/12/2011 22:11:42 com.jme3.app.Application handleError

GRAVE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]

java.lang.NullPointerException

at com.jme3.bullet.collision.shapes.MeshCollisionShape.createCollisionMesh(Unknown Source)

at com.jme3.bullet.collision.shapes.MeshCollisionShape.(Unknown Source)

at com.jme3.bullet.util.CollisionShapeFactory.createSingleMeshShape(Unknown Source)

at com.jme3.bullet.util.CollisionShapeFactory.createMeshShape(Unknown Source)

at core.TerrainChunk.generarNuevosCubos(TerrainChunk.java:418)

at core.TerrainChunk.recalcular(TerrainChunk.java:267)

at core.TerrainFactory.init(TerrainFactory.java:272)

at core.MainCore.init(MainCore.java:177)

at core.MainCore.simpleInitApp(MainCore.java:139)

at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:230)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:129)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:205)

at java.lang.Thread.run(Unknown Source)



i really was starting to give up on it and looking how to do a compound shape myself, but i really want to know what’s going on and what am i doing wrong… also using CollisionShapeFactory would save me time… but well… any insights are appreciated :slight_smile:



thx in advance…



edit:

i now believe my problem lies with the fact i commented the index buffer, i though i didnt need it… i dont quite understand it so im doing my research on it now :slight_smile:



edit2: yep, it was the indexes… sorry about the trouble :stuck_out_tongue: but hope this helps someone that makes the same mistake i did xD :slight_smile:



cheers…

1 Like
edit2: yep, it was the indexes… sorry about the trouble :P but hope this helps someone that makes the same mistake i did


Yes, it will :).

lol… since i was using just 3 vertex, and each triplet was a single tringle, i didnt think i would need the indexes… it actually worked all fine, untill i tried to do the “CollisionShapeFactory.createMeshShape” xD



cheers :stuck_out_tongue:



PD: btw, now i have graphical errors lol… ill fix that soon xD but everything else works awesome!

thx to all the JME3 team for making this work of art ^.^



edit:graphic errors fixed.

tip: start from 0 when making the indexes… lol… a simple:

indexes.add(verticesSize+2);

indexes.add(verticesSize+0);

indexes.add(verticesSize+1);

will work just fine…

and add:

int verticesSize = vertices.size();

before the first vertices.add(blah);

xD



cheers…