problems with my mario clone using bullet physics

aked
Posts: 4
Joined: Wed Dec 18, 2013 5:23 pm

problems with my mario clone using bullet physics

Post by aked »

I am trying to create a mario clone with bullet physics,however,there is something wrong with my mario movement, when mario fall down on the ground ,the y position seems like this:
0.1
0.2
0.3
...
8.0
8.1
8.2
8.5 -- this is wierd,since 8.2 is when mario hit the ground
8.2 -- then back to 8.2 which is normal
8.2
8.2
8.2

so the screenshot looks like this: when mario hit the ground, he just go into the ground a little bit normal, then back to normal.

here is the initialize code:

Code: Select all

mv_broadphase = new btDbvtBroadphase();

    //gerenic
    mvDefaultCollisionConfiguration = new btDefaultCollisionConfiguration();
    //mv_broadphase                   = new btDbvtBroadphase();
    mv_constraintSolver             = new btSequentialImpulseConstraintSolver();
    mv_dispatcher                   = new btCollisionDispatcher ( mvDefaultCollisionConfiguration );


    btVoronoiSimplexSolver* simplex = new btVoronoiSimplexSolver();
    btMinkowskiPenetrationDepthSolver* pdSolver = new btMinkowskiPenetrationDepthSolver();


    btConvex2dConvex2dAlgorithm::CreateFunc* convexAlgo2d = new btConvex2dConvex2dAlgorithm::CreateFunc ( simplex,pdSolver );

    mv_dispatcher->registerCollisionCreateFunc ( CONVEX_2D_SHAPE_PROXYTYPE,CONVEX_2D_SHAPE_PROXYTYPE,convexAlgo2d );
    mv_dispatcher->registerCollisionCreateFunc ( BOX_2D_SHAPE_PROXYTYPE,CONVEX_2D_SHAPE_PROXYTYPE,convexAlgo2d );
    mv_dispatcher->registerCollisionCreateFunc ( CONVEX_2D_SHAPE_PROXYTYPE,BOX_2D_SHAPE_PROXYTYPE,convexAlgo2d );
    mv_dispatcher->registerCollisionCreateFunc ( BOX_2D_SHAPE_PROXYTYPE,BOX_2D_SHAPE_PROXYTYPE,new btBox2dBox2dCollisionAlgorithm::CreateFunc() );


    mvDynamicsWorld                 = new btDiscreteDynamicsWorld ( mv_dispatcher, mv_broadphase, mv_constraintSolver, mvDefaultCollisionConfiguration );



    


    mvDynamicsWorld->setDebugDrawer ( &sDebugDrawer );
and the mario is just a box created by this function:

Code: Select all

btRigidBody* addBox ( float x,float y,float width,float height,float friciton,float restitution,float mass,bool in3D )
{
    btRigidBody * body;
    btConvexShape* shape;

    // shape = new btConvex2dShape ( new btBoxShape ( btVector3 ( width *PSF,height *PSF,btScalar ( 0.04 ) ) ) );
    shape = new btConvex2dShape ( new btBoxShape ( btVector3 ( width * PSF,height *PSF, btScalar ( 0.04 ) ) ) );

    //shape->setMargin ( btScalar ( 0.04f ) ); //uncomment this ,same result.

    btVector3 localinertia ( 0,0,0 );

    shape->calculateLocalInertia ( mass,localinertia );

    btTransform defaultTransform;
    defaultTransform.setIdentity();

    btQuaternion defaultRotation;

    defaultRotation.setEuler ( 0,0,0 );

    if ( in3D )
        defaultTransform.setOrigin ( btVector3 ( x * PSF - width  * PSF,-y * PSF + height * PSF,-3 * 61 * PSF ) );
    else
        defaultTransform.setOrigin ( btVector3 ( x * PSF - width * PSF,y * PSF - height * PSF,0 ) );

    defaultTransform.setRotation ( defaultRotation );

    btDefaultMotionState* my_motion_state = new btDefaultMotionState ( defaultTransform );

    btRigidBody::btRigidBodyConstructionInfo rbInfo ( 0,0,0 );

    rbInfo = btRigidBody::btRigidBodyConstructionInfo ( mass,my_motion_state,shape,localinertia );

    rbInfo.m_friction = friciton;
    rbInfo.m_restitution = restitution;


    body = new btRigidBody ( rbInfo );


    body->setLinearFactor ( btVector3 ( 1,1,0 ) );
    body->setAngularFactor ( btVector3 ( 0,0,0 ) );


    body->setActivationState ( DISABLE_DEACTIVATION );

    body->setCenterOfMassTransform ( defaultTransform );

    //add to the world ha
    mvDynamicsWorld->addRigidBody ( body );

    
also there is a #define PSF 0.01 from header file,to scale everything a little bit then scale it back by / PSF
and I have to changed the world gravity to -40,to let mario fall a little bit faster,but when I change to -10 ,nothing changes,mario always fall into the ground a little bit ,then back to normal,what param do I need to change? thanks,here is an image that I captured,when mario fall into the ground.
Image
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: problems with my mario clone using bullet physics

Post by Basroil »

Bullet (and most 3d physics libraries) use collision margins on the shapes to improve performance and stability. Add in the fact that penetration corrections can only happen every frame (rather than continuously), and you get a small offset for most purposes.

Considering mario and most other 2d scrollers don't need full physics, you could just use continuous collision detection and update velocities manually. It's faster and lets you do wildly unrealistic things like flight and jumping on shells (without flying off them).