Search code examples
physicssimulationgame-physicsphysics-enginebulletphysics

Physical simulation of realistic two-legged skeleton


I wish to utilize bullet-physics or similar physical-engine to create a realistic skeleton simulation of human-like body with two legs. That is, create a simulation of "a body" made of round mass on top of two "legs", where each leg is made of 3 solid pieces connected through 3 joints and each joint have some degrees of freedom and a limited movement range in each direction, similar to human hip, knee and ankle.

I aim for a realistic model, and hence it will 'stand' only if all joints are balanced correctly and it will fall otherwise.

Any directions, suggestion or pointers to existing tutorials or resources is appreciated! This looks like an awful lot of work doing from scratch...


Solution

  • I'm working on similar code at the moment. My approach is use the Bullet Physics Rag Doll Demo as a starting point. It has a rag doll with body parts connected by joints.

    I'm then using the Bullet Physics Dynamic Control Demo to learn to bend the joints. The challenging part at the moment is setting all the parameters.

    I suggest you learn how to create two rigid bodies connected by a constraint and then to activate the constraint motor to bend the joint.

    The following is some code that I'm working with to learn how rigid bodies and constraints work in Bullet Physics. The code creates two blocks connected by a hinge constraint. The update function bends the hinge constraint slowly over time.

    Now that I've got this I'll be going back to the Rag Doll and adjusting the joints.

    class Simple
    {
    private:
        btScalar targetAngle;
    
        btCollisionShape* alphaCollisionShape;
        btCollisionShape* bravoCollisionShape;
        btRigidBody* alphaRigidBody;
        btRigidBody* bravoRigidBody;
        btHingeConstraint* hingeConstraint;
    
        btDynamicsWorld* dynamicsWorld;
    
    public:
        ~Simple( void )
        {
        }
    
        btRigidBody* createRigidBody( btCollisionShape* collisionShape, 
                      btScalar mass, 
                      const btTransform& transform ) const
        {
        // calculate inertia
        btVector3 localInertia( 0.0f, 0.0f, 0.0f );
        collisionShape->calculateLocalInertia( mass, localInertia );    
    
        // create motion state
        btDefaultMotionState* defaultMotionState 
            = new btDefaultMotionState( transform );
    
        // create rigid body
        btRigidBody::btRigidBodyConstructionInfo rigidBodyConstructionInfo( 
            mass, defaultMotionState, collisionShape, localInertia );
        btRigidBody* rigidBody = new btRigidBody( rigidBodyConstructionInfo );      
    
        return rigidBody;
        }
    
        void Init( btDynamicsWorld* dynamicsWorld )
        {
        this->targetAngle = 0.0f;
    
        this->dynamicsWorld = dynamicsWorld;
    
        // create collision shapes
        const btVector3 alphaBoxHalfExtents( 0.5f, 0.5f, 0.5f );
        alphaCollisionShape = new btBoxShape( alphaBoxHalfExtents );
    
        //
        const btVector3 bravoBoxHalfExtents( 0.5f, 0.5f, 0.5f );
        bravoCollisionShape = new btBoxShape( bravoBoxHalfExtents );
    
        // create alpha rigid body
        const btScalar alphaMass = 10.0f;
        btTransform alphaTransform;
        alphaTransform.setIdentity();
        const btVector3 alphaOrigin( 54.0f, 0.5f, 50.0f );  
        alphaTransform.setOrigin( alphaOrigin );
        alphaRigidBody = createRigidBody( alphaCollisionShape, alphaMass, alphaTransform );
        dynamicsWorld->addRigidBody( alphaRigidBody );
    
        // create bravo rigid body
        const btScalar bravoMass = 1.0f;
        btTransform bravoTransform;
        bravoTransform.setIdentity();
        const btVector3 bravoOrigin( 56.0f, 0.5f, 50.0f );  
        bravoTransform.setOrigin( bravoOrigin );
        bravoRigidBody = createRigidBody( bravoCollisionShape, bravoMass, bravoTransform );
        dynamicsWorld->addRigidBody( bravoRigidBody );
    
        // create a constraint
        const btVector3 pivotInA( 1.0f, 0.0f, 0.0f );   
        const btVector3 pivotInB( -1.0f, 0.0f, 0.0f );
        btVector3 axisInA( 0.0f, 1.0f, 0.0f );
        btVector3 axisInB( 0.0f, 1.0f, 0.0f );
        bool useReferenceFrameA = false;
        hingeConstraint = new btHingeConstraint( 
            *alphaRigidBody,
            *bravoRigidBody,
            pivotInA,
            pivotInB,
            axisInA,
            axisInB,
            useReferenceFrameA );
    
        // set constraint limit
        const btScalar low = -M_PI;
        const btScalar high = M_PI;
        hingeConstraint->setLimit( low, high );
    
        // add constraint to the world
        const bool isDisableCollisionsBetweenLinkedBodies = false;
        dynamicsWorld->addConstraint( hingeConstraint, 
                          isDisableCollisionsBetweenLinkedBodies );
        }
    
        void Update( float deltaTime )
        {
        alphaRigidBody->activate();
        bravoRigidBody->activate();
    
        bool isEnableMotor = true;
        btScalar maxMotorImpulse = 1.0f; // 1.0f / 8.0f is about the minimum
    
        hingeConstraint->enableMotor( isEnableMotor );
        hingeConstraint->setMaxMotorImpulse( maxMotorImpulse );
    
        targetAngle += 0.1f * deltaTime;
        hingeConstraint->setMotorTarget( targetAngle, deltaTime );  
        }
    
    };