btCompoundShape problem?

Post Reply
radubolovan
Posts: 7
Joined: Mon Dec 22, 2008 3:13 pm

btCompoundShape problem?

Post by radubolovan »

Hello,

I am trying to add some btCompoundShape to my app and looks like it doesn't work.
I made a siple test, please see the code and screenshot below:

--> Setup the world:

Code: Select all

m_broadphase = snew btDbvtBroadphase();
m_collision_configuration = snew btDefaultCollisionConfiguration();
m_dispatcher = snew btCollisionDispatcher(m_collision_configuration);
m_solver = new btSequentialImpulseConstraintSolver;
m_dynamics_world = new btDiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_solver, m_collision_configuration);
m_dynamics_world->setGravity(btVector3(0.f,-9.8f,0.f));

--> adding 2 objects: static ground & 1 compound:

Code: Select all

//general variables
float mass = 1.f;
btVector3 localInertia( 0.f, 0.f, 0.f );
btTransform	transform;

//ground
btCollisionShape* ground_shape = new btBoxShape( btVector3( 1000.f, 0.1f, 1000.f ) );
m_collisionShapes.push_back( ground_shape );

transform.setIdentity();
btDefaultMotionState* ground_motion_state = new btDefaultMotionState( transform );
btRigidBody::btRigidBodyConstructionInfo ground_cInfo( 0.f, ground_motion_state, ground_shape, localInertia );
btRigidBody* ground_body = new btRigidBody( ground_cInfo );
ground_body->setFriction( 1.f );
ground_body->setRollingFriction( 1.f );
m_dynamics_world->addRigidBody( ground_body );

//compound
btCompoundShape* colShape = new btCompoundShape();

//box
btCollisionShape* boxShape = new btBoxShape( btVector3( 0.5f, 0.5f, 0.5f ) );

//add box #1
transform.setIdentity();
transform.setOrigin( btVector3( 0.f, 5.f, 0.f ) );
colShape->addChildShape( transform, boxShape );

//add box #2
transform.setIdentity();
transform.setOrigin( btVector3( 1.f, 5.f, 0.f ) );
colShape->addChildShape( transform, boxShape );

//compute inertia
transform.setIdentity();
transform.setOrigin( btVector3( 0, 5, 0 ) );
colShape->calculateLocalInertia( mass, localInertia );

m_collisionShapes.push_back( colShape );

//create rigid body and add it to the world
btDefaultMotionState* comp_motion_state = new btDefaultMotionState( transform );
btRigidBody::btRigidBodyConstructionInfo comp_cInfo( mass, comp_motion_state, colShape, localInertia );
btRigidBody* comp_body = new btRigidBody( comp_cInfo );
comp_body->setActivationState( DISABLE_DEACTIVATION );
m_dynamics_world->addRigidBody( comp_body );

--> update the world:

Code: Select all

m_dynamics_world->stepSimulation(1.f/60.f);
m_dynamics_world->updateAabbs();

--> render: I'm using my own render system

Code: Select all

btVector3 min, max;
myAABB aabb;
btTransform child_trans;
btVector3 child_min, child_max;
int i, j;
for(i = 0; i < m_collisionShapes.size(); i++)
{
	btCollisionShape* shape = m_collisionShapes[i];
	if(shape->getShapeType() == COMPOUND_SHAPE_PROXYTYPE)
	{
		btCompoundShape* shape = (btCompoundShape*)m_collisionShapes[i];
		for(j = shape->getNumChildShapes()-1; j >= 0; j--)
		{
			const btCollisionShape* col_shape = shape->getChildShape(j);
			child_trans = shape->getChildTransform(j);
			col_shape->getAabb(child_trans, child_min, child_max);
			aabb.Make(enVec3(child_min.getX(), child_min.getY(), child_min.getZ()), enVec3(child_max.getX(), child_max.getY(), child_max.getZ()));
			my_render->DrawAABB(aabb);
		}
	}
}

--> screenshot
Image

Can anyone tell me what I do wrong, please?

Thanks in advance,
Radu
papaonn
Posts: 41
Joined: Wed Nov 20, 2013 4:14 pm

Re: btCompoundShape problem?

Post by papaonn »

Hi,

Just a guess,

getChildTransform() is it a local transform?

perhaps you might want to draw a rect and update the position without using AABB from getChildTransform() to run a test first, or could you trace / printf the position of the compound shape?

OR

you might want to place the object higher in the air to see if it drops / update position.

Because the setup of rigid body for the compound shape doesn't seem to have problem.
(Or I might miss out something).
radubolovan
Posts: 7
Joined: Mon Dec 22, 2008 3:13 pm

Re: btCompoundShape problem?

Post by radubolovan »

Thanks for your reply.

From the traces I see that getChildTransform() is not local:

Code: Select all

child position: (1 5 0)
child position: (0 5 0)
child position: (1 5 0)
child position: (0 5 0)
child position: (1 5 0)
child position: (0 5 0)
child position: (1 5 0)
child position: (0 5 0)
child position: (1 5 0)
child position: (0 5 0)
These ^^ are main positions of the children of the compound. So drawing 2 boxes on these values will not gonna help much.

And adding the objects higher will not help either. There are 5 units to drop from since the objects are at 5.0 on y and the ground on 0.0.

Looks like the compound and/of the rigid body (of the compound) doesn't have physics at all. No gravity, no collisions. This is giving me a headache for ~1 week now =))

I added a simple btBoxShape and it is falling as expected:

Code: Select all

position: (0 4.99728 0)
position: (0 4.99183 0)
position: (0 4.98367 0)
position: (0 4.97278 0)
position: (0 4.95917 0)
position: (0 4.94283 0)
position: (0 4.92378 0)
position: (0 4.902 0)
position: (0 4.8775 0)
position: (0 4.85028 0)
position: (0 4.82033 0)
position: (0 4.78767 0)
position: (0 4.75228 0)
position: (0 4.71417 0)
position: (0 4.67333 0)
position: (0 4.62978 0)
position: (0 4.5835 0)
position: (0 4.5345 0)
radubolovan
Posts: 7
Joined: Mon Dec 22, 2008 3:13 pm

Re: btCompoundShape problem?

Post by radubolovan »

I added the bodies(btRigidBody) to a a list and this is fine for the compound object:

btVector3 pos = m_bodies->getCenterOfMassPosition();

So only children of the compound shape don't update properly ...
papaonn
Posts: 41
Joined: Wed Nov 20, 2013 4:14 pm

Re: btCompoundShape problem?

Post by papaonn »

Hi,

To what my understanding is that collisionShape is only a shape for collision checking purpose, it doesn't automatically update is it? for rigid body it handles the update position after impulse response etc. Hence i am guessing u could see the update after adding it to rigid body, actually my suggestion is to add all the things u need into the rigid body list, so to let the bullet tackle when to wake up the call for collision checking and etc.

the alternative is to update the orientation of child collisionShape manually with reference to the parents.

Hope it helps, just some of my guess =)
Karrok
Posts: 65
Joined: Fri May 13, 2011 1:11 pm

Re: btCompoundShape problem?

Post by Karrok »

if the problem is in the drawing:

I've recently updated my local bullet with a child-drawing function myself and it seems to work (tested only locally and with forklift demo)

Image

Code: Select all

if (colObj->getCollisionShape()->isCompound() )
{
  btCompoundShape * compoundshape = static_cast<btCompoundShape*>(colObj->getCollisionShape());
  int childshapes = compoundshape->getNumChildShapes();
  btVector3 child_color(0,1,0); // green
  for ( int j = 0; j < childshapes ; j++)
  {
    compoundshape->getChildShape(j)->getAabb(colObj->getWorldTransform() * compoundshape->getChildTransform(j) ,minAabb,maxAabb);
    minAabb -= contactThreshold;
    maxAabb += contactThreshold;
    minAabb.setMin(minAabb);
    maxAabb.setMax(maxAabb);
    m_debugDrawer->drawAabb(minAabb,maxAabb,child_color);
  }
}



edit:

Keep in mind the code above is obviously not recursively applied. But if you require that I'm sure you can figure out how to do that ;-)
radubolovan
Posts: 7
Joined: Mon Dec 22, 2008 3:13 pm

Re: btCompoundShape problem?

Post by radubolovan »

Looks like if I multiply colObj->getWorldTransform() with compoundshape->getChildTransform(j), the compound object (children) is falling down as expected. So the problem was in my draw function indeed.

However, after the compound object is hitting the ground crazy stuff is going on. I wish I could make a movie.
I'll investigate more.

Thank you @Karrok!



edit:
I found the second problem too: I was building the compound with wrong coordinates for children.
Post Reply