btBoxShape not behaving as expected

Post Reply
self.adilPatel.name
Posts: 8
Joined: Mon Jul 29, 2013 10:21 am

btBoxShape not behaving as expected

Post by self.adilPatel.name »

So I'm new to bullet physics, and I'm trying to first simulate a cuboid, then I'll move to more complex stuff.

As an overview, the cuboid is of dimensions 2 x 4 x 1.

So here's how I declared the shape:

Code: Select all

btCollisionShape *blockShape = new btBoxShape(btVector3(1, 2, 0.5));
As a test, I simply placed it 100 units directly above the ground, and let it drop. What happens is that when it falls, it moves to the side (not supposd to do that), and bounces irregularly. It then settles down by going through the ground, where the origin of the block is 1.5 units above the ground (it's supposed to be 2). In case you want the code, here it is:

Code: Select all

//Initialisation code:
    
    //We'll need to set up a proper broadphase interface so it can quickly detect
    //the obvious collisions that won't happen. The btDvbtBroadphase algorithm is the best for this.
    btBroadphaseInterface *broadphase = new btDbvtBroadphase();
    
    //Collision configs allow us to fine tune the collisions. The default one should be good enough
     btDefaultCollisionConfiguration *collisionConfiguration = new btDefaultCollisionConfiguration();
    btCollisionDispatcher *dispatcher = new btCollisionDispatcher(collisionConfiguration);
    
    //The "solver" is what actually resolves the interaction of objects and the environmental
    //parameters
    btSequentialImpulseConstraintSolver *solver = new btSequentialImpulseConstraintSolver;
    
    //The actual game world initialised!
    btDiscreteDynamicsWorld *dynamicWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
    
    dynamicWorld->setGravity(btVector3(0, -9.81, 0));
    
    //Great, now we set up the floor...
    btCollisionShape *floorShape = new btStaticPlaneShape(btVector3(0, 1, 0), 1);
    btDefaultMotionState *floorMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, 0)));
    
    btRigidBody::btRigidBodyConstructionInfo
    //First and last parameters are mass and inertia repectively. Bullet detects a mass of 0
    //as immovable. Do we really want a floor to move?
    floorRigidBodyCI(0, floorMotionState, floorShape, btVector3(0, 0, 0));
    
    btRigidBody *floorRigidBody = new btRigidBody(floorRigidBodyCI);
    
    dynamicWorld->addRigidBody(floorRigidBody);
    
    //Now, we set up the block (but we won't add it to the world. That's for another method).
    btCollisionShape *blockShape = new btBoxShape(btVector3(1, 2, 0.5));
    
    //For the purposes of the demo, we will just use one block.
    btDefaultMotionState *blockMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 100, 0)));
    
    btScalar mass = 50;
    btVector3 blockIntertia(0, 0, 0);
    blockShape->calculateLocalInertia(mass, blockIntertia);
    
    btRigidBody::btRigidBodyConstructionInfo
    blockRigidBodyCI(mass, blockMotionState, blockShape, blockIntertia);
    btRigidBody *blockRigidBody = new btRigidBody(blockRigidBodyCI);
    
    dynamicWorld->addRigidBody(blockRigidBody);
   
//Code to step the simulation (I'm using Apple's GLKit and iOS. Should still be easy to use)

- (GLKVector3)getBLockLocation {

    dynamicWorld->stepSimulation(1/60.f, 10);
    
    btTransform trans;
    blockRigidBody->getMotionState()->getWorldTransform(trans);
    
    return GLKVector3Make(trans.getOrigin().getX(), trans.getOrigin().getY(), trans.getOrigin().getZ());
    
}
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: btBoxShape not behaving as expected

Post by Basroil »

If your boxes fall and bounce, the lowest energy state is with the 1 length axis being vertical, so you will get 1.5 height for the origin on a good deal of boxes. As for moving to the side, is there a graph of the x,y,z values you can put up, or does it just look like it does that due to how cameras are set up?
self.adilPatel.name
Posts: 8
Joined: Mon Jul 29, 2013 10:21 am

Re: btBoxShape not behaving as expected

Post by self.adilPatel.name »

Basroil wrote:If your boxes fall and bounce, the lowest energy state is with the 1 length axis being vertical, so you will get 1.5 height for the origin on a good deal of boxes. As for moving to the side, is there a graph of the x,y,z values you can put up, or does it just look like it does that due to how cameras are set up?
Ok, so I actualy logged my values, and yes, it is actually trying to send my box to the side. One thing I'm asking is: Have I constructed the box correctly? I want a 2x4x1 block, so please check my first post if I have constructed it correctly.

If yes, then how can I make my box settle on the ground? Because after it bounces for an unnatural amount, it sinks in the ground and stays there. Since the y value is 4, having it settle at 1.5 means that it sinks by 0.5 units into the ground.
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: btBoxShape not behaving as expected

Post by Basroil »

Bullet (how you have it set up and default) is Y-up, so your box is 2 wide, 1 deep, and 4 tall. It should rest at either 0.5 (made a typo up top), 1, or 2 above the ground (as taken from the origin).

But now I see your inertia is set to 0 (it's missing a shape->calculateLocalInertia(mass,localInertia) equivalent) so it shouldn't actually rotate making my point moot. A fall from 100 units at 9.8unit-s^-2 means a velocity of ~45unit-s^-1 when it hits, which is about 0.7 units per simulation step. More than even that, if the drop happens at t=0, it should be just outside the collision margin in the step just before the collision step (by carrying out freefall calculations by hand), and it'll be well inside the plane by the next time collisions are called. Maybe the "bounces irregularly" thing wasn't really bouncing, rather penetration corrections. Tried dropping it from 100.5 units or increasing the simulation rate?
self.adilPatel.name
Posts: 8
Joined: Mon Jul 29, 2013 10:21 am

Re: btBoxShape not behaving as expected

Post by self.adilPatel.name »

Basroil wrote:Bullet (how you have it set up and default) is Y-up, so your box is 2 wide, 1 deep, and 4 tall. It should rest at either 0.5 (made a typo up top), 1, or 2 above the ground (as taken from the origin).

But now I see your inertia is set to 0 (it's missing a shape->calculateLocalInertia(mass,localInertia) equivalent) so it shouldn't actually rotate making my point moot. A fall from 100 units at 9.8unit-s^-2 means a velocity of ~45unit-s^-1 when it hits, which is about 0.7 units per simulation step. More than even that, if the drop happens at t=0, it should be just outside the collision margin in the step just before the collision step (by carrying out freefall calculations by hand), and it'll be well inside the plane by the next time collisions are called. Maybe the "bounces irregularly" thing wasn't really bouncing, rather penetration corrections. Tried dropping it from 100.5 units or increasing the simulation rate?
Tried both, but it still didn't help. Also, I have actually calculated the local inertia. And it's still sliding to the side.
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: btBoxShape not behaving as expected

Post by Basroil »

self.adilPatel.name wrote:Tried both, but it still didn't help. Also, I have actually calculated the local inertia. And it's still sliding to the side.
Tried both as in changing

Code: Select all

dynamicWorld->stepSimulation(1/60.f, 10);
to

Code: Select all

dynamicWorld->stepSimulation(1/60.f, 10,1/300.0);
or just changed one of the existing numbers there? How does the box fare when dropped from a very small height like 5 units? If you can put up a video, graphs, serialized export, anything it might help, since the code seems to be set up just like the demos.

If the box is translating other than in the gravity direction before it hits the ground, then there's probably code somewhere that's changing where the box is (outside what's been posted here). If it's only after hitting the ground, it might just be an artifact from the fall.
DonTzzy
Posts: 4
Joined: Tue Sep 13, 2011 11:14 pm
Contact:

Re: btBoxShape not behaving as expected

Post by DonTzzy »

This might be the problem:

Code: Select all

btScalar mass = 50;
Can you try:

Code: Select all

btScalar mass = 1;
self.adilPatel.name
Posts: 8
Joined: Mon Jul 29, 2013 10:21 am

Re: btBoxShape not behaving as expected

Post by self.adilPatel.name »

DonTzzy wrote:This might be the problem:

Code: Select all

btScalar mass = 50;
Can you try:

Code: Select all

btScalar mass = 1;
Same thing happens. I only increased it to 50 in the hopes of removing the irregular bouncing.
self.adilPatel.name
Posts: 8
Joined: Mon Jul 29, 2013 10:21 am

Re: btBoxShape not behaving as expected

Post by self.adilPatel.name »

Basroil wrote:
self.adilPatel.name wrote:Tried both, but it still didn't help. Also, I have actually calculated the local inertia. And it's still sliding to the side.
Tried both as in changing

Code: Select all

dynamicWorld->stepSimulation(1/60.f, 10);
to

Code: Select all

dynamicWorld->stepSimulation(1/60.f, 10,1/300.0);
or just changed one of the existing numbers there? How does the box fare when dropped from a very small height like 5 units? If you can put up a video, graphs, serialized export, anything it might help, since the code seems to be set up just like the demos.

If the box is translating other than in the gravity direction before it hits the ground, then there's probably code somewhere that's changing where the box is (outside what's been posted here). If it's only after hitting the ground, it might just be an artifact from the fall.
Ok, so I've tried increasing the rate, and with no effect.

I have attached links to my google drive, that contains both of the videos: for dropping it from 5 units, and for dropping it from 100 units.

From 5 units, you may be deceived. It's actually floating above the ground when it settles. I know this because I logged it.

Here are the links:
https://docs.google.com/file/d/0BwklWsY ... sp=sharing
https://docs.google.com/file/d/0BwklWsY ... sp=sharing

Sorry for the late reply and thank you very much for your time
self.adilPatel.name
Posts: 8
Joined: Mon Jul 29, 2013 10:21 am

Re: btBoxShape not behaving as expected

Post by self.adilPatel.name »

self.adilPatel.name wrote:
Basroil wrote:
self.adilPatel.name wrote:Tried both, but it still didn't help. Also, I have actually calculated the local inertia. And it's still sliding to the side.
Tried both as in changing

Code: Select all

dynamicWorld->stepSimulation(1/60.f, 10);
to

Code: Select all

dynamicWorld->stepSimulation(1/60.f, 10,1/300.0);
or just changed one of the existing numbers there? How does the box fare when dropped from a very small height like 5 units? If you can put up a video, graphs, serialized export, anything it might help, since the code seems to be set up just like the demos.

If the box is translating other than in the gravity direction before it hits the ground, then there's probably code somewhere that's changing where the box is (outside what's been posted here). If it's only after hitting the ground, it might just be an artifact from the fall.
Ok, so I've tried increasing the rate, and with no effect.

I have attached links to my google drive, that contains both of the videos: for dropping it from 5 units, and for dropping it from 100 units.

From 5 units, you may be deceived. It's actually floating above the ground when it settles. I know this because I logged it.

Here are the links:
https://docs.google.com/file/d/0BwklWsY ... sp=sharing
https://docs.google.com/file/d/0BwklWsY ... sp=sharing

Sorry for the late reply and thank you very much for your time

Nobody wants to help me? Please, I'm trying but I still can't get it to work! I can't give up! :(
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: btBoxShape not behaving as expected

Post by xexuxjy »

sorry - probably obvious question - but have you used debug draw or similar to confirm that the strange behaviour is in bullet, or if it's a mismatch between the physics and visualisation?
self.adilPatel.name
Posts: 8
Joined: Mon Jul 29, 2013 10:21 am

Re: btBoxShape not behaving as expected

Post by self.adilPatel.name »

xexuxjy wrote:sorry - probably obvious question - but have you used debug draw or similar to confirm that the strange behaviour is in bullet, or if it's a mismatch between the physics and visualisation?
I haven't used the debug draw, but I have logged the positions that bullet has calculated. Bullet has indeed calculated the wrong values.Interestingly, when I change it to a btSphereShape, none of these problems happen.
sezaru
Posts: 4
Joined: Tue Jan 08, 2013 1:54 am

Re: btBoxShape not behaving as expected

Post by sezaru »

I'm having the exact same problem, the boxes behave strangely and then simply stops simulating..

Here is my code for initializing it:

Code: Select all

  const auto mass = 1.0;
  
  btTransform transform;
  transform.setIdentity();
  transform.setOrigin(btVector3{position[0] - 0.5f, position[1] - 0.5f, position[2] - 0.5f});

  auto cube = new btBoxShape{btVector3{0.5, 0.5, 0.5}};
  auto inertia = btVector3{0, 0, 0};
  cube->calculateLocalInertia(mass, inertia);

  auto motion = new btDefaultMotionState{transform};
  auto info = btRigidBody::btRigidBodyConstructionInfo{mass, motion, cube, inertia};
  info.m_restitution = 0.8f;
  info.m_friction = 0.2f;
  
  body_ = new btRigidBody{info};

  world_->addRigidBody(body_);
My bullet version is 2.81, as stated above, btSphereShape doesn't have these problems, only btBoxShape.
self.adilPatel.name
Posts: 8
Joined: Mon Jul 29, 2013 10:21 am

Re: btBoxShape not behaving as expected

Post by self.adilPatel.name »

sezaru wrote:I'm having the exact same problem, the boxes behave strangely and then simply stops simulating..

Here is my code for initializing it:

Code: Select all

  const auto mass = 1.0;
  
  btTransform transform;
  transform.setIdentity();
  transform.setOrigin(btVector3{position[0] - 0.5f, position[1] - 0.5f, position[2] - 0.5f});

  auto cube = new btBoxShape{btVector3{0.5, 0.5, 0.5}};
  auto inertia = btVector3{0, 0, 0};
  cube->calculateLocalInertia(mass, inertia);

  auto motion = new btDefaultMotionState{transform};
  auto info = btRigidBody::btRigidBodyConstructionInfo{mass, motion, cube, inertia};
  info.m_restitution = 0.8f;
  info.m_friction = 0.2f;
  
  body_ = new btRigidBody{info};

  world_->addRigidBody(body_);
My bullet version is 2.81, as stated above, btSphereShape doesn't have these problems, only btBoxShape.
Good god it's wonderful to see a reply after so long!

But I still have no solution. I've put my work on hold because of university, but maybe you can try and message the admin, and explain the situation.
Post Reply