1
votes

I have a repeatable block type that uses the same body definition and fixture definition. Should I create the bodyDef and fixtureDef for every block, or just change the position for bodyDef and reuse the same objects every time?

1

1 Answers

2
votes

You can't have one fixture for multiple bodies, like the title says, as each fixture is attached to a particular body, and destroyed with it.

However, you can use the same body definition to create multiple bodies, and the same fixture definition to create multiple fixtures (possibly attached to different bodies). It is also better practice, since it allows to save memory and lines of code.

It is stated in section 7.2 of the Box2D manual:

Box2D copies the data out of the body definition; it does not keep a pointer to the body definition. This means you can recycle a body definition to create multiple bodies.