# RigidBody class design

This topic is 3740 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Well, I was not sure where to create this topic, as it is related to both general programming( design issues ) and to Math and Physics. I am just expecting that the experience of some of you in designing classes, will help me. So, I've designed two RigidBody classes, each using different state variables: the two have position and orientation( Matrix or quaternion ) as state variables, the first one, linear and angular momentums in addition and the second one, linear and angular velocities...I designed the second class because computing forces and torques to have a rigid body, after updating, in a certain state( velocities ) was much more intuitive. When designing both classes, I notice that in fact, designing a function to compute the state derivate and update the auxiliary variables( for the momentum approach, these are the velocities and the inverse of the world inertia tensor ), made the code tighter and eased the debugging process. For the state updating, I do it outside the class: to avoid hard coding the integration scheme inside the class, and hence enabling me to use any integration method. I've defined some explicit Runge-Kutta methods, and it was easy to change the integration scheme during a simulation. To achieve that, I wrote a function template inside my RigidBody class, with the integration method as a template parameter. Well, to make the RigidBody class, more general, the idea came to me, to create a template RigidBody class, with a StateVariables class and an AuxiliaryVariables class as template parameters, and to make it inherit from both class templates...I avoid using composition, since not knowing state variables and auxiliary variables, in the general case, obliges me to inherit from both classes. I simply define inside the RigidBody class, constructors and an update function( template ) as following. So here is the design: Well, I was not sure where to create this topic, as it is related to both general programming( design issues ) and to Math and Physics. I am just expecting that the experience of some of you in designing classes related to Physics Simulation will help me. So, I've designed two RigidBody classes, each using different state variables: the two have position and orientation( Matrix or quaternion ) as state variables, the first one, linear and angular momentums in addition and the second one, linear and angular velocities...I designed the second class because computing forces and torques to have a rigid body, after updating, in a certain state( velocities ) was much more intuitive. When designing both classes, I notice that in fact, designing a function to compute the state derivate and update the auxiliary variables( for the momentum approach, these are the velocities and the inverse of the world inertia tensor ), made the code tighter and eased the debugging process. For the state updating, I do it outside the class: to avoid hard coding the integration scheme inside the class, and hence enabling me to use any integration method. I've defined some explicit Runge-Kutta methods, and it was easy to change the integration scheme during a simulation. To achieve that, I wrote a function template inside my RigidBody class, with the integration method as a template parameter. Well, to make the RigidBody class, more general, the idea came to me, to create a template RigidBody class, with a StateVariables class and an AuxiliaryVariables class as template parameters, and to make it inherit from both class templates...I avoid using composition, since not knowing state variables and auxiliary variables, in the general case, obliges me to inherit from both classes. I simply define inside the RigidBody class, constructors and an update function( template ) as following. So here is the design:
template< class Properties,
class StateVariables,
class AuxiliaryVariables >

class RigidBody: public Properties, public StateVariables, public AuxiliaryVariables
{
public:
RigidBody(){}
RigidBody( const Properties& props,
const StateVariables& state,
const AuxiliaryVariables& auxiliary );

template< class IntegrationScheme,
class ForceGenerator,
class TorqueGenerator >

void update( IntegrationScheme& scheme,
ForceGenerator& force,
TorqueGenerator& torque,
const Real& time,
const Real& deltaTime )
{
scheme.integrate( force, torque, *this, *this, *this, time, deltaTime );
}
////////////////////////////////////////////////////////////////////////////
//functions?
};


template<class Vector,class Matrix>
class State
{
private:
Vector position_;
Matrix orientM_;
Vector linMom_;
Vector angMom_;
////////////////////////////////////////////////////////////////////////////////
public:
////////////////////////////////////////////////////////////////////////////////
//constructors...
////////////////////////////////////////////////////////////////////////////////
//accessors
////////////////////////////////////////////////////////////////////////////////
//utility functions
////////////////////////////////////////////////////////////////////////////////
template< class Force, class Torque >
void computeDerivate( const ForceGenerator&,
const TorqueGenerator&,
const Properties&,
Velocities&,
parameter,
State<Vector,Matrix>& stateDerivate )const;
void updateAuxiliary( Properties&,
Velocities& )const;
};


template<class Vector,class Matrix>
class Auxiliary
{
public:
//constructors, destructors
////////////////////////////////////////////////////////////////////////////////
//accessors
////////////////////////////////////////////////////////////////////////////////
private:
Vector angVel_,
Vector linVel_,
Matrix invInertia_;
};


template<class Real,class Matrix>
class Properties
{
public:
//constructors,destructors
////////////////////////////////////////////////////////////////////////////////
//accessors
////////////////////////////////////////////////////////////////////////////////
private:
Real invMass_;
Matrix bodyInertia_;
Matrix invBodyInertia_;
};


class IntegrationScheme
{
public:
IntegrationScheme();

template< class ForceGenerator,
class TorqueGenerator,
class Properties,
class State,
class Auxiliary>

void integrate( const ForceGenerator&,
const TorqueGenerator&,
const Properties&,
State&,
Auxiliary&,
const Real&,
const Real& );
};


I just want to have your opinions, on a design point of view... Thanks for any remark. I just want to have your opinions, on a design point of view...and on the performance side, because I use multiple inheritance and pass in the update function of the RigidBody class, its reference three times: the first time, it is interpretated as a reference to a Properties object, the second, to a State one and the last time, to an AuxiliaryState one. Thanks for your critics. [Edited by - johnstanp on July 23, 2008 10:40:15 AM]

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632948
• Total Posts
3009399
• ### Who's Online (See full list)

There are no registered users currently online

×