I am currently learning Box2d, a 2D physics engine within libgdx. But Box2d seems to be in every game framework these days so I am not really talking about libgdx.

I understand Box2d comes with lots of new concepts such as shape, bodydef, body, fixture, sprite, joint. But I think I can slowly but surely manage to learn about them.

My question, however, is how to organize them into a game object for better maintenance of code. Naturally, I am thinking about following fields in the game object class.

  • GameArea gameArea
  • Body body
  • BodyDef bodyDef;
  • FixtureDef fixtureDef
  • Shape shape;
  • Sprite sprite;

As for its methods, it has constructor, update, and display. But I am talking without experience. For example, I don't know how I can organize joints - maybe in GameArea. Any thought from experienced would be appreciated.

  • \$\begingroup\$ The question as currently stated cannot be answered without randomly guessing at what you're trying to achieve. You can't know where to put objects architecturally speaking without knowing what it is you're trying to do with them. And that requires knowing what kind of game you're trying to make and so forth. \$\endgroup\$ Jan 13, 2012 at 20:10
  • 1
    \$\begingroup\$ @NicolBolas This is a design pattern question asking about good practice. I understand coding is little different between situations. But overall practice of how you structure your classes for game objects such as bob, bullets, platforms is not that varied, I suppose. \$\endgroup\$ Jan 13, 2012 at 20:36

1 Answer 1


The answer is based on the as3 port of Box2D, but I believe, the concepts are the same.

I think, you're complicating things. bodyDef, fixtureDef and shapes are used for instantiating a body and don't need to be stored after that. Sometimes having a pointer to a specific fixture might come in handy, but not in a generic Game Object.

The same applies to joints. Sometimes you need to control them from your class - for example, you need to destroy a joint when a player steps on a trigger, or to move a platform along a joint. In that case, you'd want to put a joint field into the Trigger/Platform class.

So, what a game object needs is just a body and a sprite. GameArea reference (what ever that is) might be useful to. You might also want to store a reference to a b2World, but I prefer to store it in a static ("global") variable in a separate Physics class.

Constructor, update and display is a good base. Box2D body initialization is pretty complex, so you could add some helper methods for creating bodies and shapes.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .