Convex shapes problem

Post Reply
morando
Posts: 9
Joined: Wed Jun 27, 2012 7:51 am

Convex shapes problem

Post by morando »

I am using DirectX 9 with VS 2010 and bullet-2.81-rev2613.

Code to generate and add convex shape to scene:

Code: Select all

btRigidBody* CBulletFactory::addConvex(CMesh* mesh, const btVector3& pos, float mass)
{
	btTransform startTrans;
	startTrans.setIdentity();
	startTrans.setOrigin(pos);
	btMotionState* ms = new btDefaultMotionState(startTrans);
	vMotionStates.push_back(ms);// vector of motion states for later cleanup

        // This vertex format is correct for my meshes!!!
	struct VERTEX
	{
		D3DXVECTOR3 p;
		D3DXVECTOR3 n;
		D3DXVECTOR2 t;
	};

	DWORD numVertices = mesh->getMesh()->GetNumVertices();

	VERTEX* pVertices;
	btVector3* btVertices = new btVector3[numVertices];
	mesh->getMesh()->LockVertexBuffer(0, (LPVOID*)&pVertices);
	for(DWORD i = 0; i < numVertices; ++i)
	{
		btVertices[i] = btVector3(pVertices[i].p.x, pVertices[i].p.y, pVertices[i].p.z);
	}
	mesh->getMesh()->UnlockVertexBuffer();

	btConvexHullShape* cs = new btConvexHullShape((btScalar*)btVertices, numVertices, sizeof(btVector3));
	vCollisionShapes.push_back(cs);// vector of collision shapes for later cleanup

	btVector3 localInertia(0.0f, 0.0f, 0.0f);
	cs->calculateLocalInertia(mass, localInertia);

	btRigidBody* body = new btRigidBody(mass, ms, cs, localInertia);
	phyEngine->getPhysicsWorld()->addRigidBody(body);
	vRigidBodies.push_back(body);// vector of rigid bodies for later cleanup

#if 0
	int maxSerializeBufferSize = 1024*1024*5;
	btDefaultSerializer* serializer = new btDefaultSerializer(maxSerializeBufferSize);
	serializer->startSerialization();
	cs->serializeSingleShape(serializer);
	serializer->finishSerialization();
	FILE* f = fopen("bottle.bullet","wb");
	fwrite(serializer->getBufferPointer(), serializer->getCurrentBufferSize(), 1, f);
	fclose(f);
#endif

	return body;
}
It works for rock mesh:
Image
wireframe debug display:
Image

But doesn't for bottle mesh :
Image
wireframe debug display:
Image

apart from bottle wireframe, collision is all wrong for it too.

Can Bullet generate approximate convex shape for this kind of meshes?

Thank you for your time.

EDIT:
Debug display with NewtonGD physics.
Image
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Convex shapes problem

Post by Erwin Coumans »

Bullet should be able to handle convex shapes fine.

Can you share the vertices? Either in a .cpp file, Wavefront .obj or a .bullet file will do.
Thanks,
Erwin
morando
Posts: 9
Joined: Wed Jun 27, 2012 7:51 am

Re: Convex shapes problem

Post by morando »

Here is bullet file of bottle model from image:
SPAM removed

Don't know (yet) how to convert my model (.x format) to obj.

Give me 15 minutes more time i will upload source and test Release binary of a demo.

SPAM to sendspace removed
morando
Posts: 9
Joined: Wed Jun 27, 2012 7:51 am

Re: Convex shapes problem

Post by morando »

I have a bug with compound bodies, however not related to my original problem (bottle mesh). When i create each body peace for compound i add it to world witch i should not. I need only to add final\compound body. :oops:
Here is a fixed src. files:

SPAM to sendspace removed
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Convex shapes problem

Post by Erwin Coumans »

Please remove the links to external websites with advertising.

You should be able to attach a zipfile (.zip) smaller than 2MB.

Thanks,
Erwin
morando
Posts: 9
Joined: Wed Jun 27, 2012 7:51 am

Re: Convex shapes problem

Post by morando »

Sorry. I didn't see i could upload it here too.
I have scaled down texture to fit 2mb limit and uploaded it all in one zip file.

Thank you for your time.
Attachments
All.zip
(990.83 KiB) Downloaded 297 times
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Convex shapes problem

Post by Erwin Coumans »

The .obj file loads just fine in the Bullet/Demos/ConvexDecompositionDemo (just replace file.obj with your .obj).

You are feeding the wrong vertices into Bullet, please double check.
Thanks,
Erwin
morando
Posts: 9
Joined: Wed Jun 27, 2012 7:51 am

Re: Convex shapes problem

Post by morando »

Yes, its my fault. I rechecked again, and it apeirs that number of bytes per vertex of a .x model was 88 :shock: instead 32 as i was expecting.
There is something wrong with exporter that i am using.

Changed code to this and it works now:

Code: Select all

DWORD numVertices  = mesh->getMesh()->GetNumVertices();
	int numBytesPerVert = mesh->getMesh()->GetNumBytesPerVertex();

	BYTE* pVertices;
	btVector3* btVertices = new btVector3[numVertices];
	mesh->getMesh()->LockVertexBuffer(0, (LPVOID*)&pVertices);
	for(DWORD i = 0; i < numVertices; ++i)
	{
		btVertices[i] = btVector3(
			*((btScalar*)pVertices), 
			*((btScalar*)(pVertices + sizeof(float))), 
			*((btScalar*)(pVertices + sizeof(float) * 2))
			);
		pVertices += numBytesPerVert;
	}
	mesh->getMesh()->UnlockVertexBuffer();
Thank you.
Post Reply