Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EntityListener and Box2D bodies #188

Closed
lukz opened this issue Oct 26, 2015 · 4 comments
Closed

EntityListener and Box2D bodies #188

lukz opened this issue Oct 26, 2015 · 4 comments

Comments

@lukz
Copy link
Contributor

lukz commented Oct 26, 2015

Ashely wiki suggests to use EntityListeners to remove Box2D bodies. I also think it's the best way to handle that task.

However, you might be interested in knowing when entities enter or leave a particular family. This can be useful to keep track of when components of a particular type are added to or removed from entities. For example, you may want to know whenever a PhysicsComponent is removed from an entity so you can destroy the physics body from the Box2D World. In this case, you will need to pass in the family to the addEntityListener() method.

So I've created listener:

public class PhysicsComponentListener implements EntityListener {

    @Override
    public void entityAdded(Entity entity) {

    }

    @Override
    public void entityRemoved(Entity entity) {
        PhysicsComponent physComp = Components.PHYSICS.get(entity);

        if(physComp != null) {
            G.engine.getSystem(Box2DWorldSystem.class).getWorld().destroyBody(physComp.body);
        }
    }

}

And added listener to engine:

G.engine.addEntityListener(Family.all(PhysicsComponent.class).get(), new PhysicsComponentListener());

My PhysicsComponent contains Box2D body so I have to get this component in listener to destroy the body but I can't do this because Listener is notified after component removal in case of removing single component from Entity.

Is there any way to resolve that issue?

@antag99
Copy link
Contributor

antag99 commented Oct 26, 2015

Best thing to do is probably just to avoid removing the single component altogether. These semantics were discussed in #186.

@lukz
Copy link
Contributor Author

lukz commented Oct 26, 2015

@antag99 my case is to remove body from particle entity that have stopped and I don't need to move it ever again.

If there is no other way I will create special component and system that would handle body removal before component removal. Listeners seemed more convenient.

@antag99
Copy link
Contributor

antag99 commented Oct 26, 2015

@lukz You could kill the particle and create a separate "decal" entity, or freeze the body.

@lukz lukz closed this as completed Oct 27, 2015
@vaughandroid
Copy link

@lukz You could just create a Map<Entity, Body> and use that. Add the entry in entityAdded(), and use it to get the Body in entityRemoved()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants