void BulletRigidBody::setMass(f32 Mass)
{
    DynamicPhysicsObject::setMass(Mass);
    
    if (BtBody_)
    {
        btVector3 LocalInertia(0, 0, 0);
        BtShape_->calculateLocalInertia(Mass_, LocalInertia);
        LocalInertia_ = dim::vector3df(LocalInertia.x(), LocalInertia.y(), LocalInertia.z());
        
        BtBody_->setMassProps(Mass_, LocalInertia);
    }
}
	/**
	 * addCollisionMap
	 */
	rigidbody* PhysicsManagerImpl::addCollisionMap(meshscenenode * node)
	{
		if(node == 0) return 0; // Avoid crashing here in the library
		// Bullet specific for now
		node->grab();
		btTriangleMesh * mesh = getTriangleMesh(node);

		if(mesh == 0) // Looks like there was no mesh data - or something went wrong?
		{
			node->drop();
			return 0;
		}

		map = new mapshape(mesh, false, true);

		// Init pos - Irrlicht specific
		btTransform trans;
		trans.setIdentity();
		//trans.setOrigin(vector3dfToBtVector3(node->getAbsolutePosition()));
		trans.setOrigin(btVector3(0,0,0));

		// Default motion state
		btDefaultMotionState * motionstate = new btDefaultMotionState(trans);

		// Local Inertia
		btVector3 LocalInertia(0,0,0);
//		map->calculateLocalInertia(0.0, LocalInertia);

		rigidbody * b = new rigidbody(0.0f, motionstate, map, LocalInertia);
		b->setUserPointer((void*)node);
		b->setGravity(btVector3(0,0,0));
		b->setCollisionFlags(1); // Static object
		getWorld()->addRigidBody(b);
		collisions.push_back(b);
		return b;
	}
Beispiel #3
0
int main(int ArgumentCount, char **Arguments) {

	// Set up physics modules
	CollisionConfiguration = new btDefaultCollisionConfiguration();
	//BroadPhase = new btAxisSweep3(btVector3(-1000, -1000, -1000), btVector3(1000, 1000, 1000));
	BroadPhase = new btDbvtBroadphase();
	Dispatcher = new btCollisionDispatcher(CollisionConfiguration);
	Solver = new btSequentialImpulseConstraintSolver();
	World = new btDiscreteDynamicsWorld(Dispatcher, BroadPhase, Solver, CollisionConfiguration);
	World->setGravity(btVector3(0.0, -9.81, 0.0));
	btContactSolverInfo &SolverInfo = World->getSolverInfo();
	//SolverInfo.m_splitImpulseTurnErp = 0.0f;
	//SolverInfo.m_splitImpulse = 1;
	//SolverInfo.m_splitImpulsePenetrationThreshold = -0.02f;
	
	gContactAddedCallback = CustomMaterialCallback;
	
	{
		btScalar Mass = 1.0;
		btCollisionShape *Shape = new btSphereShape(0.5);
		btVector3 LocalInertia(0.0, 0.0, 0.0);
		Shape->calculateLocalInertia(Mass, LocalInertia);
		
		SphereBody = new btRigidBody(Mass, NULL, Shape, LocalInertia);
		SphereBody->setFriction(1.0);
		SphereBody->setDamping(0.1, 0.3);
		SphereBody->setActivationState(DISABLE_DEACTIVATION);
		SphereBody->translate(btVector3(0.71, 2.5, -0.8));
		
		World->addRigidBody(SphereBody);
	}
	
	// Set to 1 to test static box shape
	if(USE_STATIC_BOX) {
		
		btScalar Mass = 0.0;
		btCollisionShape *Shape = new btBoxShape(btVector3(1, 1, 1));
		btVector3 LocalInertia(0.0, 0.0, 0.0);
		Shape->calculateLocalInertia(Mass, LocalInertia);
		
		BoxBody = new btRigidBody(Mass, NULL, Shape, LocalInertia);
		BoxBody->setFriction(1.0);
		BoxBody->setDamping(0.1, 0.3);
		BoxBody->setActivationState(DISABLE_DEACTIVATION);
		BoxBody->translate(btVector3(0, -2, 0));
		
		World->addRigidBody(BoxBody);
	}
	else {
		btScalar Mass = 0.0;
		int VertexCount = sizeof(Vertices) / sizeof(btScalar);
		int FaceCount = sizeof(Indices) / sizeof(int) / 3;
		TriangleIndexVertexArray = new btTriangleIndexVertexArray(FaceCount, Indices, 3 * sizeof(int), VertexCount, Vertices, 3 * sizeof(btScalar));

		btBvhTriangleMeshShape *Shape = new btBvhTriangleMeshShape(TriangleIndexVertexArray, true);
		btTriangleInfoMap *TriangleInfoMap = new btTriangleInfoMap();
		btGenerateInternalEdgeInfo(Shape, TriangleInfoMap);
	
		MeshBody = new btRigidBody(Mass, NULL, Shape);
		MeshBody->setFriction(1.0);
		MeshBody->setDamping(0.1, 0.3);
		MeshBody->setCollisionFlags(MeshBody->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);

		// Add body
		World->addRigidBody(MeshBody);
	}
	
	for(int i = 0; i < 100; i++) {
		World->stepSimulation(TIMESTEP, 0, 0);
	
		const btTransform &Transform = SphereBody->getCenterOfMassTransform();
		const btVector3 &Position = Transform.getOrigin();
		printf("%f %f %f\n", Position.getX(), Position.getY(), Position.getZ());
	}
	
	World->removeRigidBody(SphereBody);
	if(MeshBody)
		World->removeRigidBody(MeshBody);
	if(BoxBody)
		World->removeRigidBody(BoxBody);
		
	delete SphereBody->getCollisionShape();	
	delete SphereBody;

	if(MeshBody) {
		delete ((btBvhTriangleMeshShape *)MeshBody->getCollisionShape())->getTriangleInfoMap();	
		delete ((btBvhTriangleMeshShape *)MeshBody->getCollisionShape())->getMeshInterface();	
		delete MeshBody->getCollisionShape();	
		delete MeshBody;
	}
	
	if(BoxBody) {
		delete BoxBody->getCollisionShape();	
		delete BoxBody;
	}
	
	delete World;
	delete Solver;
	delete Dispatcher;
	delete BroadPhase;
	delete CollisionConfiguration;

	return 0;
}