Code: Select all
void BulletDebugDrawer::drawLine(const btVector3& from, const btVector3& to, const btVector3& color) {
vertices.push_back(ColoredVertex(vec3(from.getX(), from.getY(), from.getZ()), vec3(color.getX(), color.getY(), color.getZ())));
vertices.push_back(ColoredVertex(vec3(to.getX(), to.getY(), to.getZ()), vec3(color.getX(), color.getY(), color.getZ())));
}
void BulletDebugDrawer::DrawBuffers() {
glUseProgram(shaderProgram);
glBindBuffer(GL_ARRAY_BUFFER, VB);
glBufferData(GL_ARRAY_BUFFER, sizeof(ColoredVertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW);
glUniformMatrix4fv(mvpLocation, 1, GL_TRUE, camera->GetViewProjection().data());
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float), 0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float), (GLvoid*)sizeof(vec3));
glDrawArrays(GL_LINES, 0, vertices.size());
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(0);
vertices.clear();
}

In my scene I have a box collider and a sphere collider. I create them like this:
Box
Code: Select all
btBoxShape* box = new btBoxShape(btVector3(halfExtents[0], halfExtents[1], halfExtents[2]));
btVector3 inertia(0, 0, 0);
if (mass != 0.0f)
box->calculateLocalInertia(mass, inertia);
physicsTransform.setIdentity();
physicsTransform.setOrigin(btVector3(0, 0, 0));
btMotionState* motion = new btDefaultMotionState(physicsTransform);
btRigidBody::btRigidBodyConstructionInfo info = btRigidBody::btRigidBodyConstructionInfo(mass, motion, box, inertia);
rigidBody = new btRigidBody(info);
physicsEngine = parent->GetRenderer()->GetPhysicsEngine();
physicsEngine->GetWorld()->addRigidBody(rigidBody);
Code: Select all
btSphereShape* sphere = new btSphereShape(radius);
btVector3 inertia(0, 0, 0);
if (mass != 0.0f)
sphere->calculateLocalInertia(mass, inertia);
physicsTransform.setIdentity();
physicsTransform.setOrigin(btVector3(0, 10, 0));
btMotionState* motion = new btDefaultMotionState(physicsTransform);
btRigidBody::btRigidBodyConstructionInfo info = btRigidBody::btRigidBodyConstructionInfo(mass, motion, sphere, inertia);
rigidBody = new btRigidBody(info);
physicsEngine = parent->GetRenderer()->GetPhysicsEngine();
physicsEngine->GetWorld()->addRigidBody(rigidBody);
Code: Select all
void SphereCollider::Update(float delta) {
btTransform tr;
tr.setIdentity();
rigidBody->getMotionState()->getWorldTransform(tr);
tr.setOrigin(btVector3(GetTransform()->GetPos_r()[0], GetTransform()->GetPos_r()[1], GetTransform()->GetPos_r()[2]));
tr.setRotation(btQuaternion(GetTransform()->GetRot_r()[GetTransform()->GetRot_r().X], GetTransform()->GetRot_r()[GetTransform()->GetRot_r().Y],
GetTransform()->GetRot_r()[GetTransform()->GetRot_r().Z], GetTransform()->GetRot_r()[GetTransform()->GetRot_r().W]));
rigidBody->getMotionState()->setWorldTransform(tr);
}
void SphereCollider::PostUpdate(float delta) {
btTransform tr;
tr.setIdentity();
rigidBody->getMotionState()->getWorldTransform(tr);
GetTransform()->SetPos(tr.getOrigin().getX(), tr.getOrigin().getY(), tr.getOrigin().getZ());
GetTransform()->SetRot(quat(tr.getRotation().getW(), tr.getRotation().getX(), tr.getRotation().getY(), tr.getRotation().getZ()));
}