I’m having a little trouble with EntityX and Box2D - particularly with assigning a fixture’s userData to an Entity.
Here’s the code I have when I create a new entity that has a Body/Fixture:
Entity myEntity = _entities.create(); myEntity.assign<Body>( pos ); //assign a component to the entity //set up fixture etc b2FixtureDef * fixtureDef = new b2FixtureDef(); b2CircleShape * circle = new b2CircleShape(); circle->m_radius = 0.15f; fixtureDef->shape = circle; .... //here I tell the fixture to point to the 'owner' entity fixtureDef->userData = &myEntity; ...
The issue is that, when I later use
Entity * entityA = static_cast<Entity*>(contact->GetFixtureA()->GetUserData());
inside my Contact Listener, the resulting entity pointer counts an ‘invalid’ entity - so I cant do anything with it - EntityX will just crash if I try and check for components, or check it’s id.
As far as I can tell, it’s because the initial “myEntity” object is a temporary object - so I’m actually assigning a pointer to a useless temporary object. I can’t think of anyway to get around this, however. “entities.create()” is just the way that EntityX creates entities.
Any ideas how I can get around this? It’s possible I’m just approaching this the wrong way entirely!