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());