Static Engine

Topics: User Forum
Nov 10, 2006 at 4:05 AM
Is there any reason why you haven't made Farseer static?

I can't say I've thought a lot about it, so the answer is probably obvious. (c:

Is it because you want to be able to run several unrelated engines at the same time? Why would you want that?

The reason I'm asking is because the program I'm working on has a pretty clean design, at least as seen from the instance of the XNA Game class:

I have a constructor(), Initialize(), Update(), and Draw() methods in each class. None of my classes take arguments. All calls to them are done like this:

Initialize()

and then the internal code of that class deals with everything.

To set this up, I am passing references to the SpriteBatch and the Texture2D in the LoadGraphicsContent() method. This is the only method which actually passes anything to my ball class.

I've layered everything with multiple classes:

Ball inherits from Sprite, which inherits from Image, and so on.

At this point, I tried to rewrite Initialize() in the Sprite class, so that it takes a reference to a PhysicsSimulator object. It is needed for the PhysicsSimulator.Add(RigidBody) pseudo-code, as always

This breaks the structure of Initialize(), as everything else in my inheritance tree takes no arguments.

It also confuses the programming, because there are two overloaded methods:

Initialize() and Initialize(PhysicsSimulator)

My solution was to have both Initialize() and InitialzeFarseer(PhysicsSimulator) on my Sprite class. This is the first time in the code I need to pass anything graphics-unrelated.

I then needed to massage the code until it works with physics if I remember to call InitializeFarseer(), and works without physics if I forget.

I'd love if PhysicsSimulator.Add() was static, or some other solution, so I don't need to pass that reference. Is it reasonably clear why? If not, I can send you my entire Solution.

I'm rambling again. Let me know it it doesn't make sense.

Cheers!
Jöran
Coordinator
Nov 10, 2006 at 12:18 PM
I'll give this some thought...
Coordinator
Nov 10, 2006 at 12:55 PM
I thought about this a bit on the way to work this morning.

The problem with making the PhysicsSimulator object static is that once that is done, nobody can inherit and extend it anymore. I think there is value in keeping it extendable.

However, I think there is an easy solution to your problem. Create a simple singleton object.

In your own code base, add something like this
public class MyPhysicsSimulator
{
private PhysicsSimulator instance;
//make constuctor private
private MyPhysicsSimulator()
{
}

public Static PhysicsSimulator Instance
{
get
{
if(instance==null)
{
instance = new PhysicsSimulator();
}
return instance;
}
}
}

Once you do this, you now have a global access to the PhysicsSimulator by making a call like this.

MyPhysicsSimulator.Instance.Add()
MyPhysicsSimulator.Instance.Update(..)

etc...

You can good for the Singleton pattern for more info.

Hope this helps
Coordinator
Nov 10, 2006 at 12:56 PM
I wish these forums allowed editing! That last line was suppose to be:

You can GOOGLE for the Singleton pattern for more info.
Nov 10, 2006 at 5:03 PM
Ah, good idea! Stupid of me not to think of it. :-/

I'll wrap it up myself, based on the code you put above (Great service!). Consider this thread closed.

Cheers!
Jöran