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 using Farseer Physics for my 2D sidescroller game and I'm not sure how to proceed with it. I currently have a Sprite class (handles nothing but graphics), a GameObject class (contains specific object info like hit points), a World object which contains the list of Bodies, and a Level object which contains all of these objects.

Originally I was trying to keep track of the Sprites, GameObjects, and Bodies separately because I felt that would provide loose coupling but it quickly became a headache.

So my new idea was to add a Sprite member to the GameObject class but I'm still not sure how to maintain the Bodies because they have to communicate with GameObject.

Specifically, my issue is this: The position of the Body is used to draw the Sprite inside of the Level. In order to do that I would have to maintain a link between GameObjects and Bodies. Is this correct or is there a better way to architect my game?

If any of this is unclear please ask and I'll try to clarify. Thank you in advance for any help.

share|improve this question

1 Answer 1

What I do is add a Texture2D directly to my game objects. I have another class called an AnimationController which manages my source rectangle/frames/timing for drawing animations from the texture. I suppose this would be similar to what your Sprite class does.

If my game object will have a physics body (i.e. players, enemies, etc.), I add a Body property to it as well. If my game object doesn't have a physics body, then it will just have a regular Vector2 Position property (in world units).

Now, my game objects usually have Update and Draw methods. If it has a physics body, I draw the texture at the Body.Position or otherwise at this.Position using the source rect from my AnimationController. In the Update I update the AnimationController so it can switch frames and whatnot and then do whatever else for the object.

So, I would say add Sprite and Body fields/properties to your game objects that need them and let the game objects decide how to use them.

Remember, you can always go back and refactor the code later. You don't want to get stuck in a rut where you're spending more time thinking about programming your game than actually programming it.

share|improve this answer
    
Nice answer - can I add you can make the objects with bodies actuality have a property called "Position" and you can make it return Body.Position in world units. This provides great abstraction. –  Vaughan Hilts Sep 10 '12 at 13:23
    
@VaughanHilts Yep. You can definitely do that too. :) –  Richard Marskell - Drackir Sep 10 '12 at 22:51

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.