Step – The Physics Simulator (Code Study)

colliding discs

Discs colliding with Box:

Studying the Code:

The root of inheritance tree is the ‘Object’ class in the StepCore/object.h file which takes help from the MetaObject and MetaProperty classes to assign the properties to the objects created .To construct an Object –  ‘a’  , STEPCORE_OBJECT(a) is called which in turn calls the ‘EIGEN_MAKE_ALIGNED_OPERATOR_NEW’ macro (which is to generate aligned-pointers for fixed-size vectorisable eigen objects, it is pre-defined in the Eigen library) and then  the STEPCORE_OBJECT_NA(a) macro.

An important class for controlling the Objects and Forces is the ‘World’ class. All the objects as well as the forces that are created in the workspace by the user, reside in the world which contains a _timeScale and Itemlist (typedef std::vector<Item*>  ItemList), BodyList (typedef std::vector<Body*>  BodyList;), ForceList (typedef std::vector<Force*> ForceList;) which act as parameters to the solver functions to generate simulation. addItem() and removItem() functions of this class add and remove Items respectively from the world.
collisionSolver() resolves the collisions occuring between bodies depending on the flag value.
doCalcFn() is an important function here.

Then there is the collisionSolver class inheriting from the Object class. It uses information from the ‘Contact’ structure which contains information about the contact that has occurred – like pointers to the bodies which have come in contact, and type of the objects (disk-polygon, disk-disk etc.). The checkContact() virtual function of the CollisionSolver class takes a bodyList& as argument and counts the number of contacts. Objects are treated as in-contact and colliding if the minimum( all points in first body, all points in second body) < a limiting value. The functions are based on Gilbert-Johnson-Keerthi Algorithm(GJK Algorithm). GSL library is being used to find the approxiamte solutions of  the  differential-equations.

RigidBody class is the base class for all bodies that we see on the left-palette of the window (disc, box, polygon). This class defines  the basic properties of rigidbodies i.e.  mass, inertia, posotion, velocity, acceleration, angular-velocity, etc. (along with the corresponding getter-setter functions). It also contains an Eigen::Vector2d type member  ‘_force ‘  which keeps track of total force acting on the centre-of-mass of the rigidbody. Similarly a double type member ‘_torque’ keeps track of the total torque acting on the rigidbody.  Functions  pointWorldToLocal()  and pointLocalToWorld()  are there to convert a point on the global scene  to the specific  body’s co-ordinates and a point on the body to the scene’s co-ordinates respectively.

The basic interfacing is controlled by the WorldModel, WorldView  WorldGraphicsItem and WorldScene classes.  worldgraphics.h file contains the arrow classes and the corresponding handler events to display the velocities, angular-velocities etc. of the bodies on scene (when they are hovered or selected). It also contains definitions of the itemCreator classes. The WorldModel class inherits from  QAbstractItemModel  and is mainly responsible for controlling the selection behaviour and simulation related behavours. The WorlGraphicsView (inheriting from QGraphicsView) provides a view to the WorldScene. It caontains the zoomIn, zoomOut functions (actually slots)  for the scene and  the scroll Events. WorldScene is the scene where all the objects created and the simulation is displayed.

to be continued later…..