[Solved] Bullet debug draw lines all over the place

LazyKernel
Posts: 2
Joined: Thu Oct 08, 2015 2:52 pm

[Solved] Bullet debug draw lines all over the place

Post by LazyKernel »

I've been struggling with debug drawing using VBO for a while. With this code

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();
}
I get this as a result
Image

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);
Sphere

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);
I update every rigidbody between my transform and bullet's transform like this

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()));
}
Getting debug drawing to work would help a lot but I don't know what's broken. Any ideas to point me to the right direction?
Last edited by LazyKernel on Sat Oct 17, 2015 4:24 pm, edited 1 time in total.
User avatar
Dave13h
Posts: 6
Joined: Tue Oct 13, 2015 8:43 am

Re: Bullet debug draw lines all over the place

Post by Dave13h »

I think your problem is your stride..

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float), 0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float), (GLvoid*)sizeof(vec3));

This should be the size of your vertex data. In this case you have 6 floats per vertex. So this will be 6 * sizeof(float)... 24. Then the offset (last argument) will be different on each pointer, half way through that in this case per attribute to split between position and colour.

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, 0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, (GLvoid*) 12);
LazyKernel
Posts: 2
Joined: Thu Oct 08, 2015 2:52 pm

Re: Bullet debug draw lines all over the place

Post by LazyKernel »

What a stupid mistake! Thanks a bunch, it works perfectly now.

Image
User avatar
Dave13h
Posts: 6
Joined: Tue Oct 13, 2015 8:43 am

Re: Bullet debug draw lines all over the place

Post by Dave13h »

Excellent, glad it worked out. I can't count the amount of times I've made similar mistakes :lol: