Adding numerical integration to objects that update their physical state

I reviewed this scenario: objects that look something like this:

class PhyisicalObject { private: virtual void Update() = 0; friend class PhysicsController; void DoUpdate() { this->Update(); } }; 

There is a controller class called PhysicsController that controls the dynamics of a pool of physical objects by calling their DoUpdate() method. This method, in terms, calls the overloaded version of the Update() function, where a numerical integrator is used to calculate the position, speed and acceleration of objects in stages. I thought 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 an implementation for various methods is the next step (explicit Euler, RK4, Verlet, Midpoint, Symplectic Euler, and possibly some semi-implicit / IMEX or implicit, would be excellent). The problem is that I do not see clearly how to do the following two things:

  • Each physical object calculates its own acceleration in any of its vertices in different ways (given that the objects consist of mass points connected through springs or some kind of restraining objects). This function must be passed to the integrator, but it is specific to the object. You can get pointers to non-static methods, but how will this match the IIntegrator interface?

  • When an object calls its Update() method, what happens behind the scenes is that the integrator is used to provide functionality. Perhaps I would like to switch the integration method on the fly. Or at least create an instance of one object with different integrators. For me it sounds like a factory, and to switch the integrator on the fly .. maybe a strategy template? Which solution will be quite elegant and effective in this context?

+4
source share
1 answer

Without going into implementation details, here are a few design patterns that can be applied to your problem.

  • Factory or Prototype . To create objects at startup from a file or to clone them at runtime, respectively.
  • Composite This can be used to model PhysicalObjects , as stand-alone objects or collections connected by strings, springs or gravitational forces.
  • Iterator or Visitor . This can be used by the PhysicsController to repeat all physical objects (compound or autonomous) and apply functions to them.
  • Strategy . To select various IIntegrator objects and their runtime integration functions.

Besides GoF book ( Amazon ), a good online resource is here

+2
source

All Articles