Question

I have a Box2D World which contains 10 Bodies.
1 Body is STATIC, which is the ground, the rest are all DYNAMIC. They are all Rectangles, with varying dimensions.
1 Rectangle acts as the Player, when you press left/right that body moves and you can use space bar to jump.

All objects have a restitution of 0.3f, which I feel is realistic.
When the Player lands after jumping, they gradually loose speed and remain on the ground as though they're standing. This is the behaviour if Player lands on any of the other bodies in the World.

Id like to be able to define a new object, a Spring.
When the Player jumps on it, they are projected into the air with same force that they landed on it with e.g using restitution of 1.0f
However, If the Spring falls off a cliff, I don't want it to bounce.

What would be a suitable way to implement this?

Was it helpful?

Solution

Used this in the end ..

public void preSolve(Contact contact, Manifold oldManifold) {

        if (contact.getFixtureA().getBody().getUserData() instanceof Spring){

            float impulse = contact.getFixtureB().getBody().getMass() * impulseToApply; 

            contact.getFixtureB().getBody().applyLinearImpulse( 
                    new Vec2(0, impulse), 
                    contact.getFixtureB().getBody().getWorldCenter() );

        }else if (contact.getFixtureB().getBody().getUserData() instanceof Spring){

            float impulse = contact.getFixtureA().getBody().getMass() * impulseToApply; 

            contact.getFixtureA().getBody().applyLinearImpulse( 
                    new Vec2(0, impulse), 
                    contact.getFixtureA().getBody().getWorldCenter() );
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top