Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've been trying to get Mario type jumping/landing in a Box2d game and it's been giving me some trouble.

Results: Player loses velocity on landing

Desired Results: When player lands it will continue at the same velocity it was in the air.

[edit] Player loses velocity on impact, but quickly regains its set max speed.

I know this is an issue with friction and I've tried setting the friction to 0 when the user is pressing Left and Right movement keys and back to normal friction when the player is not pressing those keys. This however, does not work as the state does not get changed until the collision is over and the player will be stuck sliding until the collision state has changed.

My next best guess is to set friction to zero all together for the player and simulating an opposing force on the player.

Solution? Tips? Heck... maybe there's a tutorial out there I missed.

Thanks in advance.

Example http://www.guioui.com/saxattack/box2d_landing.png

        Vector2 a = entityBody.getLinearVelocity();
        float force = 0;


        if(Gdx.input.isKeyPressed(Keys.D)){
            if(a.x < 5) force = 25;
            //entityBody.getFixtureList().get(0).setFriction(0);
            //floor.entityBody.getFixtureList().get(0).setFriction(0);
            //entityBody.resetMassData();

            //entityBody.applyLinearImpulse(new Vector2(0f,1f), entityBody.getWorldCenter());
            //entityBody.applyForce(new Vector2(0f,50f), entityBody.getWorldCenter());
        }
        else if(Gdx.input.isKeyPressed(Keys.A)){
            if(a.x > -5) force = -25;
            //entityBody.getFixtureList().get(0).setFriction(0);
            //floor.entityBody.getFixtureList().get(0).setFriction(0);
            //entityBody.resetMassData();

            //entityBody.applyForce(new Vector2(0f,50f), entityBody.getWorldCenter());
        }
        else{
            entityBody.getFixtureList().get(0).setFriction(.4f);
            floor.entityBody.getFixtureList().get(0).setFriction(.4f);
        }

        if(Gdx.input.isKeyPressed(Keys.W)){
            a.y = 5;
            entityBody.setLinearVelocity(a);
        }

        entityBody.applyForce(new Vector2(force, 0), entityBody.getWorldCenter());  
share|improve this question
7  
For what it's worth, making a mario-like jump in a 2D physics engine is a total nightmare. It's possible, but a true struggle to make it feel right, because the truth is, Mario's jumps weren't physically "real" at all. Higher jumps when you hold down longer while in-air, steering while in-air, etc. –  Cameron Fredman Mar 8 '13 at 2:54
    
haha, tell me about it :) ... but minus the player movement, which I want to have strict control over, the rest of the game will be physics based. –  mjrevel Mar 8 '13 at 3:30
4  
I don't know about Box2D, but some physics engines have the concept of a "kinematic" rigid body which is something that you control directly that interacts with the rest of the physics world. So you could control movement directly but still have it somewhat work with the physics engine. –  Tetrad Mar 8 '13 at 17:13
    
Yes try kinematic... –  Mikael Högström Mar 18 '13 at 22:16
add comment

1 Answer 1

I would only use the physics input for what should react to the physics. As long as you can keep track of solid surfaces and your character or take advantage of events, I would write your own code to handle player actions, instead of trying to use Box2D to make it happen.

It will probably be more worth your time creating your own code than to tinker with Box2D to get working.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.