Incorrect aabb in btUniformScalingShape of btConvexHullShape

Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Incorrect aabb in btUniformScalingShape of btConvexHullShape

Post by Flix »

I'm not 100% sure about it but think I've discovered that in some cases the aabb of an uniform scaling shape containing a convex hull shape can be wrong, like in this example:

Code: Select all

	{
		const btScalar x=1;
		const btScalar y=1;
		const btScalar z=1;	
		const btScalar ox=1;	// offset
		const btScalar oy=1;	// offset
		const btScalar oz=1;	// offset			
		const btVector3 points[] =	{
			btVector3(ox+x,oy+y,oz+z),
			btVector3(ox-x,oy+y,oz+z),
			btVector3(ox+x,oy-y,oz+z),
			btVector3(ox-x,oy-y,oz+z),
			btVector3(ox+x,oy+y,oz-z),
			btVector3(ox-x,oy+y,oz-z),
			btVector3(ox+x,oy-y,oz-z),
			btVector3(ox-x,oy-y,oz-z)				
		};
		btConvexHullShape* shape = new btConvexHullShape(&points[0].x(),sizeof(points)/sizeof(points[0]));
		//m_collisionShapes.push_back(shape);
		btTransform transform;transform.setIdentity();
		transform.setOrigin(btVector3(-10,2*y,-20));transform.setRotation(btQuaternion(btVector3(0,1,0),btRadians(60)));
		const btScalar mass(1.);
		const bool isDynamic = (mass != 0.f);
		btVector3 localInertia(0,0,0);
		if (isDynamic)	shape->calculateLocalInertia(mass,localInertia);

		btDefaultMotionState* myMotionState = new btDefaultMotionState(transform);
		btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,shape,localInertia);
		btRigidBody* body = new btRigidBody(rbInfo);
		
		btWorld->addRigidBody(body);	// aabb OK
		
		// Clone with uniform scaling:
		{
			btUniformScalingShape* scaledShape = new btUniformScalingShape(shape,2);
			//m_collisionShapes.push_back(shape);
			const btScalar mass(2.);
			transform.setOrigin(btVector3(10,4*y,-20));	
			
			
			const bool isDynamic = (mass != 0.f);
			btVector3 localInertia(0,0,0);
			if (isDynamic)	shape->calculateLocalInertia(mass,localInertia);

			btDefaultMotionState* myMotionState = new btDefaultMotionState(transform);
			btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,scaledShape,localInertia);
			btRigidBody* body = new btRigidBody(rbInfo);
		
			btWorld->addRigidBody(body);	// aabb too small!	
		}
			
	}
Here is the Bullet debug drawer aabb (in red):
wrongAabbForUniformScalingConvexHullShape.png
I know that probably the problem in the example is that the offset added to the convex hull center is not taken into consideration by the uniform scaling shape, but I experience something weird (that I think can be related to this problem) when I "clone" a compound shape by uniform scaling its child convex hull shapes (this time centered in their own center (AFAIK)) together with their child transform origin:
the weird behavior is that the new compound shape (actually bodies using it...) keeps jumping (although sometimes its aabb looks correct when the child shapes are centered in their own center).

In short I would like to be able to make something like a btUniformScalingCompoundShape, and I hope that, if that problem gets solved, I can make it.
You do not have the required permissions to view the files attached to this post.
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Incorrect aabb in btUniformScalingShape of btConvexHullS

Post by Flix »

Flix wrote:I know that probably the problem in the example is that the offset added to the convex hull center is not taken into consideration by the uniform scaling shape, but I experience something weird (that I think can be related to this problem) when I "clone" a compound shape by uniform scaling its child convex hull shapes (this time centered in their own center (AFAIK)) together with their child transform origin:
the weird behavior is that the new compound shape (actually bodies using it...) keeps jumping (although sometimes its aabb looks correct when the child shapes are centered in their own center).
I discovered that that happened because the child convex hull shapes were not centered in the correct point.

So, to summarize the whole thing, from what I'm seeing:
1) btConvexHullShapes work correctly.
2) btUniformScalingShape(btConvexHullShape) works correctly only if the convex hull is centered in the origin.
3) It's better not to use btUniformScalingShape(btConvexHullShape) when the btConvexHullShape is not properly centered in the origin. This is expecially true inside btCompoundShapes (in the current version of Bullet).

However, since point 2 still holds, it's possible to create a particular set of btCompoundShapes that are suitable for being uniformly scaled, like in this image:
uniformScaledCompoundShape.png
Hope this is not a too trivial topic...
You do not have the required permissions to view the files attached to this post.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Incorrect aabb in btUniformScalingShape of btConvexHullS

Post by Erwin Coumans »

Thanks for the report, I found a bug and fixed it in latest trunk.

Can you try it (or attached file) and see if that works for you?
Thanks,
Erwin
You do not have the required permissions to view the files attached to this post.
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Incorrect aabb in btUniformScalingShape of btConvexHullS

Post by Flix »

Erwin Coumans wrote:Can you try it (or attached file) and see if that works for you?
I just made a single test and it seems to work now. I tested a compound shape made of uniform scaling shapes of convex hulls not centered in the proper origin and it seems to work good.

I consider the issue fixed for now. I will make further tests in the future (the debug aabb test posted above and "uniformly scaled" compounds using other models/offsets), and if I'll find something unusual I will report it.

Thanks for your quick help, Erwin :wink:
Flix