Triangle collision

Hey guys !



I searched a lot for that can’t find what I did wrong. It’s been like hours that I tried to make a triangle “solid”. I use the SimplexCollisionShape class to do so but it doesn’t work…I cant find where is it from. Maybe you guys can help my sorry bottom ? Here’s my code :



for(int l = 0; l < this.triangles.size(); l ++){
Mesh newMesh = new Mesh();

Vector3f [] vertices = new Vector3f[3];
vertices[0] = this.triangles.get(l).get1();
vertices[1] = this.triangles.get(l).get2();
vertices[2] = this.triangles.get(l).get3();

Vector2f[] texCoord = new Vector2f[3];
texCoord[0] = new Vector2f(0,1);
texCoord[1] = new Vector2f(1,1);
texCoord[2] = new Vector2f(0,0);

int [] indexes = { 0,1,2};

newMesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
newMesh.setBuffer(Type.TexCoord, 3, BufferUtils.createFloatBuffer(texCoord));
newMesh.setBuffer(Type.Index, 3, BufferUtils.createIntBuffer(indexes));
newMesh.updateBound();

Geometry newGeom = new Geometry("newTriangle", newMesh);
Material mat = new Material(this.assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
ColorRGBA color = new ColorRGBA((138.0f+(l*2))/ 255.0f, 68.0f/255.0f, 154.0f/255.0f, 1.0f);
mat.setColor("Color", color);
newGeom.setMaterial(mat);

//Important buggy part
SimplexCollisionShape triColl = new SimplexCollisionShape(this.triangles.get(l).get1(),
this.triangles.get(l).get2(),
this.triangles.get(l).get3());
RigidBodyControl triControl = new RigidBodyControl(triColl, 0.0f);
newGeom.addControl(triControl);

this.getRootNode().attachChild(newGeom);
this.getBulletAppState().getPhysicsSpace().add(triControl);
}


Eveything seems fine : I create a triangle inside the this.triangles list, I convert them to meshes and those triangles displays in a good way. But the collision debug lines dont appear on the triangles and my character passes through those triangles. They have no collision.

Any idea or suggestion guys ? Thank you in advance.

Do you see the triangle in physics debug? (physicsSpace.enableDebug(assetManager):wink:

Yeah, that’s what I said : with physicsSpace.enableDebug(assetManager) on, the physic triangle doesnt appear (I guess it should translate as a blue triangle, as my boxes get two triangles with blue borders on their faces). But the visual triangle (the mesh) appears without errors.

Can you please make a test case for this?

[java]public class Main extends SimpleApplication {



private BulletAppState bulletAppState;



public static void main(String[] args) {

Main app = new Main();



app.start();

}



@Override

public void simpleInitApp() {

bulletAppState = new BulletAppState();

bulletAppState.setEnabled(true);

stateManager.attach(bulletAppState);



bulletAppState.getPhysicsSpace().enableDebug(assetManager);



Triangle ttt = new Triangle(new Vector3f(3.0f, 3.0f, 3.0f),

new Vector3f(3.0f, 2.0f, 3.0f),

new Vector3f(1.0f, 3.0f, 3.0f));

Mesh g = new Mesh();



Vector3f [] vertices = new Vector3f[3];

vertices[0] = ttt.get1();

vertices[1] = ttt.get2();

vertices[2] = ttt.get3();



Vector2f[] texCoord = new Vector2f[3];

texCoord[0] = new Vector2f(0,1);

texCoord[1] = new Vector2f(1,1);

texCoord[2] = new Vector2f(0,0);



int [] indexes = { 0,1,2};



g.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));

g.setBuffer(Type.TexCoord, 3, BufferUtils.createFloatBuffer(texCoord));

g.setBuffer(Type.Index, 3, BufferUtils.createIntBuffer(indexes));

g.updateBound();

Geometry ngeom = new Geometry("A shape", g);

ngeom.setLocalTranslation(5.0f, 0.0f, 0.0f);



Material gmat = new Material( this.assetManager,

"Common/MatDefs/Misc/ShowNormals.j3md");

ngeom.setMaterial(gmat);



SimplexCollisionShape triColl = new SimplexCollisionShape(ttt.get1(), ttt.get2(), ttt.get3());

RigidBodyControl triControl = new RigidBodyControl(triColl, 0);

ngeom.addControl(triControl);



this.getRootNode().attachChild(ngeom);

this.getBulletAppState().getPhysicsSpace().add(triControl);



}



@Override

public void simpleUpdate(float tpf) {



}



@Override

public void simpleRender(RenderManager rm) {



}



public Node getRootNode(){

return this.rootNode;

}



public BulletAppState getBulletAppState(){

return this.bulletAppState;

}

}[/java]

1 Like

Here’s an update. This morning, I found out that the SimplexCollisionShape is actually created but the coordinates of the points of this “collision shape” are wrong.



See the image below extracted from my tests : the light green thing is a box, representing the floor. It has a collision box which works good. I can walk on it and I can see it with the debug shapes on, so I do not have a problem with the physic space.

The dark green thing is the terrain I want to create. It is made of triangles, and it displays in a good way (though we cannot see it on this image). The yellow triangle is the result of my tests. It has been created with the test case code I posted.







You see these two blue dots ? I couldn’t figure out what it was, so I commented the yellow triangle section and figured it made one of the blue dot disappear. The remaining blue dot is in the middle of the terrain (dark green stuff), so it is in fact the SimplexCollisionShape ! Why doesnt it follow the triangles shapes ? it’s like every points of the SimplexCollisionShape is at the same coordinates.

Okay, it seems that SimplexCollisionShape is broken. I decided I’ll use a MeshCollisionShape instead. Not a great solution in optimisation, I think but that’ll do for the moment.