blRigidBodyAPI — A simple rigid body API in c++

This entry is part 1 of 1 in the series blRigidBodyAPI -- BarbatoLabs Rigid Body API

Introduction

Rigid body dynamics is the body of knowledge that deals with the motion of rigid, non-flexible bodies. As it turns out, there are many things that look rigid in our daily human experiences. Because of that, there are literally hundreds of rigid body APIs for c++ out there, commercial or open source, professionally thought-out ones or messy ones, the choice is yours.

In this series, I present the Barbato Labs Rigid Body API, which I called blRidigBodyAPI. A very simple but scalable rigid body API I created in c++, and which I have used for various applications, such as physical simulations, interactive interfaces with physical behaviours, and much more.

API Structure

When building a model for a rigid body, we can choose  to account for certain physical behaviours, while ignoring the ones we don’t care about. To allow for this flexibility, the blRigidBodyAPI uses multiple inheritance.

We define a class for each behaviour we want to model, and then we combine them to form a rigid body class.

The Interface Mechanism

The blRigidBody class uses the following simple mechanism to interface with an object:

  1. Use the AddForce function to add forces to the object.
    1. Use as many times as needed
  2. Use the AddTorque functions to add torques/couples to the object.
    1. Use as many times an needed
  3. Simulate the object
    1. Pass it a time-step, a gravitational field and the integration method to use
      1. As of now the available integration methods are:
        1. Euler Method
        2. Runga-kutta 4th order Method
  4. Reset to zero the total force and torque acting on the object.

Note: As of this writing, the blRigidBodyAPI does not implement a collision system yet, but that’s coming soon, check the updates section.

The Physics

For a quick introduction to rigid body dynamics look up this wikipedia article.

Rotation in the API is represented using a normalized quaternion. Keeping that in mind, the algorithm used to simulate the rigid body is the following:

  1. Calculate Acceleration and Angular Acceleration
  2. Integrate the position
    1. Position = Position + Velocity*TimeStep
  3. Integrate the velocity
    1. Velocity = Velocity + Acceleration*TimeStep
  4. Integrate the angular position
    1. Create a quaternion representing the current angular velocity (We create the quaternion without normalizing)
    2. Multiply the velocity quaternion to the current rotation quaternion (Again without normalizing)
      1. RotQtn = 0.5*(AngVelQtn*RotQtn)*TimeStep
    3. Normalize the new rotation quaternion
  5. Integrate the angular velocity
    1. Inertia*(TotalTorque – CrossProduct(AngularVelocity,Inertia*AngularVelocity))*TimeStep

Note: The simulation algorithm is the same no matter which integration method is chosen

Dependencies

  1. blInertia dependencies
    1. blMatrix3d –3×3 matrix class from the blMathAPI library
      1. Used to hold the rotational inertia of an object and its inverse
  2. blPosition dependencies
    1. blVector3d — Three dimensional vector class from the blMathAPI library
      1. Used to hold the position
  3. blVelocity dependencies
    1. blVector3d
      1. Used to hold the velocity
  4. blAngularVelocity dependencies
    1. blVector3d
      1. Used to hold the angular velocity
  5. blOrientation dependencies
    1. blVector3d
      1. Used to hold the rotation axis and the orientation axes
    2. blQuaternion — Quaternion class from the blMathAPI library
      1. Used to hold the current rotation orientation
  6. blSize dependencies
    1. blVector3d
      1. Used to hold the size
  7. blDamping
    1. blVector3d
      1. Used to calculate the linear and rotational damping forces acting on the object

The Code

The code uses inline documentation and strives for code clarity and readability.

The blRigidBodyAPI is made of several classes as shown above, and they’ve been placed inside the namespace blRigidBodyAPI, which resides in the file blRigidBodyAPI.hpp

Note:  This file will change as I add more classes to it, but the latest version will be found in the downloads section at the bottom of this post

blRigidBodyAPI.hpp (Click to see code…)

The blInertia class is defined in the file blInertia.hpp as follows:

blInertia.hpp (Click to see code…)

The blPosition class is defined in the file blPosition.hpp as follows:

blPosition.hpp (Click to see code…)

The blVelocity class is defined in the file blVelocity.hpp as follows:

blVelocity.hpp (Click to see code…)

The blAngularVelocity class is defined in the file blAngularVelocity.hpp as follows:

blAngularVelocity.hpp (Click to see code…)

The blOrientation class is defined in the file blOrientation.hpp as follows:

blOrientation.hpp (Click to see code…)

The blSize class is defined in the file blSize.hpp as follows:

blSize.hpp (Click to see code…)

The blDamping class is defined in the file blDamping.hpp as follows:

blDamping.hpp (Click to see code…)

The blRigidBody class is defined in the file blRigidBody.hpp as follows:

blRigidBody.hpp (Click to see code…)

Usage

I will be posting in this series, examples that show how to use the API. Check back soon.

Downloads

I have put all the files into a zip file which can be downloaded here. All you have to do is extract it somewhere, let’s say in a directory called blRigidBodyAPI, and then include the blRigidBodyAPI.hpp file as follows:

#include "blRigidBodyAPI/blRigidBodyAPI.hpp"
using namespace blRigidBodyAPI;

Note: The blRigidBodyAPI depends on the blMathAPI and thus you’ll need both APIs:
blMathAPI.zip Ver 02/22/2011 2:50am (1688) blRigidBodyAPI.zip Ver 12/5/2010 2:28pm (1125)

Update

No updates yet, check back soon.

About Vincenzo Barbato

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few.

Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes.

Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration.

His outlook on life:

"Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

About Enzo

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few. Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes. Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration. His outlook on life: "Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

Leave a Reply

Your email address will not be published. Required fields are marked *