Bit of a newbie question!

Topics: Developer Forum
Feb 18, 2010 at 12:13 PM

Hi Folks,

I've been using Farseer Physics for about a week now, and I'm absolutely loving it. =)

 

I've got a bit of a newbie question though.  My Game.cs class is getting pretty cluttered and I was wondering if it was possible to move all my physics related stuff to another class, and then use the Game.cs class to call them when I needed to load content, update and draw it.

 

I had a go at this, and while it compiled okay, I got an error when it couldn't find the Content folder for boxTexture.  I tried defining where the Content folder was (Content.RootDirectory = "Content";) but still had no luck.  I also tried creating a new GraphicsDeviceManager but it said there can only be one of these.

 

Apologies if it is a very newbie problem, but any help would be fantastic.

 

Thanks in advance,

Alan

            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

 

 

Feb 18, 2010 at 5:20 PM

Yes, you definitely should be keeping everything separate. Have a look at http://www.nuclex.org/blog/gamedev/78-game-architecture-day-2 for an idea for how you should be structuring things.

 

As for your problem, it's hard to tell without seeing some code. Post the method that's loading the content and I'll have a look at it.

Feb 18, 2010 at 9:36 PM
Edited Feb 18, 2010 at 9:44 PM

Hi Seregrail, thanks for your response.

I'll have a look at that link.  That'll certainly come in handy. =)

Here's the snippets of code I'm talking about.  In this example I've got two classes - the Game class, where almost everything originally was, a new class, PhysicsObject.  I'm trying to move anything physics related from the Game class into the PhysicsObject class to clean things up:

 clasPhysicsObject : Game
    {
        PhysicsSimulator simulator = new PhysicsSimulator(new Vector2(0, 250));
        Game game;
        Body boxBody;
        Geom boxGeom;
        Texture2D boxTexture;
        Vector2 boxOrigin;
        public void PhysicsContent()
        {
            boxTexture = Content.Load<Texture2D>("Box");
            boxOrigin = new Vector2(boxTexture.Width / 2, boxTexture.Height / 2);
            boxBody = BodyFactory.Instance.CreateRectangleBody(boxTexture.Height, boxTexture.Width, 1);
            boxBody.Position = new Vector2(100, 300);
            boxBody.IsStatic = false;
            simulator.Add(boxBody);
            boxGeom = GeomFactory.Instance.CreateRectangleGeom(boxBody, boxTexture.Width, boxTexture.Height);
            boxGeom.FrictionCoefficient = 0.3f;
            boxGeom.RestitutionCoefficient = 0.5f;
            simulator.Add(boxGeom);
        }
    class PhysicsObject : Game
    {
        PhysicsSimulator simulator = new PhysicsSimulator(new Vector2(0, 250));

        Game game;
        Body boxBody;
        Geom boxGeom;
        Texture2D boxTexture;
        Vector2 boxOrigin;

        public void PhysicsContent()
        {
            boxTexture = Content.Load<Texture2D>("Box");
            boxOrigin = new Vector2(boxTexture.Width / 2, boxTexture.Height / 2);

            boxBody = BodyFactory.Instance.CreateRectangleBody(boxTexture.Height, boxTexture.Width, 1);
            boxBody.Position = new Vector2(100, 300);
            boxBody.IsStatic = false;
            simulator.Add(boxBody);

            boxGeom = GeomFactory.Instance.CreateRectangleGeom(boxBody, boxTexture.Width, boxTexture.Height);
            boxGeom.FrictionCoefficient = 0.3f;
            boxGeom.RestitutionCoefficient = 0.5f;
            simulator.Add(boxGeom);
        }
Then in my Game class, I call this method in the LoadContent method:
            physicsObject.PhysicsContent();

This compiles okay, but when I go to run the game, I get the following error message:

"ContentLoadException was unhandled.

Error loading "Box". GraphicsDevice component not found."

The line of code in question is from the new PhysicsObject class:

boxTexture = Content.Load<Texture2D>("Box");

I've obviously gone wrong somewhere, and since I've done pretty much the same for the rest of the class (such as applying forces in the PhysicsObject class, then calling it from the Game class) I expect to get similar errors later on.

Again, apologies if this is a very obvious problem but any help really would be appreciated =)

 

Thanks very much,

Alan