Libgdx + Box2D design problem

Hi, i am currently working on a little project with Box2D and i am having a little problem.
It isn’t hard to solve it, but i am not sure, which solution is the right one, so it is more a design question.

I have some “Model-classes”, which all extend Entity. Inside the consturctor of Entity, i create the Body and set it’s User-Data to “this” (the Model).
The Entity also has an “update(Body b)” method, which is used to update the Bodys properties. So for example if the Entity needs to move left, a property moveLeft is set to true. Inside the update i check this property and if it is set i apply an impulse to the Body, which moves it to the left.

Inside the render loop i go throught all the Bodies in the World and call


Entity e = (Entity)b.getUserData();
e.update(b);

The problem i am now having is, that some Entities (for example a Bullet) need to have some initial speed, it should move to the direction of the mouse.
But as the Body is created inside the Entity class, where i don’t have the direction/initial speed and the direction is passed to the constructor of Bullet (Subclass of Entity) and there i don’t have the Body, i can’t apply that initial impulse.

A few solutions i can think about:

  • Add the Body as a member-variable inside Entity. I don’t really like that solution, as then the Body would have a reference to the Model and the Model would have a reference to the Body.
  • create the Body in the subclasses, instead of in the constructor. I could use a method “createBody”, to avoid redundant code. However, if that method changes i may need to change the call inside every Entity subclass.
  • Use the direction parameter inside the Entity class. I don’t really like that solution, as that param is used only forBullets, why should i pass it to the base class then?

Is there another, cleaner solution or should i rework the whole design?

Thanks :slight_smile:

Well you could do it this way.

Think about it,

  • The entity manager has the player
  • The player is controlled by you
  • The player has a gun
  • The gun shoots bullets

Therfore the player can have a reference to the entity manager, the gun is parent to the player and bullets are parent to the gun.
The player always knows where they are looking so this information can be passed into the gun and then the bullets.

When the gun calls the fire method, have it return the bullet it just created and use that to send a message to the player saying “Hey I just fired”, and in turn you can have the player send that message to the entity manager as well.

Then let the entity manager do its thing.

Have a scene model which holds all entities.
Have scene model listeners, one is a box2d creator.
Listeners care about added and removed models.
When you update, add and remove entities you only operate on the scene model.
The box2d creator takes directions, velocities etc

Thanks for the replies :slight_smile:

To me it seems like I allready have a similar structure:
I have a Base-class “Entity”.
I have a class “WorldManager” with a static method “createBody(type)”.
The constructor of “Entity” takes an int type, which is passed to “WorldManager.createBody(type)”.
That method returns a “Body”, “Entity” then calls “Body.setUserData(this)”, which basicly links my Model class with its Body.
The scene model seems to be my Box2D world, which holds all Bodies (and the Bodies hold my Model/Entity).
In the render I go through all those Bodies and update them, by using the properties set in my “Entity” class.

The player (or living entity, as there are also NPCs) itself has a “IntMap weapons”, where the Key is the “int slot” and the value is the “Weapon”.
When “shoot” is pressed, a “boolean shoot” inside the player/living entity is set to true.
Inside the “update(Body b)” i then check this flag.
If it is set, i “ask” the current “Weapon” if it can attack (check if it is loaded etc.). If the “Weapon” is ready, i create a new Bullet, which is a subclass of Entity and which ofc also has a Body.
The problem is, that cause of my design, i don’t create a Body, but a Entity (in this case Bullet), which internally creates a new Body.
As the Body creation is inside the Entity-class (the base class), which does not know anything about a direction/initial speed, I cannot add that to the Bullets body.

So basicly I create a Body, set it’s user data and forget it, as the “World” manages it for me. The only place, where I actually have the Body of my Entity is the “update(Body b)” method.
Ofc, it would be possible, to add the initial impulse there, but to me it seems “dirty”, as i need to use a flag “firstUpdate”, it would be cleaner to have an init method or something similar.

The “Listener” seems to be interesting, but I allready tryed a similar thing out: I called the “add(Body b)” method in the constructor of “Entity”, which gets called before the constructor of “Bullet()” can set the direction (“super()” needs to be the first line…).
So basicly I need to set the “direction”, before the “Body” is created or at least, before the “Listener” gets notified.

I hope I am not confusing you to much :smiley:

Thanks for your help!

Keep box2d completely out of your game entities and models.
Notify the box2d creator about model updates but don’t intervene from model classes.
Very simple and powerful.

I allready try to do that, more or less.
The model/game entity does only create a Body, as a new Entity needs a new Body.
And instead of using an update method inside the “Box2DCreator” (or in my case “WorldManager”) i use an “update(Body b)” inside my Entity, which allowes me to override the behaivor.
This can avoid type checks, as a Bullet has different properties then a Player and so the “update” needs to take care about different properties.

I have seen many structures, which store a reference to the Body inside the Model class, i am trying to avoid that.
So isn’t that what you say i should do?

Why do your entities update bodies and not vice versa?

Because i controll the Entity, i.e. set the properties in the Entity class.
In the update, i then check this properties and add impulses to the Body to control it.
Why should i update the Entity instead? How would the Body affect the Entity?
The only thing i can think about would be the position, which the Entity does not need, as the Body allready has it.
So i use the Bodies position and therefore the Entity does not need a position.

Actually is box2d controlling the entities.
the real question seems to be whether entities need to talk to box2d by them self. Or if they just need to define forces and let another subsystem (box2d) handle it.

It depends on how you see it. If you see Entity as my Entity class, then Box2D does not really controll it (it may affect an Entitys health on collisions but that does not count here), if you see an Entity as the whole thing (the Body + its referenced UserData i.e. Entity class), then yea, Box2D does controll it.
But the Entity itself affects, how box2D affects it’s Body.
Is my design a bit strange? It seemed pretty clean to me, but it seems like i am facing a few problems now.

[quote]the real question seems to be whether entities need to talk to box2d by them self. Or if they just need to define forces and let another subsystem (box2d) handle it.
[/quote]

The Entity does only need to talk to it’s Body and add forces to it. And also this should be done only once/renderloop, which is allready possible, and when the Body is created, which actually is not possible.
So i am looking for the best way, to let the Entity talk to it’s Body, as soon as the Body has been created + the Entity got all it’s properties set.
My point is, that i only need the Body in this 2 given moments, so i don’t want to store it permanent, but only give it to the methods, which are called in this specific moments. Also the initial speed+initial direction is needed only once, when a new Bullet is created, so again i don’t want to store it, but access it once and then forget it.
But i just can’t find a clean solution for this.