After a while i’m getting with this. For the record, I made an update to ofxBulletv2.0 and put some code from https://github.com/trotil/ofxBullet wich made objects collide correctly with the terrain imported with ofxAssimpModelLoader.
http://imgur.com/SHLcKhu
I had to add some code because it was showing some wrong. Here an example
http://imgur.com/gFjC52B
And this is how look my add Mesh method now.
bool ofxBulletCustomShape::addMesh( ofMesh a_mesh, ofVec3f a_localScaling, bool a_bUseConvexHull ) {
if(a_mesh.getMode() != OF_PRIMITIVE_TRIANGLES) {
ofLog( OF_LOG_ERROR, "ofxBulletCustomShape :: addMesh : mesh must be set to OF_PRIMITIVE_TRIANGLES!! aborting");
return false;
}
if(_bAdded == true) {
ofLog( OF_LOG_ERROR, "ofxBulletCustomShape :: addMesh : can not call after calling add()" );
return false;
}
btVector3 localScaling( a_localScaling.x, a_localScaling.y, a_localScaling.z );
vector <ofIndexType> indicies = a_mesh.getIndices();
vector <ofVec3f> verticies = a_mesh.getVertices();
btVector3 centroid = btVector3(0, 0, 0);
if(!a_bUseConvexHull) {
for(int i = 0; i < verticies.size(); i++) {
btVector3 tempVec = btVector3(verticies[i].x, verticies[i].y, verticies[i].z);
tempVec *= localScaling;
centroid += tempVec;
}
centroid /= (float)verticies.size();
// vector<btVector3> newVerts;
// for ( int i = 0; i < indicies.size(); i++) {
// btVector3 vertex( verticies[indicies[i]].x, verticies[indicies[i]].y, verticies[indicies[i]].z);
// vertex *= localScaling;
// vertex -= centroid;
// newVerts.push_back(vertex);
btTriangleMesh* trimesh = new btTriangleMesh();
for ( int i = 0; i < indicies.size()/3; i++) {
int index0 = indicies[i*3];
int index1 = indicies[i*3+1];
int index2 = indicies[i*3+2];
btVector3 vertex0( verticies[index0].x, verticies[index0].y, verticies[index0].z );
btVector3 vertex1( verticies[index1].x, verticies[index1].y, verticies[index1].z );
btVector3 vertex2( verticies[index2].x, verticies[index2].y, verticies[index2].z );
vertex0 *= localScaling;
vertex1 *= localScaling;
vertex2 *= localScaling;
// this code corrects the position of the mesh
vertex0 -= centroid;
vertex1 -= centroid;
vertex2 -= centroid;
trimesh->addTriangle(vertex0, vertex1, vertex2);
}
// btConvexHullShape* convexShape = new btConvexHullShape(&(newVerts[0].getX()), newVerts.size());
// convexShape->setMargin( 0.01f );
// shapes.push_back( convexShape );
btBvhTriangleMeshShape *shapetr = new btBvhTriangleMeshShape(trimesh, false);
shapes.push_back( shapetr );
centroids.push_back( ofVec3f(centroid.getX(), centroid.getY(), centroid.getZ()) );
} else {
// HULL Building code from example ConvexDecompositionDemo.cpp //
btTriangleMesh* trimesh = new btTriangleMesh();
for ( int i = 0; i < indicies.size()/3; i++) {
int index0 = indicies[i*3];
int index1 = indicies[i*3+1];
int index2 = indicies[i*3+2];
btVector3 vertex0( verticies[index0].x, verticies[index0].y, verticies[index0].z );
btVector3 vertex1( verticies[index1].x, verticies[index1].y, verticies[index1].z );
btVector3 vertex2( verticies[index2].x, verticies[index2].y, verticies[index2].z );
vertex0 *= localScaling;
vertex1 *= localScaling;
vertex2 *= localScaling;
trimesh->addTriangle(vertex0, vertex1, vertex2);
}
//cout << "ofxBulletCustomShape :: addMesh : input triangles = " << trimesh->getNumTriangles() << endl;
//cout << "ofxBulletCustomShape :: addMesh : input indicies = " << indicies.size() << endl;
//cout << "ofxBulletCustomShape :: addMesh : input verticies = " << verticies.size() << endl;
btConvexShape* tmpConvexShape = new btConvexTriangleMeshShape(trimesh);
//create a hull approximation
btShapeHull* hull = new btShapeHull(tmpConvexShape);
btScalar margin = tmpConvexShape->getMargin();
hull->buildHull(margin);
tmpConvexShape->setUserPointer(hull);
centroid = btVector3(0., 0., 0.);
for (int i = 0; i < hull->numVertices(); i++) {
centroid += hull->getVertexPointer()[i];
}
centroid /= (float)hull->numVertices();
//printf("ofxBulletCustomShape :: addMesh : new hull numTriangles = %d\n", hull->numTriangles());
//printf("ofxBulletCustomShape :: addMesh : new hull numIndices = %d\n", hull->numIndices());
//printf("ofxBulletCustomShape :: addMesh : new hull numVertices = %d\n", hull->numVertices());
btConvexHullShape* convexShape = new btConvexHullShape();
for (int i=0;i<hull->numVertices();i++) {
convexShape->addPoint(hull->getVertexPointer()[i] - centroid);
}
delete tmpConvexShape;
delete hull;
shapes.push_back( convexShape );
centroids.push_back( ofVec3f(centroid.getX(), centroid.getY(), centroid.getZ()) );
}
return true;
}