Page 1 of 1

btBvhTriangleMeshShape does not react to physics

Posted: Tue Jun 14, 2016 4:30 pm
by standtall007
Hi i am new to Bullet Physics and i have been trying to implement it into my game engine.
i have a huge problem, i can render the triangleMesh, but i cannot apply physics to it!

here is the base code i use for the adding and rendering with bullet:

Code: Select all

btRigidBody *addPhysicsMesh(float mass)
	{
		btTriangleMesh *mesh = new btTriangleMesh();

		for (int i = 0; i < m_model->faces; ++i)
		{
			GLuint index0 = 3 * m_model->indices[i];
			GLuint index1 = 3 * m_model->indices[i + 1];
			GLuint index2 = 3 * m_model->indices[i + 2];

			GLfloat *pos0 = m_model->verts[index0];
			GLfloat *pos1 = m_model->verts[index1];
			GLfloat *pos2 = m_model->verts[index2];

			btVector3 v0(pos0[0], pos0[1], pos0[2]);
			btVector3 v1(pos1[0], pos1[1], pos1[2]);
			btVector3 v2(pos2[0], pos2[1], pos2[2]);

			mesh->addTriangle(v0, v1, v2);
		}

		btCollisionShape *shape = new btBvhTriangleMeshShape(mesh, true);

		btTransform t;
		t.setIdentity();
		t.setOrigin(btVector3(m_positionXYZ.x, m_positionXYZ.y, m_positionXYZ.z));

		btDefaultMotionState *motion = new btDefaultMotionState(t);

		btVector3 inertia(0.0f, 0.0f, 0.0f);

		if (mass != 0.0f)
			shape->calculateLocalInertia(mass, inertia);

		btRigidBody::btRigidBodyConstructionInfo info(mass, motion, shape, inertia);
		btRigidBody *body = new btRigidBody(info);
		
		return body;
	}

	void renderPhysicsMesh(btRigidBody *mesh, ShaderProgram shader)
	{
		if (mesh->getCollisionShape()->getShapeType() != TRIANGLE_MESH_SHAPE_PROXYTYPE)
			return;

		btTransform t;

		mesh->getMotionState()->getWorldTransform(t);

		float mat[16];
		t.getOpenGLMatrix(mat);

		glPushMatrix();

		glMultMatrixf(mat);

		m_model->Draw(shader);

		glPopMatrix();
	}
I appreciate all the help i can get, and the only thing the bullet physics won't do at this point is to use the mass and pull a model downwards!
I should also note that i have been setting the world's gravity so it shouldn't be that, but please let me know if the gravity has to be updated all the time, since i am unaware of this.

Thanks for making this physics system open source :)

Re: How come this not working?

Posted: Tue Jun 14, 2016 8:20 pm
by inzombiak
The mass your'e passing isn't 0 by any chance right? Bullet treats a mass of 0 like it is infinite heavy and thus immovable.

Re: How come this not working?

Posted: Tue Jun 14, 2016 8:39 pm
by standtall007
inzombiak wrote:The mass your'e passing isn't 0 by any chance right? Bullet treats a mass of 0 like it is infinite heavy and thus immovable.
Nope the mass was set to both 1.0f and a wopping 100.0f and neither worked but just how significant is the btRigidBody->setUserPointer(void*)?

i have an example on how i add the objects to the discreteWorld:

Code: Select all

if (k < gameObjects.size())
	{
		body = gameObjects[k]->addPhysicsMesh(0.0f);

		world->addRigidBody(body);
		rigidbodies.push_back(body);

		body->setUserPointer((void*)k);

		k++;
	}

Re: How come this not working?

Posted: Tue Jun 14, 2016 8:48 pm
by inzombiak
Well in that snippet the mass is 0.
The user pointer isn't a necessity from what I know. It just allows you to bind a pointer (Like one to the object that owns said body) to the btRigidBody. Then when collisions happen or you do a ray cast you can get the object from the btRigidBody.

How is the physics not working? Does the regular Hello World example work? Are tri meshes the only ones that give you issues?

Re: How come this not working?

Posted: Tue Jun 14, 2016 8:56 pm
by standtall007
inzombiak wrote:Well in that snippet the mass is 0.
The user pointer isn't a necessity from what I know. It just allows you to bind a pointer (Like one to the object that owns said body) to the btRigidBody. Then when collisions happen or you do a ray cast you can get the object from the btRigidBody.

How is the physics not working? Does the regular Hello World example work? Are tri meshes the only ones that give you issues?
To be honest i practically just started to learn Bullet Physics, so i haven't tried any examples yet, instead i tried to follow some tutorials regarding Bullet...
Maybe a dumb move from my point of view :P but i got so much going on with my engine so i have to try and see if i can get things to work!

Regarding how the physics are not working: well it visualizes the model i import (it draws it so that part doesn't contain errors!) but the model(s) imported aren't actually falling downwards when they have a mass higher than 0.0! and they don't either when the mass actually is at 0.0!

Re: How come this not working?

Posted: Wed Jun 15, 2016 6:56 am
by inzombiak
Do you add the bodies to the physics world? Is the world set up correctly?

I recommend at least looking at the Hello World example on the wiki.

Re: How come this not working?

Posted: Wed Jun 15, 2016 6:59 am
by standtall007
inzombiak wrote:Do you add the bodies to the physics world? Is the world set up correctly?

I recommend at least looking at the Hello World example on the wiki.
I believe that the DynamicWorld is set up correctly but of course i could be wrong.
Thanks for the tip, i will definitely look at the Hello World example, and i will return here if it doesn't work out for me either.

Thanks a lot for your help inzombiak, i really appreciate your help :)

Cheers,
David.

Re: How come this not working?

Posted: Wed Jun 15, 2016 8:07 am
by S1L3nCe
Hi,

btBvhTriangleMeshShape is static only, you can't apply physics on it.
You can use btConvexHullShape for create dynamic convex shapes.

Re: How come this not working?

Posted: Mon Jun 20, 2016 8:37 am
by benelot
And please go to your first post and edit its title to something more meaningful like "btBvhTriangleMeshShape does not react to physics". This will change the name of the thread, making it easier to help for other people to know what is going on.