MeshCollisionShape

Hi,

i try to add a collisionShape to a mesh, but a get an exception. Can somebody help me? My code:
Mesh mesh = new Mesh();
mesh.setMode(Mesh.Mode.Lines);
mesh.setBuffer(VertexBuffer.Type.Position, 3, new float[]{0, 0, 0, 100, 0, -200});
mesh.setBuffer(VertexBuffer.Type.Index, 2, new short[]{0, 1});
Geometry g = new Geometry(“line”, mesh);
g.setMaterial(borderMat);

    MeshCollisionShape meshCollisionShape = new MeshCollisionShape(mesh);
    mesh.updateBound();
    mesh.updateCounts();
    RigidBodyControl bodyControl = new RigidBodyControl(meshCollisionShape, 0f);
    g.addControl(bodyControl);
    rootNode.attachChild(g);
    this.bulletAppState.getPhysicsSpace().add(g);

The Exception:

java.lang.IndexOutOfBoundsException
at java.nio.Buffer.checkIndex(Buffer.java:532)
at java.nio.DirectShortBufferU.get(DirectShortBufferU.java:253)
at com.jme3.scene.mesh.IndexShortBuffer.get(IndexShortBuffer.java:53)
at com.jme3.bullet.util.Converter.convert(Converter.java:248)
at com.jme3.bullet.collision.shapes.MeshCollisionShape.createCollisionMesh(MeshCollisionShape.java:70)
at com.jme3.bullet.collision.shapes.MeshCollisionShape.<init>(MeshCollisionShape.java:65)
at mygame.Main.initRoad(Main.java:155)
at mygame.Main.simpleInitApp(Main.java:97)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:226)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:130)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:207)
at java.lang.Thread.run(Thread.java:722)

You are trying to create a collision shape from a line, which isn’t supported

http://hub.jmonkeyengine.org/forum/topic/collision-detection-for-mesh-not-happening/

I would say a MeshCollisionShape cant be made from a mesh of type Mesh.Mode.Lines, I imagine it would need a triangle based meshes to work with.

@werule:
When isn’t possible to set a collision from a line, which alternative can i use. I try to create a road with bordercollision, but i can not use a box for the border. Can i create a box like a line with start and end points?

@thetoucher, i start with jmonkey: can you tell me, how can i do that?

@emoleumassi said: Can i create a box like a line with start and end points?

You can, I would try using a BoxCollisionShape as mentioned on the “Physics: Gravity, Collisions, Forces” page. Just make sure your box isn’t to small or narrow, otherwise objects may pass right through it, and not register a collision.

thank, i known this solution. I try to make a line for a road. I thougt, it is better with a line. If i can make with the box, i 'll be happy. The problem is, how can i say, the problem start by (X1, Y1, Z1) to (X2, Y2, Z2). I think, i can only the box’s size is (x,y,z) and the coordinate, but not the start and end-coordinates

Well, write a little helper factory method to facilitate easy creation by the start-end params rather than the default one. You could also try creating a custom mesh for the road “wall”… idk if it is required to be convex though… anyway you could write a whole helper which would automatically place walls along your whole track, based on its geometry properties and some parameters…