Reputation: 9823
For a game in libgdx
with box2d
i would like to be able to let the Player
crouch. Crouching should change its Fixture
to be a 1*1m hitbox instead of a 0.5*2m hitbox.
One possibility would be to store 2 Body
s, one for normal and one for crouching. Then if the Player
wants to crouch i could remove the first Body
from the World
and add the second Body
, but this way seems to complex for such an easy task.
Is there an easier possibility to do that?
thanks
Upvotes: 1
Views: 1574
Reputation: 9705
As you say, one way to solve this would be to have both bodies present.
Specifically, you can set Filters on the corresponding Fixtures. You will then be able to use alternate the filters to "exempt"/"include" the corresponding from collisions.
Be mindful that, as the JavaDoc for setFilterData
says:
Set the contact filtering data. This will not update contacts until the next time step when either parent body is active and awake. This automatically calls Refilter.
For more info, see Section 9.5 Contact Filtering from the box2d manual (it's for the C++ implementation, but should be clear enough for you to get the overall picture), and the aforementioned Filters JavaDoc.
EDIT: regarding the comment - for a bonus you can also actually set the "unused" body to sleep.
Regarding Mass
, since you didn't mention it I assumed you use box2d for collisions only.
But if it's not the case - well, you will have to check out what works in your simulation and adjust accordingly. For example - a "wider" rectangle (i.e. the "standing" hitbox) might receive a greater rotational momentum when hit in an extremity.
In the "mass" case, you should also consider using a Joint, e.g. a WeldJoint to "synchronize" your two hitboxes.
Upvotes: 2