2014-01-23 18:45:33 +01:00
|
|
|
/********************************************************************
|
|
|
|
* Created by Dan Andersson 2014
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
#include "Inertia.h"
|
|
|
|
|
|
|
|
using namespace ::Oyster::Math3D;
|
|
|
|
using namespace ::Oyster::Physics3D;
|
|
|
|
|
|
|
|
MomentOfInertia::MomentOfInertia()
|
|
|
|
{
|
|
|
|
this->rotation = Quaternion::identity;
|
|
|
|
this->magnitude = Float3( 1.0f );
|
|
|
|
}
|
|
|
|
|
|
|
|
MomentOfInertia::MomentOfInertia( const Quaternion &r, const Float3 &m )
|
|
|
|
{
|
|
|
|
this->rotation = r;
|
|
|
|
this->magnitude = m;
|
|
|
|
}
|
|
|
|
|
|
|
|
MomentOfInertia & MomentOfInertia::operator = ( const MomentOfInertia &i )
|
|
|
|
{
|
|
|
|
this->rotation = i.rotation;
|
|
|
|
this->magnitude = i.magnitude;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-01-28 09:23:58 +01:00
|
|
|
Float3 MomentOfInertia::CalculateAngularVelocity( const Quaternion &externR, const Float3 &h ) const
|
2014-01-23 18:45:33 +01:00
|
|
|
{
|
2014-01-28 09:23:58 +01:00
|
|
|
return this->CalculateAngularVelocity( externR, h, Float3() );
|
2014-01-23 18:45:33 +01:00
|
|
|
}
|
|
|
|
|
2014-01-28 09:23:58 +01:00
|
|
|
Float3 & MomentOfInertia::CalculateAngularVelocity( const Quaternion &externR, const Float3 &h, Float3 &targetMem ) const
|
2014-01-23 18:45:33 +01:00
|
|
|
{ // w = (R * I_R) * I_M^-1 * (R * I_R)^-1 * h
|
|
|
|
Float4x4 rotation = RotationMatrix( externR ) * RotationMatrix( this->rotation );
|
2014-01-28 09:23:58 +01:00
|
|
|
Float4 w = rotation.GetInverse() * Float4( h, 0.0f );
|
2014-01-23 18:45:33 +01:00
|
|
|
return targetMem = rotation * w.PiecewiseMultiplicationAdd( Float4(1.0f / this->magnitude.x, 1.0f / this->magnitude.y, 1.0f / this->magnitude.z, 0.0f) );
|
|
|
|
}
|
|
|
|
|
2014-01-28 09:23:58 +01:00
|
|
|
Float3 MomentOfInertia::CalculateAngularMomentum( const Quaternion &externR, const Float3 &w ) const
|
2014-01-23 18:45:33 +01:00
|
|
|
{
|
2014-01-28 09:23:58 +01:00
|
|
|
return this->CalculateAngularMomentum( externR, w, Float3() );
|
2014-01-23 18:45:33 +01:00
|
|
|
}
|
|
|
|
|
2014-01-28 09:23:58 +01:00
|
|
|
Float3 & MomentOfInertia::CalculateAngularMomentum( const Quaternion &externR, const Float3 &w, Float3 &targetMem ) const
|
2014-01-23 18:45:33 +01:00
|
|
|
{ // h = (R * I_R) * I_M * (R * I_R)^-1 * w
|
|
|
|
Float4x4 rotation = RotationMatrix( externR ) * RotationMatrix( this->rotation );
|
2014-01-28 09:23:58 +01:00
|
|
|
Float4 h = rotation.GetInverse() * Float4( w, 0.0f );
|
2014-01-23 18:45:33 +01:00
|
|
|
return targetMem = rotation * h.PiecewiseMultiplicationAdd( Float4(this->magnitude.x, this->magnitude.y, this->magnitude.z, 0.0f) );
|
|
|
|
}
|