Hello
I was playing with the library (1.4.3), when weird things stared to happen: assertions failed etc.
The reason is following:
When adding shape to body definition, via b2BodyDef::AddShape( const b2ShapeDef*), the shape definition stores pointer passed as parameter. So it is required to have the shape definition existing in current scope when b2World::CreateBody(const b2BodyDef* ) is called.
IMHO this is a bad design, from following reasons:
1. Docs promised me something along the lines of 'not worrying about object def scope'. OK, I know, docs never are up-to date 😉
2. This definitely breaks encapsulation: actions performed on one object (body def) affects different (thou somehow associated) action performed on another object (world, in this case).
So, my problem popped up in following code layout:
where shapes were created on stack in createShape() method, and added to body object created elsewhere and passed as pointer. _outline variable used here is polygon definition not associated with Box2D in any way.
This is minor inconvenience, which should be at least documented. But no problem - code is available 🙂
But, IMO, it would be better to store deep copies of shape defs added to body def. Of course this harms performance, but not that much. If needed, could be improved by creating shared data sections with reference counters (lot of work unfortunately).
Also: adding/removing shapes from existing body would be cool.
Regards!
EDIT: yeah, I know, this is duplicate of: viewtopic.php?f=4&t=108 . Sorry 😳
I was playing with the library (1.4.3), when weird things stared to happen: assertions failed etc.
The reason is following:
When adding shape to body definition, via b2BodyDef::AddShape( const b2ShapeDef*), the shape definition stores pointer passed as parameter. So it is required to have the shape definition existing in current scope when b2World::CreateBody(const b2BodyDef* ) is called.
IMHO this is a bad design, from following reasons:
1. Docs promised me something along the lines of 'not worrying about object def scope'. OK, I know, docs never are up-to date 😉
2. This definitely breaks encapsulation: actions performed on one object (body def) affects different (thou somehow associated) action performed on another object (world, in this case).
So, my problem popped up in following code layout:
Code:
b2BodyDef bodyDef;
createShape( &bodyDef, _outline ); // create shape(s) based on _outline, adds to body using AddShape()
_pBody = _pWorld->CreateBody(&bodyDef);
createShape( &bodyDef, _outline ); // create shape(s) based on _outline, adds to body using AddShape()
_pBody = _pWorld->CreateBody(&bodyDef);
where shapes were created on stack in createShape() method, and added to body object created elsewhere and passed as pointer. _outline variable used here is polygon definition not associated with Box2D in any way.
This is minor inconvenience, which should be at least documented. But no problem - code is available 🙂
But, IMO, it would be better to store deep copies of shape defs added to body def. Of course this harms performance, but not that much. If needed, could be improved by creating shared data sections with reference counters (lot of work unfortunately).
Also: adding/removing shapes from existing body would be cool.
Regards!
EDIT: yeah, I know, this is duplicate of: viewtopic.php?f=4&t=108 . Sorry 😳