Search code examples
c++game-physicsphysics-enginebulletphysics

Bullet Physics: Body moves after fall (shakes and moves to the side)


I have a problem that I'm strugling to solve for a few days.

I'm trying to make a bowling game using bullet physics, but the pin shakes, jiggles and moves to the side after I position it and it falls to the floor.

Here is a GIF of what happens: https://i.sstatic.net/mj5ex.jpg

Here is how I create a Pin:

btCollisionShape* shape = createShape(pinVertices);
btScalar bodyMass = 1.6f;
btVector3 bodyInertia(0,0,0);
shape->calculateLocalInertia(bodyMass, bodyInertia);

btRigidBody::btRigidBodyConstructionInfo bodyCI = btRigidBody::btRigidBodyConstructionInfo(bodyMass, nullptr, shape, bodyInertia);
bodyCI.m_restitution = 0.7;
bodyCI.m_friction = 0.9f;

_physicsBody = std::unique_ptr<btRigidBody>(new btRigidBody(bodyCI));
_physicsBody->setUserPointer(this);

And here is how I create a floor:

btCollisionShape* shape = createShape(laneVertices);
btScalar bodyMass = 0.0f;
btVector3 bodyInertia(0,0,0);
shape->calculateLocalInertia(bodyMass, bodyInertia);

btRigidBody::btRigidBodyConstructionInfo bodyCI = btRigidBody::btRigidBodyConstructionInfo(bodyMass, nullptr, shape, bodyInertia);
bodyCI.m_restitution = 0.6;
bodyCI.m_friction = 0.5;

_physicsBody = std::unique_ptr<btRigidBody>(new btRigidBody(bodyCI));
_physicsBody->setUserPointer(this);

Right now the floor is a btBoxShape and a pin is a btConvexHullShape, but I've tried using cylinder or cone and they also slide.

Been struggling for few days especially taking into account Bullet Physics website and forum are down.


Solution

  • Perhaps you should set the restitution (bounce) of the pin and floor to something lower (try first with 0.0) This should solve it if the pin is bouncing.

    Another thing you could try is to deactivate the pin after creating it. I don't know in Bullet, but in JBullet it's done like this:

    body.setActivationState( CollisionObject.WANTS_DEACTIVATION );
    

    This will stop your pin until some other object like the ball or other pin hits it.