I considered this scenario: objects that roughly look like this:
class PhyisicalObject
{
private:
virtual void Update() = 0;
friend class PhysicsController;
void DoUpdate() { this->Update(); }
};
There’s a controller class called a PhysicsController that manages the dynamics of a pool of physical objects by calling their DoUpdate() method. This method, in terms, calls an overloaded version of the Update()function where a numerical integrator is used to compute the objects position, velocity and acceleration step-wise. I thought that having an interface implying this functionality would be a good starting point:
class IIntegrator
{
virtual void opertor() (const vec3& pos, const vec3& vel, vec3& outPos, vec3& outVel);
};
Now inheriting this IIntegrator abstract class and providing the implementation for various methods is the next step (explicit Euler, RK4, Verlet, Midpoint, Symplectic Euler and perhaps some semi-implicit/IMEX or implicit ones would be excellent). The problem is that I don’t see clearly how to do the following two things:
-
Each physical object computes its own acceleration at any of its vertices in different ways (considering the objects consist of masspoints connected through springs or some kind of constraining objects). This function must be passed to the integrator, but it is object specific. It is possible to get pointers to non-static methods, but how would this fit the
IIntegratorinterface? -
When an object calls its
Update()method, what happens behind the scenes is that an integrator is used to provide the functionality. I’d like to switch the integration method on the fly, perhaps. Or at least instantiate the same kind of object with different integrators. To me, it sounds like a factory doing that and, for on-the-fly integrator switching.. perhaps a strategy pattern? What solution would be quite elegant and efficient in this context?
Without going into implementation details, here are a few design patterns that might be applied to your problem
PhysicalObjects, either as stand-alone objects or collections connected by strings, springs or gravitational forces.PhysicsControllerto iterate over all physical objects (composite or stand-alone) and apply a function over them.IIntegratorobjects and their integration functions at runtime.Apart from the GoF book (Amazon), a good online resource is here