Hello, I want to implement the jumpthru platform. I use box2d, I create two objects in it: a platform (kinematicBody) and a player (dynamicBody);

Platform:

public Bucket(float x,float y, World world){ this.world = world; BodyDef bdef = new BodyDef(); bucket = new ChainShape(); FixtureDef fdef = new FixtureDef(); velocity = new Vector2(); velocity.x = x/PPM; velocity.y = y/PPM; bdef.position.set(velocity.x,velocity.y); bdef.type = BodyDef.BodyType.StaticBody; bucket.createChain(new float[]{ -36/PPM,22/PPM,-23/PPM,0,23/PPM,0,36/PPM,22/PPM }); fdef.shape = bucket; body = world.createBody(bdef); fixture = body.createFixture(fdef); fixture.setUserData("bucket"); body.setLinearVelocity(1,0); bucket.dispose(); } 

Player:

 public class Ball implements ContactFilter { private World world; private Body body; private Fixture fixture; private CircleShape circle; public Ball(float y,World world){ this.world = world; BodyDef bdef = new BodyDef(); FixtureDef fdef = new FixtureDef(); circle = new CircleShape(); bdef.type = BodyDef.BodyType.DynamicBody; bdef.position.set(W/2/PPM,y/PPM); circle.setRadius(14/PPM); fdef.shape = circle; fdef.friction = 1f; body = world.createBody(bdef); fixture = body.createFixture(fdef); fixture.setUserData("ball"); circle.dispose(); } public Body getBody() { return body; } public Fixture getFixture() { return fixture; } public CircleShape getCircle() { return circle; } @Override public boolean shouldCollide(Fixture fixtureA, Fixture fixtureB) { if(fixtureA == fixture || fixtureB == fixture){ return body.getLinearVelocity().y < 0; } return false; } 

}

I use the ContactFilter interface, but I can’t understand why a player doesn’t go through the platforms, I didn’t find examples on the Internet about this interface.

    1 answer 1

    Here is an excellent tutorial on this topic, though in English - https://www.youtube.com/playlist?list=PLXY8okVWvwZ2Ph9LKWiNBZ1GRAc_TyDru And here's the part that addresses your problem - https://www.youtube.com/watch?v= OFps-aYsl2g & index = 11 & list = PLXY8okVWvwZ2Ph9LKWiNBZ1GRAc_TyDru In your case, it should look something like this:

    @Override public boolean shouldCollide(Fixture fixtureA, Fixture fixtureB) { return body.getLinearVelocity().y >= 0 && !(fixtureB.getUserData().equals("bucket") || fixtureA.getUserData().equals("bucket")) }