Dynamic bodies collision
Posted: Mon Aug 03, 2015 8:38 pm
I'm implementing Box2D for my 2d top down LIBGDX game and i have some struggle with two dynamic body. Currently, my two bodies are created that way :
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(this.x, this.y);
this.body = world.createBody(bodyDef);
PolygonShape collider = new PolygonShape();
collider.setAsBox(16, 16);
FixtureDef fDef = new FixtureDef();
fDef.density = 0f;
fDef.friction = 0f;
fDef.restitution = 0f;
fDef.shape = collider;
body.createFixture(fDef);
body.setUserData(this);
collider.dispose();
So basicly, no rotation, no friction and no restitution. Also, my world have a gravity of 0 (since it's a 2d top down game, i don't that want them to fall). When player 1 collide with player 2, player 2 is moved backward like it was push but i don't want that. I want it to stay where he is, blocking the path to player 1 but he can also move.
I've tried to put both of them as sensor, they go through the walls (which is bad) and they didn't collide against each other. I can catch the collision in a collision listener (or something like that) but is it the good way ?
Is there a way to do that ? I have looked for some tips like catching collision with the contact listener but i don't know what to do in the preSolve method.
I'd like some help, thanks !
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(this.x, this.y);
this.body = world.createBody(bodyDef);
PolygonShape collider = new PolygonShape();
collider.setAsBox(16, 16);
FixtureDef fDef = new FixtureDef();
fDef.density = 0f;
fDef.friction = 0f;
fDef.restitution = 0f;
fDef.shape = collider;
body.createFixture(fDef);
body.setUserData(this);
collider.dispose();
So basicly, no rotation, no friction and no restitution. Also, my world have a gravity of 0 (since it's a 2d top down game, i don't that want them to fall). When player 1 collide with player 2, player 2 is moved backward like it was push but i don't want that. I want it to stay where he is, blocking the path to player 1 but he can also move.
I've tried to put both of them as sensor, they go through the walls (which is bad) and they didn't collide against each other. I can catch the collision in a collision listener (or something like that) but is it the good way ?
Is there a way to do that ? I have looked for some tips like catching collision with the contact listener but i don't know what to do in the preSolve method.
I'd like some help, thanks !