1
votes

seeing as how box2D bodies cannot be resized, I have a problem with bodies and my animated sprites.

here's the situation which I have no doubt is not unique: my sprite's dimensions slightly change during different motions like attacking, walking, jumping. seeing as how I was about to use box2D body for collision detection this can cause quite a problem. so far I thought of 3 ways to solve this issue.

1- not use box2d bodies for collision detection , just use them for object's physical behavior

2- delete and recreate body before each render. or on animation change.

3- trying to recheck the collision on bodies to see if it actually collides with the sprite itself. then act.

and here's the problem I have with each of these solutions.

1- this way makes using box2d as a whole seem rather absurd, also I'll be using not one, but two world logic and trying to keep them in sync. seems like a big headache.

2- this doesn't look very optimized, also I doubt I can just make objects pop in and out of existence in a physics world without side effects.

3- I'm not sure how or even if this can be done, actually this option is the one I require more advice on. will this cause any difficulties in the long run, or cause conflicts in physics.

please let me know if there is a efficient way to solve this , or if any of the above solutions is worth working on.

my thanks

Edit: It was more of a general question since I still don't have proper graphics for the game I'm writing, but here's my practice material:

walking enter image description here waiting (standing) enter image description here attacking enter image description here

1
Could you post a couple of example images of your sprite? What I would usually do is create multiple bodies that are connected by joints and behave like the different moveable parts of your sprite. But it's not easy to give advice on this without knowing what you are trying to accomplish.Dennis Korbar
@DennisKorbar added some example picturesRamin

1 Answers

1
votes

A body can have multiple fixtures, so you can add all fixtures for each state to the body. If you make them zero density it should not affect the physics behavior of the body. Keep in mind though, that at least one fixture on the body should be non-zero density - you could make one fixture to act as the main fixture, which has density to give the body some mass.

If you just need to detect when these fixtures are touching something, you could make them sensor fixtures, and use your contact listener to keep track of what other things they are currently touching. The contact listener would give you callbacks about all of the fixtures regardless of which state your character is in, and for each state you would keep a list of which things the fixtures for that state are touching.

If you need the fixtures to have physical interaction but only when their state is active, you could do the above but also in BeginContact you would do contact->setEnabled(false) if the contact is for a fixture that is not currently in active state.