//-------------------------------------------------------------- void ofxBulletSphere::create( btDiscreteDynamicsWorld* a_world, btTransform a_bt_tr, float a_mass, float a_radius ) { if(!_bInited || _shape == NULL) { ofxBulletBaseShape::create( a_world, (btCollisionShape*)new btSphereShape( a_radius ), a_bt_tr, a_mass ); } else { ofxBulletBaseShape::create( a_world, _shape, a_bt_tr, a_mass ); } createInternalUserData(); }
//-------------------------------------------------------------- void ofxBulletCapsule::create( btDiscreteDynamicsWorld* a_world, btTransform &a_bt_tr, float a_mass, float a_radius, float a_height ) { if(!_bInited || _shape == NULL) { ofxBulletRigidBody::create( a_world, (btCollisionShape*)new btCapsuleShape(a_radius, a_height ), a_bt_tr, a_mass ); } else { ofxBulletRigidBody::create( a_world, _shape, a_bt_tr, a_mass ); } createInternalUserData(); }
//-------------------------------------------------------------- void ofxBulletConvexShape::create( btDiscreteDynamicsWorld* a_world, btTransform &a_bt_tr, float a_mass ) { if(!_bInited || _shape == NULL) { ofLogWarning("ofxBulletConvexShape :: create : must call init first"); return; } ofxBulletBaseShape::create( a_world, _shape, a_bt_tr, a_mass ); createInternalUserData(); }
//-------------------------------------------------------------- void ofxBulletSoftTriMesh::create( ofxBulletWorldSoft* a_world, ofMesh& aMesh, btTransform &a_bt_tr, float a_mass ) { if(a_world == NULL) { ofLogError("ofxBulletSoftTriMesh") << "create(): a_world param is NULL"; return; } if( aMesh.getMode() != OF_PRIMITIVE_TRIANGLES ) { ofLogError("ofxBulletSoftTriMesh") << " only excepts meshes that are triangles"; return; } _world = a_world; _cachedMesh.clear(); _cachedMesh = aMesh; if( bullet_vertices != NULL ) { delete bullet_vertices; bullet_vertices = NULL; } int vertStride = sizeof(btVector3); int indexStride = 3*sizeof(int); int totalVerts = (int)aMesh.getNumVertices(); int totalIndices = (int)aMesh.getNumIndices(); bullet_vertices = new btScalar[ totalVerts * 3 ]; int* bullet_indices = new int[ totalIndices ]; auto& tverts = aMesh.getVertices(); vector< ofIndexType >& tindices = aMesh.getIndices(); for( int i = 0; i < totalVerts; i++ ) { bullet_vertices[i*3+0] = tverts[i].x; bullet_vertices[i*3+1] = tverts[i].y; bullet_vertices[i*3+2] = tverts[i].z; } for( int i = 0; i < totalIndices; i++ ) { bullet_indices[i] = tindices[i]; } _softBody = btSoftBodyHelpers::CreateFromTriMesh( _world->getInfo(), bullet_vertices, bullet_indices, totalIndices/3 ); _softBody->transform( a_bt_tr ); setMass( a_mass, true ); setCreated(_softBody); createInternalUserData(); delete [] bullet_indices; }
//-------------------------------------------------------------- void ofxBulletEllipsoid::create(ofxBulletWorldSoft* a_world, const ofVec3f& a_center, const ofVec3f& a_radius, int a_res) { if(a_world == NULL) { ofLogError("ofxBulletEllipsoid") << "create(): a_world param is NULL"; return; } _radius = a_radius; _world = a_world; _softBody = btSoftBodyHelpers::CreateEllipsoid(_world->getInfo(), btVector3(a_center.x, a_center.y, a_center.z), btVector3(a_radius.x, a_radius.y, a_radius.z), a_res); setCreated(_softBody); createInternalUserData(); }
//-------------------------------------------------------------- void ofxBulletPatch::create(ofxBulletWorldSoft* a_world, const ofVec3f& a_p0, const ofVec3f& a_p1, const ofVec3f& a_p2, const ofVec3f& a_p3, int a_resx, int a_resy) { if(a_world == NULL) { ofLogError("ofxBulletPatch") << "create(): a_world param is NULL"; return; } _resx = a_resx; _resy = a_resy; _world = a_world; _softBody = btSoftBodyHelpers::CreatePatch(_world->getInfo(), btVector3(a_p0.x, a_p0.y, a_p0.z), btVector3(a_p1.x, a_p1.y, a_p1.z), btVector3(a_p2.x, a_p2.y, a_p2.z), btVector3(a_p3.x, a_p3.y, a_p3.z), a_resx, a_resy, 0, true ); setCreated(_softBody); createInternalUserData(); }
//-------------------------------------------------------------- void ofxBulletCustomShape::add() { _bAdded = true; btTransform trans; trans.setIdentity(); for(int i = 0; i < centroids.size(); i++) { _centroid += centroids[i]; } if(centroids.size() > 0) _centroid /= (float)centroids.size(); btVector3 shiftCentroid; for(int i = 0; i < shapes.size(); i++) { shiftCentroid = btVector3(centroids[i].x, centroids[i].y, centroids[i].z); shiftCentroid -= btVector3(_centroid.x, _centroid.y, _centroid.z); trans.setOrigin( ( shiftCentroid ) ); ((btCompoundShape*)_shape)->addChildShape( trans, shapes[i]); } _rigidBody = ofGetBtRigidBodyFromCollisionShape( _shape, _startTrans, _mass); createInternalUserData(); _world->addRigidBody(_rigidBody); setProperties(.4, .75); setDamping( .25 ); }
//-------------------------------------------------------------- void ofxBulletTriMeshShape::create( btDiscreteDynamicsWorld* a_world, ofMesh& aMesh, btTransform &a_bt_tr, float a_mass, glm::vec3 aAAbbMin, glm::vec3 aAAbbMax ) { if( aMesh.getMode() != OF_PRIMITIVE_TRIANGLES ) { ofLogWarning() << " ofxBulletTriMeshShape :: create : mesh must be using triangles, not creating!!" << endl; return; } if( aMesh.getNumIndices() < 3 ) { ofLogWarning() << " ofxBulletTriMeshShape :: create : mesh must have indices, not creating!" << endl; return; } if( !_bInited || _shape == NULL ) { int vertStride = sizeof(btVector3); int indexStride = 3*sizeof(int); totalVerts = (int)aMesh.getNumVertices(); totalIndices = (int)aMesh.getNumIndices(); const int totalTriangles = totalIndices / 3; if( bullet_indices != NULL ) { removeShape(); } if( bullet_vertices != NULL ) { removeShape(); } if( bullet_indexVertexArrays != NULL ) { removeShape(); } if( _shape != NULL ) { removeShape(); } bullet_vertices = new btVector3[ totalVerts ]; bullet_indices = new int[ totalIndices ]; auto& tverts = aMesh.getVertices(); auto& tindices = aMesh.getIndices(); for( int i = 0; i < totalVerts; i++ ) { bullet_vertices[i].setValue( tverts[i].x, tverts[i].y, tverts[i].z ); } for( int i = 0; i < totalIndices; i++ ) { bullet_indices[i] = (int)tindices[i]; } bullet_indexVertexArrays = new btTriangleIndexVertexArray(totalTriangles, bullet_indices, indexStride, totalVerts, (btScalar*) &bullet_vertices[0].x(), vertStride); // if you are having trouble with objects falling through, try passing in smaller or larger aabbMin and aabbMax // to something closer to the size of your object // // btVector3 aabbMin(-10000,-10000,-10000),aabbMax(10000,10000,10000); if( aAAbbMin.length() > 0 && aAAbbMax.length() > 0 ) { btVector3 aabbMin( aAAbbMin.x, aAAbbMin.y, aAAbbMin.z ); btVector3 aabbMax( aAAbbMax.x, aAAbbMax.y, aAAbbMax.z ); _shape = new btBvhTriangleMeshShape(bullet_indexVertexArrays, true, aabbMin, aabbMax ); } else { _shape = new btBvhTriangleMeshShape(bullet_indexVertexArrays, true, true ); } } ofxBulletRigidBody::create( a_world, _shape, a_bt_tr, a_mass ); createInternalUserData(); updateMesh( a_world, aMesh ); }