Changing Hitboxes/fixture question

Topics: Developer Forum, Project Management Forum, User Forum
Apr 17, 2012 at 4:33 AM

Hello, so my problem is changing the hitbox of a character. So lets say a character does a attack that makes him go from standing tall to crouching low. Normally you would want their hitbox to resize to the crouching size, as the standing tall would be to big and you would be getting hit by attacks that visually should be going over you. So Whats the easiest way to do this? 

Possible solutions

1. Add more bodies and fixtures to an object, that are attached via a joint. Then, have them be invulnerable and unmoving unless activated and switch which ones are collide-able per move. This seems a bit bulky and pretty complicated so I'm not sure if its a good idea.

2. Remove the fixture, and then add a new one? I imagine removing and adding fixtures to a body over and over would take a lot of resources though? 

 

Is there another, easier, way to change the shape of the fixture after its attached? 

Apr 17, 2012 at 2:28 PM

You can add more than one fixture to a single body and dynamically enable/disable collisions when you need them.

Both methods that you mentioned are also ok and will give you good performance in normal situations. I prefer the second method myself but using ghost bodies is also very viable depending on your scenario. Just a little point, though, since joints are kinda soft might be better to have a true ghost body (dynamic and you move it yourself by setting its motion parameters equal to the main and it ignores  collisions, or make kinematic one...).  

There is no easier method but the ones mentioned are quite simple anyways. There is, however, a more complicated method where you manipulate the vertices of a PolygonShape directly but I've yet to see a convincing reason to do that.

Apr 17, 2012 at 6:09 PM

Awesome thanks for the reply! Good to hear that both methods have good performance. 

I am a bit worried now though of lets say the character being in the corner next to some walls, and all of a sudden they use a move that makes them have a bigger hitbox, how would you prevent them from getting stuck in the wall? 

I could maybe prevent the move from being done next to the wall if it goes through the wall upon completion but I think that would be annoying for players. I might just have to keep the same hitbox all the time maybe. 

Apr 17, 2012 at 7:09 PM

Well  - with a little tongue-in-cheek because I also have this problem and it's important to me - but how would you do it in real life? Assuming you are escaping from prison by crawling through the ventilation duct. You are in a very crammed space where you can barely fit. Suddenly you get the urge to dance to the music "jump around jump around jump up jump up and then down" ... ... ... why would a computer have a better response to that automatically?

I'm still looking for a definitive solution. Actually if there is enough space Farseer will resolve the collision pretty well, it might take a few timesteps, though. There is definitely problem when the new shape penetrates deeply into edge/loop shapes suddenly and that might be a deal-breaker for you as it was for me. Erin Catto (Box2D maker) had some pointers in this year's GDC: http://code.google.com/p/box2d/downloads/detail?name=GDC2012_ErinCatto_RagdollsVideos.pptx&can=2&q=

Apr 19, 2012 at 4:29 PM
sharp7 wrote:

Awesome thanks for the reply! Good to hear that both methods have good performance. 

I am a bit worried now though of lets say the character being in the corner next to some walls, and all of a sudden they use a move that makes them have a bigger hitbox, how would you prevent them from getting stuck in the wall? 

I could maybe prevent the move from being done next to the wall if it goes through the wall upon completion but I think that would be annoying for players. I might just have to keep the same hitbox all the time maybe. 

I see two approaches to solve this, assuming that you are using two fixtures for the same body (each fixture for a different player stance, deactivating one of the fixtures' collisions when needed). Both approaches prevent the move before making it, so you don't have to wait to complete the movement.

The first approach is the simplest one: Call an AABB raycast before making the move. If you have obstacles, then don't allow the move, nor activate the corresponding fixture. The drawback is that you are restricted to a rectangular shape for checking it (the AABB), so if your fixture is not a rectangle, or if it is slightly rotated, you could have precision problems.

The second approach is better in terms of precision, but you will spend a timestep checking: When the player has to make the move, activate the fixture but set a flag telling it that you are in a "validation step" or something like that. In the following timestep, if you have the fixture activated but with the "validation flag" activated, check all the collisions but avoid them to react (by disabling the contacts in the PreSolve method), this way you can check if the fixture is overlapping with another one, but without having contact reactions (like being stuck). Then deactivate the "validation flag" and if you didn't find any contacts, activate the fixture.

I'm not sure if I explained it clear, hope I did :)