Merge branch 'Physics' of https://github.com/dean11/Danbias into Physics

This commit is contained in:
Robin Engman 2014-01-28 09:01:50 +01:00
commit a07e2911de
17 changed files with 409 additions and 151 deletions

Binary file not shown.

Binary file not shown.

View File

@ -28,7 +28,7 @@ SphericalRigidBody::SphericalRigidBody( const API::SphericalBodyDescription &des
this->rigid.centerPos = desc.centerPosition;
this->rigid.boundingReach = Float4( desc.radius, desc.radius, desc.radius, 0.0f );
this->rigid.SetMass_KeepMomentum( desc.mass );
this->rigid.SetMomentOfInertia_KeepMomentum( Formula::MomentOfInertia::CreateSphereMatrix( desc.mass, desc.radius ) );
this->rigid.SetMomentOfInertia_KeepMomentum( MomentOfInertia::Sphere(desc.mass, desc.radius) );
this->deltaPos = Float4::null;
this->deltaAxis = Float4::null;

View File

@ -19,7 +19,7 @@ namespace Oyster
this->restitutionCoeff = 1.0f;
this->frictionCoeff_Dynamic = 0.5f;
this->frictionCoeff_Static = 0.5f;
this->inertiaTensor = ::Oyster::Math::Float4x4::identity;
this->inertiaTensor = ::Oyster::Physics3D::MomentOfInertia();
this->subscription_onCollision = NULL;
this->subscription_onCollisionResponse = NULL;
this->subscription_onMovement = NULL;
@ -41,7 +41,7 @@ namespace Oyster
this->ignoreGravity = false;
}
inline CustomBodyState::CustomBodyState( ::Oyster::Math::Float mass, ::Oyster::Math::Float restitutionCoeff, ::Oyster::Math::Float staticFrictionCoeff, ::Oyster::Math::Float kineticFrictionCoeff, const ::Oyster::Math::Float4x4 &inertiaTensor, const ::Oyster::Math::Float4 &reach, const ::Oyster::Math::Float4 &centerPos, const ::Oyster::Math::Float4 &rotation, const ::Oyster::Math::Float4 &linearMomentum, const ::Oyster::Math::Float4 &angularMomentum, const ::Oyster::Math::Float4 &gravityNormal )
inline CustomBodyState::CustomBodyState( ::Oyster::Math::Float mass, ::Oyster::Math::Float restitutionCoeff, ::Oyster::Math::Float staticFrictionCoeff, ::Oyster::Math::Float kineticFrictionCoeff, const ::Oyster::Physics3D::MomentOfInertia &inertiaTensor, const ::Oyster::Math::Float4 &reach, const ::Oyster::Math::Float4 &centerPos, const ::Oyster::Math::Float4 &rotation, const ::Oyster::Math::Float4 &linearMomentum, const ::Oyster::Math::Float4 &angularMomentum, const ::Oyster::Math::Float4 &gravityNormal )
{
this->mass = mass;
this->restitutionCoeff = restitutionCoeff;
@ -102,7 +102,7 @@ namespace Oyster
return this->kineticFrictionCoeff;
}
inline const ::Oyster::Math::Float4x4 & CustomBodyState::GetMomentOfInertia() const
inline const ::Oyster::Physics3D::MomentOfInertia & CustomBodyState::GetMomentOfInertia() const
{
return this->inertiaTensor;
}
@ -219,20 +219,17 @@ namespace Oyster
this->kineticFrictionCoeff = kineticU;
}
inline void CustomBodyState::SetMomentOfInertia_KeepMomentum( const ::Oyster::Math::Float4x4 &tensor )
inline void CustomBodyState::SetMomentOfInertia_KeepMomentum( const ::Oyster::Physics3D::MomentOfInertia &tensor )
{
this->inertiaTensor = tensor;
}
inline void CustomBodyState::SetMomentOfInertia_KeepVelocity( const ::Oyster::Math::Float4x4 &tensor )
inline void CustomBodyState::SetMomentOfInertia_KeepVelocity( const ::Oyster::Physics3D::MomentOfInertia &tensor )
{
if( tensor.GetDeterminant() != 0.0f )
{ // sanity block!
::Oyster::Math::Float4x4 rotation = ::Oyster::Math3D::RotationMatrix(this->angularAxis.xyz);
::Oyster::Math::Float4 w = ::Oyster::Physics3D::Formula::AngularVelocity( (rotation * this->inertiaTensor).GetInverse(), this->angularMomentum );
this->inertiaTensor = tensor;
this->angularMomentum = ::Oyster::Physics3D::Formula::AngularMomentum( rotation * tensor, w );
}
::Oyster::Math::Quaternion rotation = ::Oyster::Math3D::Rotation(this->angularAxis);
::Oyster::Math::Float4 w = this->inertiaTensor.CalculateAngularVelocity( rotation, this->angularMomentum );
this->inertiaTensor = tensor;
this->angularMomentum = this->inertiaTensor.CalculateAngularMomentum( rotation, w );
}
inline void CustomBodyState::SetSize( const ::Oyster::Math::Float4 &size )

View File

@ -3,6 +3,7 @@
#include "OysterMath.h"
#include "PhysicsAPI.h"
#include "Inertia.h"
namespace Oyster { namespace Physics
{
@ -17,7 +18,7 @@ namespace Oyster { namespace Physics
::Oyster::Math::Float restitutionCoeff;
::Oyster::Math::Float frictionCoeff_Static;
::Oyster::Math::Float frictionCoeff_Dynamic;
::Oyster::Math::Float4x4 inertiaTensor;
::Oyster::Physics3D::MomentOfInertia inertiaTensor;
::Oyster::Physics::ICustomBody::EventAction_Collision subscription_onCollision;
::Oyster::Physics::ICustomBody::EventAction_CollisionResponse subscription_onCollisionResponse;
::Oyster::Physics::ICustomBody::EventAction_Move subscription_onMovement;
@ -50,7 +51,7 @@ namespace Oyster { namespace Physics
::Oyster::Math::Float restitutionCoeff = 1.0f,
::Oyster::Math::Float staticFrictionCoeff = 1.0f,
::Oyster::Math::Float kineticFrictionCoeff = 1.0f,
const ::Oyster::Math::Float4x4 &inertiaTensor = ::Oyster::Math::Float4x4::identity,
const ::Oyster::Physics3D::MomentOfInertia &inertiaTensor = ::Oyster::Physics3D::MomentOfInertia(),
const ::Oyster::Math::Float4 &reach = ::Oyster::Math::Float4::null,
const ::Oyster::Math::Float4 &centerPos = ::Oyster::Math::Float4::standard_unit_w,
const ::Oyster::Math::Float4 &rotation = ::Oyster::Math::Float4::null,
@ -64,7 +65,7 @@ namespace Oyster { namespace Physics
const ::Oyster::Math::Float GetRestitutionCoeff() const;
const ::Oyster::Math::Float GetFrictionCoeff_Static() const;
const ::Oyster::Math::Float GetFrictionCoeff_Kinetic() const;
const ::Oyster::Math::Float4x4 & GetMomentOfInertia() const;
const ::Oyster::Physics3D::MomentOfInertia & GetMomentOfInertia() const;
const ::Oyster::Math::Float4 & GetReach() const;
::Oyster::Math::Float4 GetSize() const;
const ::Oyster::Math::Float4 & GetCenterPosition() const;
@ -87,8 +88,8 @@ namespace Oyster { namespace Physics
void SetMass_KeepVelocity( ::Oyster::Math::Float m );
void SetRestitutionCoeff( ::Oyster::Math::Float e );
void SetFrictionCoeff( ::Oyster::Math::Float staticU, ::Oyster::Math::Float kineticU );
void SetMomentOfInertia_KeepMomentum( const ::Oyster::Math::Float4x4 &tensor );
void SetMomentOfInertia_KeepVelocity( const ::Oyster::Math::Float4x4 &tensor );
void SetMomentOfInertia_KeepMomentum( const ::Oyster::Physics3D::MomentOfInertia &tensor );
void SetMomentOfInertia_KeepVelocity( const ::Oyster::Physics3D::MomentOfInertia &tensor );
void SetSize( const ::Oyster::Math::Float4 &size );
void SetReach( const ::Oyster::Math::Float4 &halfSize );
void SetCenterPosition( const ::Oyster::Math::Float4 &centerPos );
@ -115,7 +116,7 @@ namespace Oyster { namespace Physics
private:
::Oyster::Math::Float mass, restitutionCoeff, staticFrictionCoeff, kineticFrictionCoeff;
::Oyster::Math::Float4x4 inertiaTensor;
::Oyster::Physics3D::MomentOfInertia inertiaTensor;
::Oyster::Math::Float4 reach, centerPos, angularAxis;
::Oyster::Math::Float4 linearMomentum, angularMomentum;
::Oyster::Math::Float4 linearImpulse, angularImpulse;

View File

@ -35,7 +35,7 @@ namespace std
// x2
template<typename ScalarType>
::LinearAlgebra::Matrix2x2<ScalarType> operator * ( const ::LinearAlgebra::Matrix2x2<ScalarType> &left, const ::LinearAlgebra::Matrix2x2<ScalarType> &right )
inline ::LinearAlgebra::Matrix2x2<ScalarType> operator * ( const ::LinearAlgebra::Matrix2x2<ScalarType> &left, const ::LinearAlgebra::Matrix2x2<ScalarType> &right )
{
return ::LinearAlgebra::Matrix2x2<ScalarType>( (left.m11 * right.m11) + (left.m12 * right.m21),
(left.m11 * right.m12) + (left.m12 * right.m22),
@ -44,14 +44,14 @@ template<typename ScalarType>
}
template<typename ScalarType>
::LinearAlgebra::Vector2<ScalarType> operator * ( const ::LinearAlgebra::Matrix2x2<ScalarType> &matrix, const ::LinearAlgebra::Vector2<ScalarType> &vector )
inline ::LinearAlgebra::Vector2<ScalarType> operator * ( const ::LinearAlgebra::Matrix2x2<ScalarType> &matrix, const ::LinearAlgebra::Vector2<ScalarType> &vector )
{
return ::LinearAlgebra::Vector2<ScalarType>( (matrix.m11 * vector.x) + (matrix.m12 * vector.y),
(matrix.m21 * vector.x) + (matrix.m22 * vector.y) );
}
template<typename ScalarType>
::LinearAlgebra::Vector2<ScalarType> operator * ( const ::LinearAlgebra::Vector2<ScalarType> &vector, const ::LinearAlgebra::Matrix2x2<ScalarType> &left )
inline ::LinearAlgebra::Vector2<ScalarType> operator * ( const ::LinearAlgebra::Vector2<ScalarType> &vector, const ::LinearAlgebra::Matrix2x2<ScalarType> &left )
{
return ::LinearAlgebra::Vector2<ScalarType>( (vector.x * matrix.m11) + (vector.y * matrix.m21),
(vector.x * matrix.m12) + (vector.y * matrix.m22) );
@ -60,7 +60,7 @@ template<typename ScalarType>
// x3
template<typename ScalarType>
::LinearAlgebra::Matrix3x3<ScalarType> operator * ( const ::LinearAlgebra::Matrix3x3<ScalarType> &left, const ::LinearAlgebra::Matrix3x3<ScalarType> &right )
inline ::LinearAlgebra::Matrix3x3<ScalarType> operator * ( const ::LinearAlgebra::Matrix3x3<ScalarType> &left, const ::LinearAlgebra::Matrix3x3<ScalarType> &right )
{
return ::LinearAlgebra::Matrix3x3<ScalarType>( (left.m11 * right.m11) + (left.m12 * right.m21) + (left.m13 * right.m31), (left.m11 * right.m12) + (left.m12 * right.m22) + (left.m13 * right.m32), (left.m11 * right.m13) + (left.m12 * right.m23) + (left.m13 * right.m33),
(left.m21 * right.m11) + (left.m22 * right.m21) + (left.m23 * right.m31), (left.m21 * right.m12) + (left.m22 * right.m22) + (left.m23 * right.m32), (left.m21 * right.m13) + (left.m22 * right.m23) + (left.m23 * right.m33),
@ -68,7 +68,7 @@ template<typename ScalarType>
}
template<typename ScalarType>
::LinearAlgebra::Vector3<ScalarType> operator * ( const ::LinearAlgebra::Matrix3x3<ScalarType> &matrix, const ::LinearAlgebra::Vector3<ScalarType> &vector )
inline ::LinearAlgebra::Vector3<ScalarType> operator * ( const ::LinearAlgebra::Matrix3x3<ScalarType> &matrix, const ::LinearAlgebra::Vector3<ScalarType> &vector )
{
return ::LinearAlgebra::Vector3<ScalarType>( (matrix.m11 * vector.x) + (matrix.m12 * vector.y) + (matrix.m13 * vector.z),
(matrix.m21 * vector.x) + (matrix.m22 * vector.y) + (matrix.m23 * vector.z),
@ -76,7 +76,7 @@ template<typename ScalarType>
}
template<typename ScalarType>
::LinearAlgebra::Vector3<ScalarType> operator * ( const ::LinearAlgebra::Vector3<ScalarType> &vector, const ::LinearAlgebra::Matrix3x3<ScalarType> &left )
inline ::LinearAlgebra::Vector3<ScalarType> operator * ( const ::LinearAlgebra::Vector3<ScalarType> &vector, const ::LinearAlgebra::Matrix3x3<ScalarType> &left )
{
return ::LinearAlgebra::Vector3<ScalarType>( (vector.x * matrix.m11) + (vector.y * matrix.m21) + (vector.z * matrix.m31),
(vector.x * matrix.m12) + (vector.y * matrix.m22) + (vector.z * matrix.m32),
@ -86,7 +86,7 @@ template<typename ScalarType>
// x4
template<typename ScalarType>
::LinearAlgebra::Matrix4x4<ScalarType> operator * ( const ::LinearAlgebra::Matrix4x4<ScalarType> &left, const ::LinearAlgebra::Matrix4x4<ScalarType> &right )
inline ::LinearAlgebra::Matrix4x4<ScalarType> operator * ( const ::LinearAlgebra::Matrix4x4<ScalarType> &left, const ::LinearAlgebra::Matrix4x4<ScalarType> &right )
{
return ::LinearAlgebra::Matrix4x4<ScalarType>( (left.m11 * right.m11) + (left.m12 * right.m21) + (left.m13 * right.m31) + (left.m14 * right.m41), (left.m11 * right.m12) + (left.m12 * right.m22) + (left.m13 * right.m32) + (left.m14 * right.m42), (left.m11 * right.m13) + (left.m12 * right.m23) + (left.m13 * right.m33) + (left.m14 * right.m43), (left.m11 * right.m14) + (left.m12 * right.m24) + (left.m13 * right.m34) + (left.m14 * right.m44),
(left.m21 * right.m11) + (left.m22 * right.m21) + (left.m23 * right.m31) + (left.m24 * right.m41), (left.m21 * right.m12) + (left.m22 * right.m22) + (left.m23 * right.m32) + (left.m24 * right.m42), (left.m21 * right.m13) + (left.m22 * right.m23) + (left.m23 * right.m33) + (left.m24 * right.m43), (left.m21 * right.m14) + (left.m22 * right.m24) + (left.m23 * right.m34) + (left.m24 * right.m44),
@ -95,7 +95,7 @@ template<typename ScalarType>
}
template<typename ScalarType>
::LinearAlgebra::Vector4<ScalarType> operator * ( const ::LinearAlgebra::Matrix4x4<ScalarType> &matrix, const ::LinearAlgebra::Vector4<ScalarType> &vector )
inline ::LinearAlgebra::Vector4<ScalarType> operator * ( const ::LinearAlgebra::Matrix4x4<ScalarType> &matrix, const ::LinearAlgebra::Vector4<ScalarType> &vector )
{
return ::LinearAlgebra::Vector4<ScalarType>( (matrix.m11 * vector.x) + (matrix.m12 * vector.y) + (matrix.m13 * vector.z) + (matrix.m14 * vector.w),
(matrix.m21 * vector.x) + (matrix.m22 * vector.y) + (matrix.m23 * vector.z) + (matrix.m24 * vector.w),
@ -104,7 +104,7 @@ template<typename ScalarType>
}
template<typename ScalarType>
::LinearAlgebra::Vector4<ScalarType> operator * ( const ::LinearAlgebra::Vector4<ScalarType> &vector, const ::LinearAlgebra::Matrix4x4<ScalarType> &matrix )
inline ::LinearAlgebra::Vector4<ScalarType> operator * ( const ::LinearAlgebra::Vector4<ScalarType> &vector, const ::LinearAlgebra::Matrix4x4<ScalarType> &matrix )
{
return ::LinearAlgebra::Vector4<ScalarType>( (vector.x * matrix.m11) + (vector.y * matrix.m21) + (vector.z * matrix.m31) + (vector.w * matrix.m41),
(vector.x * matrix.m12) + (vector.y * matrix.m22) + (vector.z * matrix.m32) + (vector.w * matrix.m42),

View File

@ -163,12 +163,18 @@ namespace LinearAlgebra
Vector4<ScalarType> GetRowVector( unsigned int rowID ) const;
const Vector4<ScalarType> & GetColumnVector( unsigned int colID ) const;
};
}
template<typename ScalarType> LinearAlgebra::Matrix2x2<ScalarType> operator * ( const ScalarType &left, const LinearAlgebra::Matrix2x2<ScalarType> &right );
template<typename ScalarType> LinearAlgebra::Matrix3x3<ScalarType> operator * ( const ScalarType &left, const LinearAlgebra::Matrix3x3<ScalarType> &right );
template<typename ScalarType> LinearAlgebra::Matrix4x4<ScalarType> operator * ( const ScalarType &left, const LinearAlgebra::Matrix4x4<ScalarType> &right );
///////////////////////////////////////////////////////////////////////////////////
// Body
///////////////////////////////////////////////////////////////////////////////////
namespace LinearAlgebra
{
// Matrix2x2<ScalarType> ///////////////////////////////////////
template<typename ScalarType>
@ -753,4 +759,22 @@ namespace LinearAlgebra
{ return this->v[colID]; }
}
template<typename ScalarType>
inline LinearAlgebra::Matrix2x2<ScalarType> operator * ( const ScalarType &left, const LinearAlgebra::Matrix2x2<ScalarType> &right )
{
return right * left;
}
template<typename ScalarType>
inline LinearAlgebra::Matrix3x3<ScalarType> operator * ( const ScalarType &left, const LinearAlgebra::Matrix3x3<ScalarType> &right )
{
return right * left;
}
template<typename ScalarType>
inline LinearAlgebra::Matrix4x4<ScalarType> operator * ( const ScalarType &left, const LinearAlgebra::Matrix4x4<ScalarType> &right )
{
return right * left;
}
#endif

View File

@ -66,55 +66,29 @@ namespace Oyster { namespace Math //! Oyster's native math library
inline ::Oyster::Math::Float2 & operator *= ( ::Oyster::Math::Float2 &left, const ::Oyster::Math::Float2 &right )
{
left.x *= right.x;
left.y *= right.y;
return left;
return left.PiecewiseMultiplicationAdd( right );
}
inline ::Oyster::Math::Float2 operator * ( const ::Oyster::Math::Float2 &left, const ::Oyster::Math::Float2 &right )
{ return ::Oyster::Math::Float2(left) *= right; }
inline ::Oyster::Math::Float2 operator * ( const ::Oyster::Math::Float &left, const ::Oyster::Math::Float2 &right )
{ return ::Oyster::Math::Float2(right) *= left; }
{
return left.PiecewiseMultiplication( right );
}
inline ::Oyster::Math::Float3 & operator *= ( ::Oyster::Math::Float3 &left, const ::Oyster::Math::Float3 &right )
{
left.x *= right.x;
left.y *= right.y;
left.z *= right.z;
return left;
return left.PiecewiseMultiplicationAdd( right );
}
inline ::Oyster::Math::Float3 operator * ( const ::Oyster::Math::Float3 &left, const ::Oyster::Math::Float3 &right )
{ return ::Oyster::Math::Float3(left) *= right; }
inline ::Oyster::Math::Float3 operator * ( const ::Oyster::Math::Float &left, const ::Oyster::Math::Float3 &right )
{ return ::Oyster::Math::Float3(right) *= left; }
{
return left.PiecewiseMultiplication( right );
}
inline ::Oyster::Math::Float4 & operator *= ( ::Oyster::Math::Float4 &left, const ::Oyster::Math::Float4 &right )
{
left.x *= right.x;
left.y *= right.y;
left.z *= right.z;
left.w *= right.w;
return left;
return left.PiecewiseMultiplicationAdd( right );
}
inline ::Oyster::Math::Float4 operator * ( const ::Oyster::Math::Float4 &left, const ::Oyster::Math::Float4 &right )
{ return ::Oyster::Math::Float4(left) *= right; }
inline ::Oyster::Math::Float4 operator * ( const ::Oyster::Math::Float &left, const ::Oyster::Math::Float4 &right )
{ return ::Oyster::Math::Float4(right) *= left; }
inline ::Oyster::Math::Float2x2 operator * ( const ::Oyster::Math::Float &left, const ::Oyster::Math::Float2x2 &right )
{ return ::Oyster::Math::Float2x2(right) *= left; }
inline ::Oyster::Math::Float3x3 operator * ( const ::Oyster::Math::Float &left, const ::Oyster::Math::Float3x3 &right )
{ return ::Oyster::Math::Float3x3(right) *= left; }
inline ::Oyster::Math::Float4x4 operator * ( const ::Oyster::Math::Float &left, const ::Oyster::Math::Float4x4 &right )
{ return ::Oyster::Math::Float4x4(right) *= left; }
namespace Oyster { namespace Math2D //! Oyster's native math library specialized for 2D
{
using namespace ::Oyster::Math; // deliberate inheritance from ::Oyster::Math namespace

View File

@ -57,6 +57,12 @@ namespace LinearAlgebra
ScalarType GetMagnitude( ) const;
ScalarType Dot( const Vector2<ScalarType> &vector ) const;
//! @return (a.x * b.x, a.y * b.y)
Vector2<ScalarType> PiecewiseMultiplication( const Vector2<ScalarType> &vector ) const;
//! @return a = (a.x * b.x, a.y * b.y)
Vector2<ScalarType> & PiecewiseMultiplicationAdd( const Vector2<ScalarType> &vector );
Vector2<ScalarType> & Normalize( );
Vector2<ScalarType> GetNormalized( ) const;
};
@ -112,6 +118,12 @@ namespace LinearAlgebra
ScalarType Dot( const Vector3<ScalarType> &vector ) const;
Vector3<ScalarType> Cross( const Vector3<ScalarType> &vector ) const;
//! @return (a.x * b.x, a.y * b.y, a.z * b.z)
Vector3<ScalarType> PiecewiseMultiplication( const Vector3<ScalarType> &vector ) const;
//! @return a = (a.x * b.x, a.y * b.y, a.z * b.z)
Vector3<ScalarType> & PiecewiseMultiplicationAdd( const Vector3<ScalarType> &vector );
Vector3<ScalarType> & Normalize( );
Vector3<ScalarType> GetNormalized( ) const;
};
@ -169,14 +181,27 @@ namespace LinearAlgebra
ScalarType GetMagnitude( ) const;
ScalarType Dot( const Vector4<ScalarType> &vector ) const;
//! @return (a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w)
Vector4<ScalarType> PiecewiseMultiplication( const Vector4<ScalarType> &vector ) const;
//! @return a = (a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w)
Vector4<ScalarType> & PiecewiseMultiplicationAdd( const Vector4<ScalarType> &vector );
Vector4<ScalarType> & Normalize( );
Vector4<ScalarType> GetNormalized( ) const;
};
}
template<typename ScalarType> ::LinearAlgebra::Vector2<ScalarType> operator * ( const ScalarType &left, const ::LinearAlgebra::Vector2<ScalarType> &right );
template<typename ScalarType> ::LinearAlgebra::Vector3<ScalarType> operator * ( const ScalarType &left, const ::LinearAlgebra::Vector3<ScalarType> &right );
template<typename ScalarType> ::LinearAlgebra::Vector4<ScalarType> operator * ( const ScalarType &left, const ::LinearAlgebra::Vector4<ScalarType> &right );
///////////////////////////////////////////////////////////////////////////////////
// Body
///////////////////////////////////////////////////////////////////////////////////
namespace LinearAlgebra
{
// Vector2<ScalarType> ///////////////////////////////////////
template<typename ScalarType> const Vector2<ScalarType> Vector2<ScalarType>::null = Vector2<ScalarType>( 0, 0 );
@ -184,22 +209,22 @@ namespace LinearAlgebra
template<typename ScalarType> const Vector2<ScalarType> Vector2<ScalarType>::standard_unit_y = Vector2<ScalarType>( 0, 1 );
template<typename ScalarType>
Vector2<ScalarType>::Vector2( ) : x(), y() {}
inline Vector2<ScalarType>::Vector2( ) : x(), y() {}
template<typename ScalarType>
Vector2<ScalarType>::Vector2( const Vector2<ScalarType> &vector )
inline Vector2<ScalarType>::Vector2( const Vector2<ScalarType> &vector )
{ this->x = vector.x; this->y = vector.y; }
template<typename ScalarType>
Vector2<ScalarType>::Vector2( const ScalarType &_element )
inline Vector2<ScalarType>::Vector2( const ScalarType &_element )
{ this->x = this->y = _element; }
template<typename ScalarType>
Vector2<ScalarType>::Vector2( const ScalarType _element[2] )
inline Vector2<ScalarType>::Vector2( const ScalarType _element[2] )
{ this->x = _element[0]; this->y = _element[1]; }
template<typename ScalarType>
Vector2<ScalarType>::Vector2( const ScalarType &_x, const ScalarType &_y )
inline Vector2<ScalarType>::Vector2( const ScalarType &_x, const ScalarType &_y )
{ this->x = _x; this->y = _y; }
template<typename ScalarType>
@ -227,7 +252,7 @@ namespace LinearAlgebra
{ return this->element[i]; }
template<typename ScalarType>
Vector2<ScalarType> & Vector2<ScalarType>::operator = ( const Vector2<ScalarType> &vector )
inline Vector2<ScalarType> & Vector2<ScalarType>::operator = ( const Vector2<ScalarType> &vector )
{
this->element[0] = vector.element[0];
this->element[1] = vector.element[1];
@ -235,7 +260,7 @@ namespace LinearAlgebra
}
template<typename ScalarType>
Vector2<ScalarType> & Vector2<ScalarType>::operator = ( const ScalarType _element[2] )
inline Vector2<ScalarType> & Vector2<ScalarType>::operator = ( const ScalarType _element[2] )
{
this->element[0] = _element[0];
this->element[1] = _element[1];
@ -243,7 +268,7 @@ namespace LinearAlgebra
}
template<typename ScalarType>
Vector2<ScalarType> & Vector2<ScalarType>::operator *= ( const ScalarType &scalar )
inline Vector2<ScalarType> & Vector2<ScalarType>::operator *= ( const ScalarType &scalar )
{
this->element[0] *= scalar;
this->element[1] *= scalar;
@ -251,7 +276,7 @@ namespace LinearAlgebra
}
template<typename ScalarType>
Vector2<ScalarType> & Vector2<ScalarType>::operator /= ( const ScalarType &scalar )
inline Vector2<ScalarType> & Vector2<ScalarType>::operator /= ( const ScalarType &scalar )
{
this->element[0] /= scalar;
this->element[1] /= scalar;
@ -259,7 +284,7 @@ namespace LinearAlgebra
}
template<typename ScalarType>
Vector2<ScalarType> & Vector2<ScalarType>::operator += ( const Vector2<ScalarType> &vector )
inline Vector2<ScalarType> & Vector2<ScalarType>::operator += ( const Vector2<ScalarType> &vector )
{
this->element[0] += vector.element[0];
this->element[1] += vector.element[1];
@ -267,7 +292,7 @@ namespace LinearAlgebra
}
template<typename ScalarType>
Vector2<ScalarType> & Vector2<ScalarType>::operator -= ( const Vector2<ScalarType> &vector )
inline Vector2<ScalarType> & Vector2<ScalarType>::operator -= ( const Vector2<ScalarType> &vector )
{
this->element[0] -= vector.element[0];
this->element[1] -= vector.element[1];
@ -295,7 +320,7 @@ namespace LinearAlgebra
{ return Vector2<ScalarType>(-this->x, -this->y); }
template<typename ScalarType>
bool Vector2<ScalarType>::operator == ( const Vector2<ScalarType> &vector ) const
inline bool Vector2<ScalarType>::operator == ( const Vector2<ScalarType> &vector ) const
{
if( this->x != vector.x ) return false;
if( this->y != vector.y ) return false;
@ -303,7 +328,7 @@ namespace LinearAlgebra
}
template<typename ScalarType>
bool Vector2<ScalarType>::operator != ( const Vector2<ScalarType> &vector ) const
inline bool Vector2<ScalarType>::operator != ( const Vector2<ScalarType> &vector ) const
{
if( this->x != vector.x ) return true;
if( this->y != vector.y ) return true;
@ -319,7 +344,7 @@ namespace LinearAlgebra
{ return (ScalarType) ::sqrt( this->Dot(*this) ); }
template<typename ScalarType>
ScalarType Vector2<ScalarType>::Dot( const Vector2<ScalarType> &vector ) const
inline ScalarType Vector2<ScalarType>::Dot( const Vector2<ScalarType> &vector ) const
{
ScalarType value = 0;
value += this->element[0] * vector.element[0];
@ -327,6 +352,20 @@ namespace LinearAlgebra
return value;
}
template<typename ScalarType>
inline Vector2<ScalarType> Vector2<ScalarType>::PiecewiseMultiplication( const Vector2<ScalarType> &vector ) const
{
return Vector2<ScalarType>( this->x * vector.x, this->y * vector.y );
}
template<typename ScalarType>
inline Vector2<ScalarType> & Vector2<ScalarType>::PiecewiseMultiplicationAdd( const Vector2<ScalarType> &vector )
{
this->x *= vector.x;
this->y *= vector.y;
return *this;
}
template<typename ScalarType>
inline Vector2<ScalarType> & Vector2<ScalarType>::Normalize( )
{ return (*this) /= this->GetLength(); }
@ -343,26 +382,26 @@ namespace LinearAlgebra
template<typename ScalarType> const Vector3<ScalarType> Vector3<ScalarType>::standard_unit_z = Vector3<ScalarType>( 0, 0, 1 );
template<typename ScalarType>
Vector3<ScalarType>::Vector3( ) : x(), y(), z() {}
inline Vector3<ScalarType>::Vector3( ) : x(), y(), z() {}
template<typename ScalarType>
Vector3<ScalarType>::Vector3( const Vector3<ScalarType> &vector )
inline Vector3<ScalarType>::Vector3( const Vector3<ScalarType> &vector )
{ this->x = vector.x; this->y = vector.y; this->z = vector.z; }
template<typename ScalarType>
Vector3<ScalarType>::Vector3( const Vector2<ScalarType> &vector, const ScalarType &_z )
inline Vector3<ScalarType>::Vector3( const Vector2<ScalarType> &vector, const ScalarType &_z )
{ this->x = vector.x; this->y = vector.y; this->z = _z; }
template<typename ScalarType>
Vector3<ScalarType>::Vector3( const ScalarType &_element )
inline Vector3<ScalarType>::Vector3( const ScalarType &_element )
{ this->x = this->y = this->z = _element; }
template<typename ScalarType>
Vector3<ScalarType>::Vector3( const ScalarType _element[3] )
inline Vector3<ScalarType>::Vector3( const ScalarType _element[3] )
{ this->x = _element[0]; this->y = _element[1]; this->z = _element[2]; }
template<typename ScalarType>
Vector3<ScalarType>::Vector3( const ScalarType &_x, const ScalarType &_y, const ScalarType &_z )
inline Vector3<ScalarType>::Vector3( const ScalarType &_x, const ScalarType &_y, const ScalarType &_z )
{ this->x = _x; this->y = _y; this->z = _z; }
template<typename ScalarType>
@ -382,7 +421,7 @@ namespace LinearAlgebra
{ return this->element[i]; }
template<typename ScalarType>
Vector3<ScalarType> & Vector3<ScalarType>::operator = ( const Vector3<ScalarType> &vector )
inline Vector3<ScalarType> & Vector3<ScalarType>::operator = ( const Vector3<ScalarType> &vector )
{
this->element[0] = vector.element[0];
this->element[1] = vector.element[1];
@ -391,7 +430,7 @@ namespace LinearAlgebra
}
template<typename ScalarType>
Vector3<ScalarType> & Vector3<ScalarType>::operator = ( const ScalarType element[3] )
inline Vector3<ScalarType> & Vector3<ScalarType>::operator = ( const ScalarType element[3] )
{
this->element[0] = element[0];
this->element[1] = element[1];
@ -400,7 +439,7 @@ namespace LinearAlgebra
}
template<typename ScalarType>
Vector3<ScalarType> & Vector3<ScalarType>::operator *= ( const ScalarType &scalar )
inline Vector3<ScalarType> & Vector3<ScalarType>::operator *= ( const ScalarType &scalar )
{
this->element[0] *= scalar;
this->element[1] *= scalar;
@ -409,7 +448,7 @@ namespace LinearAlgebra
}
template<typename ScalarType>
Vector3<ScalarType> & Vector3<ScalarType>::operator /= ( const ScalarType &scalar )
inline Vector3<ScalarType> & Vector3<ScalarType>::operator /= ( const ScalarType &scalar )
{
this->element[0] /= scalar;
this->element[1] /= scalar;
@ -418,7 +457,7 @@ namespace LinearAlgebra
}
template<typename ScalarType>
Vector3<ScalarType> & Vector3<ScalarType>::operator += ( const Vector3<ScalarType> &vector )
inline Vector3<ScalarType> & Vector3<ScalarType>::operator += ( const Vector3<ScalarType> &vector )
{
this->element[0] += vector.element[0];
this->element[1] += vector.element[1];
@ -427,7 +466,7 @@ namespace LinearAlgebra
}
template<typename ScalarType>
Vector3<ScalarType> & Vector3<ScalarType>::operator -= ( const Vector3<ScalarType> &vector )
inline Vector3<ScalarType> & Vector3<ScalarType>::operator -= ( const Vector3<ScalarType> &vector )
{
this->element[0] -= vector.element[0];
this->element[1] -= vector.element[1];
@ -456,7 +495,7 @@ namespace LinearAlgebra
{ return Vector3<ScalarType>(-this->x, -this->y, -this->z); }
template<typename ScalarType>
bool Vector3<ScalarType>::operator == ( const Vector3<ScalarType> &vector ) const
inline bool Vector3<ScalarType>::operator == ( const Vector3<ScalarType> &vector ) const
{
if( this->x != vector.x ) return false;
if( this->y != vector.y ) return false;
@ -465,7 +504,7 @@ namespace LinearAlgebra
}
template<typename ScalarType>
bool Vector3<ScalarType>::operator != ( const Vector3<ScalarType> &vector ) const
inline bool Vector3<ScalarType>::operator != ( const Vector3<ScalarType> &vector ) const
{
if( this->x != vector.x ) return true;
if( this->y != vector.y ) return true;
@ -482,7 +521,7 @@ namespace LinearAlgebra
{ return (ScalarType) ::sqrt( this->Dot(*this) ); }
template<typename ScalarType>
ScalarType Vector3<ScalarType>::Dot( const Vector3<ScalarType> &vector ) const
inline ScalarType Vector3<ScalarType>::Dot( const Vector3<ScalarType> &vector ) const
{
ScalarType value = 0;
value += this->element[0] * vector.element[0];
@ -492,13 +531,28 @@ namespace LinearAlgebra
}
template<typename ScalarType>
Vector3<ScalarType> Vector3<ScalarType>::Cross( const Vector3<ScalarType> &vector ) const
inline Vector3<ScalarType> Vector3<ScalarType>::Cross( const Vector3<ScalarType> &vector ) const
{
return Vector3<ScalarType>( (this->y*vector.z) - (this->z*vector.y),
(this->z*vector.x) - (this->x*vector.z),
(this->x*vector.y) - (this->y*vector.x) );
}
template<typename ScalarType>
inline Vector3<ScalarType> Vector3<ScalarType>::PiecewiseMultiplication( const Vector3<ScalarType> &vector ) const
{
return Vector3<ScalarType>( this->x * vector.x, this->y * vector.y, this->z * vector.z );
}
template<typename ScalarType>
inline Vector3<ScalarType> & Vector3<ScalarType>::PiecewiseMultiplicationAdd( const Vector3<ScalarType> &vector )
{
this->x *= vector.x;
this->y *= vector.y;
this->z *= vector.z;
return *this;
}
template<typename ScalarType>
inline Vector3<ScalarType> & Vector3<ScalarType>::Normalize( )
{ return (*this) /= this->GetLength(); }
@ -516,30 +570,30 @@ namespace LinearAlgebra
template<typename ScalarType> const Vector4<ScalarType> Vector4<ScalarType>::standard_unit_w = Vector4<ScalarType>( 0, 0, 0, 1 );
template<typename ScalarType>
Vector4<ScalarType>::Vector4( ) : x(), y(), z(), w() {}
inline Vector4<ScalarType>::Vector4( ) : x(), y(), z(), w() {}
template<typename ScalarType>
Vector4<ScalarType>::Vector4( const Vector4<ScalarType> &vector )
inline Vector4<ScalarType>::Vector4( const Vector4<ScalarType> &vector )
{ this->x = vector.x; this->y = vector.y; this->z = vector.z; this->w = vector.w; }
template<typename ScalarType>
Vector4<ScalarType>::Vector4( const Vector3<ScalarType> &vector, const ScalarType &_w )
inline Vector4<ScalarType>::Vector4( const Vector3<ScalarType> &vector, const ScalarType &_w )
{ this->x = vector.x; this->y = vector.y; this->z = vector.z; this->w = _w; }
template<typename ScalarType>
Vector4<ScalarType>::Vector4( const Vector2<ScalarType> &vector, const ScalarType &_z, const ScalarType &_w )
inline Vector4<ScalarType>::Vector4( const Vector2<ScalarType> &vector, const ScalarType &_z, const ScalarType &_w )
{ this->x = vector.x; this->y = vector.y; this->z = _z; this->w = _w; }
template<typename ScalarType>
Vector4<ScalarType>::Vector4( const ScalarType &_element )
inline Vector4<ScalarType>::Vector4( const ScalarType &_element )
{ this->x = this->y = this->z = this->w = _element; }
template<typename ScalarType>
Vector4<ScalarType>::Vector4( const ScalarType _element[4] )
inline Vector4<ScalarType>::Vector4( const ScalarType _element[4] )
{ this->x = _element[0]; this->y = _element[1]; this->z = _element[2]; this->w = _element[3]; }
template<typename ScalarType>
Vector4<ScalarType>::Vector4( const ScalarType &_x, const ScalarType &_y, const ScalarType &_z, const ScalarType &_w )
inline Vector4<ScalarType>::Vector4( const ScalarType &_x, const ScalarType &_y, const ScalarType &_z, const ScalarType &_w )
{ this->x = _x; this->y = _y; this->z = _z; this->w = _w; }
template<typename ScalarType>
@ -559,7 +613,7 @@ namespace LinearAlgebra
{ return this->element[i]; }
template<typename ScalarType>
Vector4<ScalarType> & Vector4<ScalarType>::operator = ( const Vector4<ScalarType> &vector )
inline Vector4<ScalarType> & Vector4<ScalarType>::operator = ( const Vector4<ScalarType> &vector )
{
this->element[0] = vector.element[0];
this->element[1] = vector.element[1];
@ -569,7 +623,7 @@ namespace LinearAlgebra
}
template<typename ScalarType>
Vector4<ScalarType> & Vector4<ScalarType>::operator = ( const ScalarType element[4] )
inline Vector4<ScalarType> & Vector4<ScalarType>::operator = ( const ScalarType element[4] )
{
this->element[0] = element[0];
this->element[1] = element[1];
@ -579,7 +633,7 @@ namespace LinearAlgebra
}
template<typename ScalarType>
Vector4<ScalarType> & Vector4<ScalarType>::operator *= ( const ScalarType &scalar )
inline Vector4<ScalarType> & Vector4<ScalarType>::operator *= ( const ScalarType &scalar )
{
this->element[0] *= scalar;
this->element[1] *= scalar;
@ -589,7 +643,7 @@ namespace LinearAlgebra
}
template<typename ScalarType>
Vector4<ScalarType> & Vector4<ScalarType>::operator /= ( const ScalarType &scalar )
inline Vector4<ScalarType> & Vector4<ScalarType>::operator /= ( const ScalarType &scalar )
{
this->element[0] /= scalar;
this->element[1] /= scalar;
@ -599,7 +653,7 @@ namespace LinearAlgebra
}
template<typename ScalarType>
Vector4<ScalarType> & Vector4<ScalarType>::operator += ( const Vector4<ScalarType> &vector )
inline Vector4<ScalarType> & Vector4<ScalarType>::operator += ( const Vector4<ScalarType> &vector )
{
this->element[0] += vector.element[0];
this->element[1] += vector.element[1];
@ -609,7 +663,7 @@ namespace LinearAlgebra
}
template<typename ScalarType>
Vector4<ScalarType> & Vector4<ScalarType>::operator -= ( const Vector4<ScalarType> &vector )
inline Vector4<ScalarType> & Vector4<ScalarType>::operator -= ( const Vector4<ScalarType> &vector )
{
this->element[0] -= vector.element[0];
this->element[1] -= vector.element[1];
@ -639,7 +693,7 @@ namespace LinearAlgebra
{ return Vector4<ScalarType>(-this->x, -this->y, -this->z, -this->w); }
template<typename ScalarType>
bool Vector4<ScalarType>::operator == ( const Vector4<ScalarType> &vector ) const
inline bool Vector4<ScalarType>::operator == ( const Vector4<ScalarType> &vector ) const
{
if( this->x != vector.x ) return false;
if( this->y != vector.y ) return false;
@ -649,7 +703,7 @@ namespace LinearAlgebra
}
template<typename ScalarType>
bool Vector4<ScalarType>::operator != ( const Vector4<ScalarType> &vector ) const
inline bool Vector4<ScalarType>::operator != ( const Vector4<ScalarType> &vector ) const
{
if( this->x != vector.x ) return true;
if( this->y != vector.y ) return true;
@ -667,7 +721,7 @@ namespace LinearAlgebra
{ return (ScalarType) ::sqrt( this->Dot(*this) ); }
template<typename ScalarType>
ScalarType Vector4<ScalarType>::Dot( const Vector4<ScalarType> &vector ) const
inline ScalarType Vector4<ScalarType>::Dot( const Vector4<ScalarType> &vector ) const
{
ScalarType value = 0;
value += this->element[0] * vector.element[0];
@ -677,6 +731,22 @@ namespace LinearAlgebra
return value;
}
template<typename ScalarType>
inline Vector4<ScalarType> Vector4<ScalarType>::PiecewiseMultiplication( const Vector4<ScalarType> &vector ) const
{
return Vector4<ScalarType>( this->x * vector.x, this->y * vector.y, this->z * vector.z, this->w * vector.w );
}
template<typename ScalarType>
inline Vector4<ScalarType> & Vector4<ScalarType>::PiecewiseMultiplicationAdd( const Vector4<ScalarType> &vector )
{
this->x *= vector.x;
this->y *= vector.y;
this->z *= vector.z;
this->w *= vector.w;
return *this;
}
template<typename ScalarType>
inline Vector4<ScalarType> & Vector4<ScalarType>::Normalize( )
{ return (*this) /= this->GetLength(); }
@ -686,4 +756,22 @@ namespace LinearAlgebra
{ return Vector4<ScalarType>(*this).Normalize(); }
}
template<typename ScalarType>
inline ::LinearAlgebra::Vector2<ScalarType> operator * ( const ScalarType &left, const ::LinearAlgebra::Vector2<ScalarType> &right )
{
return right * left;
}
template<typename ScalarType>
inline ::LinearAlgebra::Vector3<ScalarType> operator * ( const ScalarType &left, const ::LinearAlgebra::Vector3<ScalarType> &right )
{
return right * left;
}
template<typename ScalarType>
inline ::LinearAlgebra::Vector4<ScalarType> operator * ( const ScalarType &left, const ::LinearAlgebra::Vector4<ScalarType> &right )
{
return right * left;
}
#endif

View File

@ -0,0 +1,51 @@
/********************************************************************
* 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;
}
Float4 MomentOfInertia::CalculateAngularVelocity( const Quaternion &externR, const Float4 &h ) const
{
return this->CalculateAngularVelocity( externR, h, Float4() );
}
Float4 & MomentOfInertia::CalculateAngularVelocity( const Quaternion &externR, const Float4 &h, Float4 &targetMem ) const
{ // w = (R * I_R) * I_M^-1 * (R * I_R)^-1 * h
Float4x4 rotation = RotationMatrix( externR ) * RotationMatrix( this->rotation );
Float4 w = rotation.GetInverse() * h;
return targetMem = rotation * w.PiecewiseMultiplicationAdd( Float4(1.0f / this->magnitude.x, 1.0f / this->magnitude.y, 1.0f / this->magnitude.z, 0.0f) );
}
Float4 MomentOfInertia::CalculateAngularMomentum( const Quaternion &externR, const Float4 &w ) const
{
return this->CalculateAngularMomentum( externR, w, Float4() );
}
Float4 & MomentOfInertia::CalculateAngularMomentum( const Quaternion &externR, const Float4 &w, Float4 &targetMem ) const
{ // h = (R * I_R) * I_M * (R * I_R)^-1 * w
Float4x4 rotation = RotationMatrix( externR ) * RotationMatrix( this->rotation );
Float4 h = rotation.GetInverse() * w;
return targetMem = rotation * h.PiecewiseMultiplicationAdd( Float4(this->magnitude.x, this->magnitude.y, this->magnitude.z, 0.0f) );
}

View File

@ -0,0 +1,119 @@
/********************************************************************
* Created by Dan Andersson & Robin Engman 2014
********************************************************************/
#ifndef OYSTER_PHYSICS_3D_INERTIA_H
#define OYSTER_PHYSICS_3D_INERTIA_H
#include "OysterMath.h"
namespace Oyster { namespace Physics3D
{
struct MomentOfInertia
{
::Oyster::Math::Quaternion rotation;
::Oyster::Math::Float3 magnitude;
MomentOfInertia();
MomentOfInertia( const ::Oyster::Math::Quaternion &r, const ::Oyster::Math::Float3 &m );
MomentOfInertia & operator = ( const MomentOfInertia &i );
::Oyster::Math::Float4 CalculateAngularVelocity( const ::Oyster::Math::Quaternion &externR, const ::Oyster::Math::Float4 &angularMomentum ) const;
::Oyster::Math::Float4 & CalculateAngularVelocity( const ::Oyster::Math::Quaternion &externR, const ::Oyster::Math::Float4 &angularMomentum, ::Oyster::Math::Float4 &targetMem ) const;
::Oyster::Math::Float4 CalculateAngularMomentum( const ::Oyster::Math::Quaternion &externR, const ::Oyster::Math::Float4 &angularVelocity ) const;
::Oyster::Math::Float4 & CalculateAngularMomentum( const ::Oyster::Math::Quaternion &externR, const ::Oyster::Math::Float4 &angularVelocity, ::Oyster::Math::Float4 &targetMem ) const;
static ::Oyster::Math::Float CalculateSphere( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius );
static ::Oyster::Math::Float CalculateHollowSphere( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius );
static ::Oyster::Math::Float CalculateCuboidX( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float height, const ::Oyster::Math::Float depth );
static ::Oyster::Math::Float CalculateCuboidY( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float width, const ::Oyster::Math::Float depth );
static ::Oyster::Math::Float CalculateCuboidZ( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float width, const ::Oyster::Math::Float height );
static ::Oyster::Math::Float CalculateRodCenter( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float length );
static ::Oyster::Math::Float CalculateCylinderXY( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float height, const ::Oyster::Math::Float radius );
static ::Oyster::Math::Float CalculateCylinderZ( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius );
static MomentOfInertia Sphere( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius );
static MomentOfInertia HollowSphere( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius );
static MomentOfInertia Cuboid( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float height, const ::Oyster::Math::Float width, const ::Oyster::Math::Float depth );
static MomentOfInertia RodCenter( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float length );
static MomentOfInertia Cylinder( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float height, const ::Oyster::Math::Float radius );
};
inline ::Oyster::Math::Float MomentOfInertia::CalculateSphere( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius )
{
return (2.0f / 5.0f) * mass * radius * radius;
}
inline ::Oyster::Math::Float MomentOfInertia::CalculateHollowSphere( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius )
{
return (2.0f / 3.0f) * mass * radius * radius;
}
inline ::Oyster::Math::Float MomentOfInertia::CalculateCuboidX( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float height, const ::Oyster::Math::Float depth )
{
return (1.0f / 12.0f) * mass * (height * height + depth * depth);
}
inline ::Oyster::Math::Float MomentOfInertia::CalculateCuboidY( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float width, const ::Oyster::Math::Float depth )
{
return (1.0f / 12.0f) * mass * (width * width + depth * depth);
}
inline ::Oyster::Math::Float MomentOfInertia::CalculateCuboidZ( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float width, const ::Oyster::Math::Float height )
{
return (1.0f / 12.0f) * mass * (height * height + width * width);
}
inline ::Oyster::Math::Float MomentOfInertia::CalculateRodCenter( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float length )
{
return (1.0f / 12.0f) * mass * length * length;
}
inline ::Oyster::Math::Float MomentOfInertia::CalculateCylinderXY( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float height, const ::Oyster::Math::Float radius )
{
return (1.0f / 12.0f) * mass * (3.0f * radius * radius + height * height);
}
inline ::Oyster::Math::Float MomentOfInertia::CalculateCylinderZ( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius )
{
return 0.5f * mass * radius * radius;
}
inline MomentOfInertia MomentOfInertia::Sphere( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius )
{
return MomentOfInertia( ::Oyster::Math::Quaternion::identity,
::Oyster::Math::Float3(MomentOfInertia::CalculateSphere(mass, radius)) );
}
inline MomentOfInertia MomentOfInertia::HollowSphere( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius )
{
return MomentOfInertia( ::Oyster::Math::Quaternion::identity,
::Oyster::Math::Float3(MomentOfInertia::CalculateHollowSphere(mass, radius)) );
}
inline MomentOfInertia MomentOfInertia::Cuboid( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float height, const ::Oyster::Math::Float width, const ::Oyster::Math::Float depth )
{
return MomentOfInertia( ::Oyster::Math::Quaternion::identity,
::Oyster::Math::Float3(MomentOfInertia::CalculateCuboidX(mass, height, depth),
MomentOfInertia::CalculateCuboidY(mass, width, depth),
MomentOfInertia::CalculateCuboidZ(mass, height, width)) );
}
inline MomentOfInertia MomentOfInertia::RodCenter( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float length )
{
return MomentOfInertia( ::Oyster::Math::Quaternion::identity,
::Oyster::Math::Float3(MomentOfInertia::CalculateRodCenter(mass , length)) );
}
inline MomentOfInertia MomentOfInertia::Cylinder( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float height, const ::Oyster::Math::Float radius )
{
::Oyster::Math::Float cylinderXY = MomentOfInertia::CalculateCylinderXY( mass , height, radius );
return MomentOfInertia( ::Oyster::Math::Quaternion::identity,
::Oyster::Math::Float3(cylinderXY, cylinderXY,
MomentOfInertia::CalculateCylinderZ(mass, radius)) );
}
} }
#endif

View File

@ -155,6 +155,7 @@
<ClInclude Include="FluidDrag.h" />
<ClInclude Include="Frustrum.h" />
<ClInclude Include="ICollideable.h" />
<ClInclude Include="Inertia.h" />
<ClInclude Include="Line.h" />
<ClInclude Include="OysterCollision3D.h" />
<ClInclude Include="OysterPhysics3D.h" />
@ -174,6 +175,7 @@
<ClCompile Include="FluidDrag.cpp" />
<ClCompile Include="Frustrum.cpp" />
<ClCompile Include="ICollideable.cpp" />
<ClCompile Include="Inertia.cpp" />
<ClCompile Include="Line.cpp" />
<ClCompile Include="OysterCollision3D.cpp" />
<ClCompile Include="Particle.cpp" />

View File

@ -78,6 +78,9 @@
<ClInclude Include="RigidBody_Inline.h">
<Filter>Header Files\Physics</Filter>
</ClInclude>
<ClInclude Include="Inertia.h">
<Filter>Header Files\Physics</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Box.cpp">
@ -125,5 +128,8 @@
<ClCompile Include="Particle.cpp">
<Filter>Source Files\Physics</Filter>
</ClCompile>
<ClCompile Include="Inertia.cpp">
<Filter>Source Files\Physics</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -23,7 +23,7 @@ RigidBody::RigidBody( )
this->frictionCoeff_Static = 0.5f;
this->frictionCoeff_Kinetic = 1.0f;
this->mass = 10;
this->momentOfInertiaTensor = Float4x4::identity;
this->momentOfInertiaTensor = MomentOfInertia();
this->rotation = Quaternion::identity;
}
@ -51,17 +51,18 @@ void RigidBody::Update_LeapFrog( Float updateFrameLength )
// updating the linear
// ds = dt * Formula::LinearVelocity( m, avg_G ) = dt * avg_G / m = (dt / m) * avg_G
this->centerPos += ( updateFrameLength / this->mass ) * AverageWithDelta( this->momentum_Linear, this->impulse_Linear );
// updating the angular
Float4x4 rotationMatrix; ::Oyster::Math3D::RotationMatrix( this->rotation, rotationMatrix );
//Float4x4 rotationMatrix; ::Oyster::Math3D::RotationMatrix( this->rotation, rotationMatrix );
// Important! The member data is all world data except the Inertia tensor. Thus a new InertiaTensor needs to be created to be compatible with the rest of the world data.
Float4x4 wMomentOfInertiaTensor = TransformMatrix( rotationMatrix, this->momentOfInertiaTensor ); // RI
//Float4x4 wMomentOfInertiaTensor = TransformMatrix( rotationMatrix, this->momentOfInertiaTensor ); // RI
// dO = dt * Formula::AngularVelocity( (RI)^-1, avg_H ) = dt * (RI)^-1 * avg_H
//! HACK: @todo Rotation temporary disabled
//this->axis += Radian( Formula::AngularVelocity(wMomentOfInertiaTensor.GetInverse(), AverageWithDelta(this->momentum_Angular, this->impulse_Angular)) );
//this->rotation = Rotation( this->axis );
this->axis += this->momentOfInertiaTensor.CalculateAngularVelocity( this->rotation, AverageWithDelta(this->momentum_Angular, this->impulse_Angular) );
this->rotation = Rotation( this->axis );
// update momentums and clear impulse_Linear and impulse_Angular
this->momentum_Linear += this->impulse_Linear;
@ -78,11 +79,12 @@ void RigidBody::Predict_LeapFrog( Float4 &outDeltaPos, Float4 &outDeltaAxis, con
outDeltaPos = ( deltaTime / this->mass ) * AverageWithDelta( this->momentum_Linear, actingLinearImpulse );
// updating the angular
Float4x4 rotationMatrix; ::Oyster::Math3D::RotationMatrix( this->rotation, rotationMatrix );
Float4x4 wMomentOfInertiaTensor = TransformMatrix( rotationMatrix, this->momentOfInertiaTensor ); // RI
//Float4x4 rotationMatrix; ::Oyster::Math3D::RotationMatrix( this->rotation, rotationMatrix );
//Float4x4 wMomentOfInertiaTensor = TransformMatrix( rotationMatrix, this->momentOfInertiaTensor ); // RI
// dO = dt * Formula::AngularVelocity( (RI)^-1, avg_H ) = dt * (RI)^-1 * avg_H
outDeltaAxis = Formula::AngularVelocity( wMomentOfInertiaTensor.GetInverse(), AverageWithDelta(this->momentum_Angular, actingAngularImpulse) );
//outDeltaAxis = Formula::AngularVelocity( wMomentOfInertiaTensor.GetInverse(), AverageWithDelta(this->momentum_Angular, actingAngularImpulse) );
outDeltaAxis = this->momentOfInertiaTensor.CalculateAngularVelocity( this->rotation, AverageWithDelta(this->momentum_Angular, this->impulse_Angular) );
}
void RigidBody::Move( const Float4 &deltaPos, const Float4 &deltaAxis )
@ -106,7 +108,7 @@ void RigidBody::ApplyImpulse( const Float4 &worldJ, const Float4 &atWorldPos )
}
}
const Float4x4 & RigidBody::GetMomentOfInertia() const
const MomentOfInertia & RigidBody::GetMomentOfInertia() const
{ // by Dan Andersson
return this->momentOfInertiaTensor;
}
@ -143,7 +145,7 @@ Float4 RigidBody::GetVelocity_Linear() const
Float4 RigidBody::GetVelocity_Angular() const
{ // by Dan Andersson
return Formula::AngularVelocity( this->momentOfInertiaTensor.GetInverse(), this->momentum_Angular );
return this->momentOfInertiaTensor.CalculateAngularVelocity( this->rotation, this->momentum_Angular );
}
Float4 RigidBody::GetLinearMomentum( const Float4 &atWorldPos ) const
@ -151,24 +153,16 @@ Float4 RigidBody::GetLinearMomentum( const Float4 &atWorldPos ) const
return this->momentum_Linear + Formula::TangentialLinearMomentum( this->momentum_Angular, atWorldPos - this->centerPos );
}
void RigidBody::SetMomentOfInertia_KeepVelocity( const Float4x4 &localTensorI )
void RigidBody::SetMomentOfInertia_KeepVelocity( const MomentOfInertia &localTensorI )
{ // by Dan Andersson
if( localTensorI.GetDeterminant() != 0.0f )
{ // insanity check! MomentOfInertiaTensor must be invertable
Float4x4 rotationMatrix; RotationMatrix( this->rotation, rotationMatrix );
Float4 w = Formula::AngularVelocity( (rotationMatrix * this->momentOfInertiaTensor).GetInverse(), this->momentum_Angular );
this->momentOfInertiaTensor = localTensorI;
this->momentum_Angular = Formula::AngularMomentum( rotationMatrix * localTensorI, w );
}
Float4 w = this->momentOfInertiaTensor.CalculateAngularVelocity( this->rotation, this->momentum_Angular );
this->momentOfInertiaTensor = localTensorI;
this->momentum_Angular = this->momentOfInertiaTensor.CalculateAngularVelocity( this->rotation, w );
}
void RigidBody::SetMomentOfInertia_KeepMomentum( const Float4x4 &localTensorI )
void RigidBody::SetMomentOfInertia_KeepMomentum( const MomentOfInertia &localTensorI )
{ // by Dan Andersson
if( localTensorI.GetDeterminant() != 0.0f )
{ // insanity check! MomentOfInertiaTensor must be invertable
this->momentOfInertiaTensor = localTensorI;
}
this->momentOfInertiaTensor = localTensorI;
}
void RigidBody::SetMass_KeepVelocity( const Float &m )
@ -217,13 +211,13 @@ void RigidBody::SetVelocity_Linear( const Float4 &worldV )
void RigidBody::SetVelocity_Linear( const Float4 &worldV, const Float4 &atWorldPos )
{ // by Dan Andersson
Float4 worldOffset = atWorldPos - this->centerPos;
this->momentum_Linear = Formula::LinearMomentum( this->mass, VectorProjection(worldV, worldOffset) );
this->momentum_Angular = Formula::AngularMomentum( RotationMatrix(this->rotation) * this->momentOfInertiaTensor, Formula::AngularVelocity(worldV, worldOffset) );
this->momentum_Linear = Formula::LinearMomentum( this->mass, VectorProjection(worldV, worldOffset) );
this->momentum_Angular = this->momentOfInertiaTensor.CalculateAngularMomentum( this->rotation, Formula::AngularVelocity(worldV, worldOffset) );
}
void RigidBody::SetVelocity_Angular( const Float4 &worldW )
{ // by Dan Andersson
this->momentum_Angular = Formula::AngularMomentum( this->momentOfInertiaTensor, worldW );
this->momentum_Angular = this->momentOfInertiaTensor.CalculateAngularMomentum( this->rotation, worldW );
}
void RigidBody::SetImpulse_Linear( const Float4 &worldJ, const Float4 &atWorldPos )

View File

@ -8,6 +8,7 @@
#include "OysterMath.h"
#include "OysterCollision3D.h"
#include "OysterPhysics3D.h"
#include "Inertia.h"
namespace Oyster { namespace Physics3D
{
@ -42,23 +43,23 @@ namespace Oyster { namespace Physics3D
// GET METHODS ////////////////////////////////
const ::Oyster::Math::Float4x4 & GetMomentOfInertia() const;
::Oyster::Math::Float GetMass() const;
const ::Oyster::Math::Quaternion & GetRotation() const;
::Oyster::Math::Float4x4 GetRotationMatrix() const;
::Oyster::Math::Float4x4 GetOrientation() const;
::Oyster::Math::Float4x4 GetView() const;
::Oyster::Math::Float4x4 GetToWorldMatrix() const;
::Oyster::Math::Float4x4 GetToLocalMatrix() const;
::Oyster::Math::Float4 GetSize() const;
::Oyster::Math::Float4 GetVelocity_Linear() const;
::Oyster::Math::Float4 GetVelocity_Angular() const;
::Oyster::Math::Float4 GetLinearMomentum( const ::Oyster::Math::Float4 &atWorldPos ) const;
const ::Oyster::Physics3D::MomentOfInertia & GetMomentOfInertia() const;
::Oyster::Math::Float GetMass() const;
const ::Oyster::Math::Quaternion & GetRotation() const;
::Oyster::Math::Float4x4 GetRotationMatrix() const;
::Oyster::Math::Float4x4 GetOrientation() const;
::Oyster::Math::Float4x4 GetView() const;
::Oyster::Math::Float4x4 GetToWorldMatrix() const;
::Oyster::Math::Float4x4 GetToLocalMatrix() const;
::Oyster::Math::Float4 GetSize() const;
::Oyster::Math::Float4 GetVelocity_Linear() const;
::Oyster::Math::Float4 GetVelocity_Angular() const;
::Oyster::Math::Float4 GetLinearMomentum( const ::Oyster::Math::Float4 &atWorldPos ) const;
// SET METHODS ////////////////////////////////
void SetMomentOfInertia_KeepVelocity( const ::Oyster::Math::Float4x4 &localTensorI );
void SetMomentOfInertia_KeepMomentum( const ::Oyster::Math::Float4x4 &localTensorI );
void SetMomentOfInertia_KeepVelocity( const ::Oyster::Physics3D::MomentOfInertia &localTensorI );
void SetMomentOfInertia_KeepMomentum( const ::Oyster::Physics3D::MomentOfInertia &localTensorI );
void SetMass_KeepVelocity( const ::Oyster::Math::Float &m );
void SetMass_KeepMomentum( const ::Oyster::Math::Float &m );
@ -78,7 +79,8 @@ namespace Oyster { namespace Physics3D
private:
::Oyster::Math::Float mass; //!< m (kg)
::Oyster::Math::Float4x4 momentOfInertiaTensor; //!< I (Nm*s) Tensor matrix ( only need to be 3x3 matrix, but is 4x4 for future hardware acceleration ) (localValue)
//::Oyster::Math::Float4x4 momentOfInertiaTensor; //!< I (Nm*s) Tensor matrix ( only need to be 3x3 matrix, but is 4x4 for future hardware acceleration ) (localValue)
::Oyster::Physics3D::MomentOfInertia momentOfInertiaTensor;
::Oyster::Math::Quaternion rotation; //!< RotationAxis of the body.
};
} }