Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  Nicol Bolas Jan 13 '12 at 20:10
1  
@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. –  Paul Jan 13 '12 at 20:36
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
Makes sense. Thanks for your answer. –  Paul Jan 13 '12 at 22:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.