I am new to the Bullet SDK and I am already stuck at the first try to integrate Bullet into an existing 3D engine.
For my for test I looked at the tutorial BasicDemo. Could you please tell me what I am missing?
First, this is how I init Bullet:
Code: Select all
dispatcher = new btCollisionDispatcher(true);
btCollisionAlgorithmCreateFunc * sphereSphereCF = new btSphereSphereCollisionAlgorithm::CreateFunc;
dispatcher->registerCollisionCreateFunc(SPHERE_SHAPE_PROXYTYPE, SPHERE_SHAPE_PROXYTYPE, sphereSphereCF);
broadphase = new btSimpleBroadphase;
solver = new btSequentialImpulseConstraintSolver;
dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver);
dynamicsWorld->setGravity(btVector3(0.0, -10.0 , 0.0));
Code: Select all
RSG::ObjectRef groundSphere = RSG::Object::newObject<Sphere>();
groundSphere->setField(RSG::Sphere::RADIUS_FIELD_ID, 7.0);
createObject(groundSphere);
Code: Select all
void BulletTest::createObject(RSG::ObjectRef node, double mass = 0.0)
{
if (node->getClassId() == RSG::Sphere::getStaticClassId())
{
double radius = node->getField<double>(RSG::Sphere::RADIUS_FIELD_ID);
HomogenousMatrixD4 transform = node->getField<HomogenousMatrixD4>(RSG::Sphere::TRANSFORMATION_FIELD_ID);
btCollisionShape* shape = new btSphereShape(btScalar(radius));
btVector3 localInertia(0, 0, 0)
if (mass > 0.0)
shape->calculateLocalInertia(mass, localInertia);
TestMotionState * motionState = new TestMotionState(node, transform);
btRigidBody* body = new btRigidBody(0.0, motionState, shape, localInertia);
dynamicsWorld->addRigidBody(body);
body->setWorldTransform( motionState->transform );
body->activate();
if (mass > 0.0)
{
body->setLinearVelocity(btVector3(0,0,0));
body->setAngularVelocity(btVector3(0,0,0));
}
}
sceneref->addChildNode(node);
}
Code: Select all
for (unsigned int i = 0; i < 50; ++i)
{
int colsize = 2;
int row = (int)((i*0.6*2)/(colsize*2*0.6));
int row2 = row;
int col = (i)%(colsize)-colsize/2;
HomogenousMatrixD4 matrix(VectorD3(col*2*0.6 + (row2%2)*0.6, row*2*0.6+0.6 + 20.0, 0));
RSG::ObjectRef sphere = RSG::Object::newObject<Sphere>();
sphere->setField(RSG::Sphere::TRANSFORMATION_FIELD_ID, matrix);
sphere->setField(RSG::Sphere::RADIUS_FIELD_ID, 0.6);
createObject(sphere, 1.0);
}
Code: Select all
class TestMotionState : public btMotionState
{
friend class BulletTest;
public:
TestMotionState(RSG::ObjectRef obj, const HomogenousMatrixD4& matrix)
: node(obj)
{
transform.setOrigin(btVector3(btScalar(matrix.get(0, 3)), btScalar(matrix.get(1, 3)),
btScalar(matrix.get(2, 3))));
transform.setBasis(btMatrix3x3(btScalar(matrix.get(0, 0)), btScalar(matrix.get(0, 1)), btScalar(matrix.get(0, 2)),
btScalar(matrix.get(1, 0)), btScalar(matrix.get(1, 1)), btScalar(matrix.get(1, 2)),
btScalar(matrix.get(1, 0)), btScalar(matrix.get(1, 1)), btScalar(matrix.get(1, 2))));
};
/// synchronizes world transform from user to physics
virtual void getWorldTransform(btTransform& centerOfMassWorldTrans ) const
{
centerOfMassWorldTrans = transform;
}
///synchronizes world transform from physics to user
///Bullet only calls the update of worldtransform for active objects
virtual void setWorldTransform(const btTransform& centerOfMassWorldTrans)
{
transform = centerOfMassWorldTrans;
float m[16];
transform.getOpenGLMatrix(&m[0]);
HomogenousMatrixD4 matrix;
for (int i = 0; i < 16; ++i)
matrix(i) = m[i];
node->setField(RSG::Node::TRANSFORMATION_FIELD_ID, matrix);
}
private:
RSG::SmartObjectRef<Node> node;
btTransform transform;
bool dirty;
};
Code: Select all
float ms = clock->getTimeMicroseconds();
clock->reset();
float minFPS = 1000000.f/60.f;
if (ms > minFPS)
ms = minFPS;
dynamicsWorld->stepSimulation(1.0/420.f, 1);