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 !
Dynamic bodies collision
Re: Dynamic bodies collision
What you're talking about goes against the way Box2D (and real world physics) works, but you might be able to get something that works well enough - these threads might help:
http://www.box2d.org/forum/viewtopic.php?f=3&t=7501
http://www.box2d.org/forum/viewtopic.php?f=3&t=7897
http://stackoverflow.com/questions/2093 ... each-other
http://www.box2d.org/forum/viewtopic.php?f=3&t=7501
http://www.box2d.org/forum/viewtopic.php?f=3&t=7897
http://stackoverflow.com/questions/2093 ... each-other
Re: Dynamic bodies collision
Hello and thanks,
Sorry for the late answer.
I'm trying to understand your answers on the different links and i think i've managed to understand it but i'm facing a problem.
If i understand correctly each player need to have 2 body (a dynamic body and a kinematic). Those 2 bodies should not collide against each other BUT every dynamic body (so for example the dynamic body of the player 1 and the dynamic body of the player 2 should not collide against each other).
If i represent that as code i can do the "no collision against a body and his shadow" but not the "no collision against his shadow AND every dynamic body".
I'm doing it like that :
http://pastebin.com/Hh0wBvKP
Any clue on how to do it, thanks !
Sorry for the late answer.
I'm trying to understand your answers on the different links and i think i've managed to understand it but i'm facing a problem.
If i understand correctly each player need to have 2 body (a dynamic body and a kinematic). Those 2 bodies should not collide against each other BUT every dynamic body (so for example the dynamic body of the player 1 and the dynamic body of the player 2 should not collide against each other).
If i represent that as code i can do the "no collision against a body and his shadow" but not the "no collision against his shadow AND every dynamic body".
I'm doing it like that :
http://pastebin.com/Hh0wBvKP
Any clue on how to do it, thanks !
Re: Dynamic bodies collision
First I should mention that there is actually a working source code example at the second link:
http://www.box2d.org/forum/viewtopic.ph ... 897#p34145
I'm a bit confused about the point you're describing, so let's make it clearer. Suppose we have two bodies A and B which we want to do this mutual non-bumping for. They will each need a shadow (kinematic) body, which we will call a and b respectively. The collisions we want to have between these four bodies are:
A b
B a
... and the collisions we don't want would be:
A a
B b
A B
Note that a and b will not collide with each other to begin with by nature of being kinematic bodies.
This is not a terribly complicated situation and it should be possible to set it up using the category and mask bits of the fixtures involved, as shown in: http://www.iforce2d.net/b2dtut/collision-filtering
To have larger numbers of bodies doing this, you will need another way of doing it because of the limited number of bits in the category and mask bits variables. In that case you could decide in the PreSolve callback whether the given two bodies should collide, and if not you can do contact->SetEnabled(false). There is also the b2ContactFilter::ShouldCollide method for setting up a callback function that Box2D will call every time it is deciding whether two fixtures should collide. This is mentioned briefly near the end of the tutorial I linked to above, let me know if the usage is not clear.
http://www.box2d.org/forum/viewtopic.ph ... 897#p34145
I'm a bit confused about the point you're describing, so let's make it clearer. Suppose we have two bodies A and B which we want to do this mutual non-bumping for. They will each need a shadow (kinematic) body, which we will call a and b respectively. The collisions we want to have between these four bodies are:
A b
B a
... and the collisions we don't want would be:
A a
B b
A B
Note that a and b will not collide with each other to begin with by nature of being kinematic bodies.
This is not a terribly complicated situation and it should be possible to set it up using the category and mask bits of the fixtures involved, as shown in: http://www.iforce2d.net/b2dtut/collision-filtering
To have larger numbers of bodies doing this, you will need another way of doing it because of the limited number of bits in the category and mask bits variables. In that case you could decide in the PreSolve callback whether the given two bodies should collide, and if not you can do contact->SetEnabled(false). There is also the b2ContactFilter::ShouldCollide method for setting up a callback function that Box2D will call every time it is deciding whether two fixtures should collide. This is mentioned briefly near the end of the tutorial I linked to above, let me know if the usage is not clear.
Re: Dynamic bodies collision
Hello,
I'd like to thank you. After some digging and some experience i finally made it work ! In case someone need a working example, here it is : http://pastebin.com/HFynR5wx
Thanks !
I'd like to thank you. After some digging and some experience i finally made it work ! In case someone need a working example, here it is : http://pastebin.com/HFynR5wx
Thanks !