SimpleRigidBody factory done

not tested though
This commit is contained in:
Dander7BD 2013-11-28 11:58:46 +01:00 committed by Erik Persson
parent 3a3d6fb151
commit 89fd71fb02
8 changed files with 136 additions and 36 deletions

View File

@ -1,6 +1,7 @@
#include "PhysicsAPI_Impl.h" #include "PhysicsAPI_Impl.h"
#include "SimpleRigidBody.h"
#include "OysterPhysics3D.h" #include "OysterPhysics3D.h"
#include "SimpleRigidBody.h"
#include "SphericalRigidBody.h"
using namespace ::Oyster::Physics; using namespace ::Oyster::Physics;
using namespace ::Oyster::Physics3D; using namespace ::Oyster::Physics3D;
@ -161,9 +162,14 @@ void API_Impl::SetSize( const ICustomBody* objRef, const Float3 &size )
//! @todo TODO: implement stub //! @todo TODO: implement stub
} }
UniquePointer<ICustomBody> API_Impl::CreateSimpleRigidBody() const UniquePointer<ICustomBody> API_Impl::CreateRigidBody( const API::SimpleBodyDescription &desc ) const
{ {
return new SimpleRigidBody(); return new SimpleRigidBody( desc );
}
UniquePointer<ICustomBody> API_Impl::CreateRigidBody( const API::SphericalBodyDescription &desc ) const
{
return new SphericalRigidBody();
} }
namespace Oyster { namespace Physics { namespace Default namespace Oyster { namespace Physics { namespace Default

View File

@ -41,7 +41,9 @@ namespace Oyster
void SetOrientation( const ICustomBody* objRef, const ::Oyster::Math::Float4x4 &orientation ); void SetOrientation( const ICustomBody* objRef, const ::Oyster::Math::Float4x4 &orientation );
void SetSize( const ICustomBody* objRef, const ::Oyster::Math::Float3 &size ); void SetSize( const ICustomBody* objRef, const ::Oyster::Math::Float3 &size );
::Utility::DynamicMemory::UniquePointer<ICustomBody> CreateSimpleRigidBody() const; ::Utility::DynamicMemory::UniquePointer<ICustomBody> CreateRigidBody( const SimpleBodyDescription &desc ) const;
::Utility::DynamicMemory::UniquePointer<ICustomBody> CreateRigidBody( const SphericalBodyDescription &desc ) const;
private: private:
::Oyster::Math::Float gravityConstant, updateFrameLength; ::Oyster::Math::Float gravityConstant, updateFrameLength;
EventAction_Destruction destructionAction; EventAction_Destruction destructionAction;

View File

@ -2,16 +2,38 @@
#include "PhysicsAPI_Impl.h" #include "PhysicsAPI_Impl.h"
using namespace ::Oyster::Physics; using namespace ::Oyster::Physics;
using namespace ::Oyster::Physics3D;
using namespace ::Oyster::Math3D; using namespace ::Oyster::Math3D;
using namespace ::Oyster::Collision3D; using namespace ::Oyster::Collision3D;
using namespace ::Utility::DynamicMemory; using namespace ::Utility::DynamicMemory;
using namespace ::Utility::Value; using namespace ::Utility::Value;
SimpleRigidBody::SimpleRigidBody() SimpleRigidBody::SimpleRigidBody()
: previous(), current(), {
gravityNormal(0.0f), this->rigid = RigidBody();
collisionAction(Default::EventAction_Collision), this->gravityNormal = Float3::null;
ignoreGravity(false) {} this->collisionAction = Default::EventAction_Collision;
this->ignoreGravity = false;
}
SimpleRigidBody::SimpleRigidBody( const API::SimpleBodyDescription &desc )
{
this->rigid = RigidBody( Box( desc.rotation, desc.centerPosition, desc.size ),
desc.mass,
desc.inertiaTensor );
this->gravityNormal = Float3::null;
if( desc.subscription )
{
this->collisionAction = desc.subscription;
}
else
{
this->collisionAction = Default::EventAction_Collision;
}
this->ignoreGravity = desc.ignoreGravity;
}
SimpleRigidBody::~SimpleRigidBody() {} SimpleRigidBody::~SimpleRigidBody() {}
@ -27,10 +49,10 @@ bool SimpleRigidBody::IsAffectedByGravity() const
bool SimpleRigidBody::Intersects( const ICustomBody &object, Float timeStepLength, Float &deltaWhen, Float3 &worldPointOfContact ) const bool SimpleRigidBody::Intersects( const ICustomBody &object, Float timeStepLength, Float &deltaWhen, Float3 &worldPointOfContact ) const
{ {
if( object.Intersects(this->current.box) ) if( object.Intersects(this->rigid.box) )
{ //! @todo TODO: better implementation needed { //! @todo TODO: better implementation needed
deltaWhen = timeStepLength; deltaWhen = timeStepLength;
worldPointOfContact = Average( this->current.box.center, object.GetCenter() ); worldPointOfContact = Average( this->rigid.box.center, object.GetCenter() );
return true; return true;
} }
else else
@ -41,18 +63,18 @@ bool SimpleRigidBody::Intersects( const ICustomBody &object, Float timeStepLengt
bool SimpleRigidBody::Intersects( const ICollideable &shape ) const bool SimpleRigidBody::Intersects( const ICollideable &shape ) const
{ {
return this->current.box.Intersects( shape ); return this->rigid.box.Intersects( shape );
} }
Sphere & SimpleRigidBody::GetBoundingSphere( Sphere &targetMem ) const Sphere & SimpleRigidBody::GetBoundingSphere( Sphere &targetMem ) const
{ {
return targetMem = Sphere( this->current.box.center, this->current.box.boundingOffset.GetMagnitude() ); return targetMem = Sphere( this->rigid.box.center, this->rigid.box.boundingOffset.GetMagnitude() );
} }
Float3 & SimpleRigidBody::GetNormalAt( const Float3 &worldPos, Float3 &targetMem ) const Float3 & SimpleRigidBody::GetNormalAt( const Float3 &worldPos, Float3 &targetMem ) const
{ {
//! @todo TODO: better implementation needed //! @todo TODO: better implementation needed
return targetMem = (worldPos - this->current.box.center).GetNormalized(); return targetMem = (worldPos - this->rigid.box.center).GetNormalized();
} }
Float3 & SimpleRigidBody::GetGravityNormal( Float3 &targetMem ) const Float3 & SimpleRigidBody::GetGravityNormal( Float3 &targetMem ) const
@ -62,32 +84,31 @@ Float3 & SimpleRigidBody::GetGravityNormal( Float3 &targetMem ) const
Float3 & SimpleRigidBody::GetCenter( Float3 &targetMem ) const Float3 & SimpleRigidBody::GetCenter( Float3 &targetMem ) const
{ {
return targetMem = this->current.box.center; return targetMem = this->rigid.box.center;
} }
Float4x4 & SimpleRigidBody::GetRotation( Float4x4 &targetMem ) const Float4x4 & SimpleRigidBody::GetRotation( Float4x4 &targetMem ) const
{ {
return targetMem = this->current.box.rotation; return targetMem = this->rigid.box.rotation;
} }
Float4x4 & SimpleRigidBody::GetOrientation( Float4x4 &targetMem ) const Float4x4 & SimpleRigidBody::GetOrientation( Float4x4 &targetMem ) const
{ {
return targetMem = this->current.GetOrientation(); return targetMem = this->rigid.GetOrientation();
} }
Float4x4 & SimpleRigidBody::GetView( Float4x4 &targetMem ) const Float4x4 & SimpleRigidBody::GetView( Float4x4 &targetMem ) const
{ {
return targetMem = this->current.GetView(); return targetMem = this->rigid.GetView();
} }
UpdateState SimpleRigidBody::Update( Float timeStepLength ) UpdateState SimpleRigidBody::Update( Float timeStepLength )
{ {
this->previous = this->current; // memorizing the old state this->rigid.Update_LeapFrog( timeStepLength );
this->current.Update_LeapFrog( timeStepLength );
// compare previous and new state and return result // compare previous and new state and return result
return this->current == this->previous ? UpdateState_resting : UpdateState_altered; //return this->current == this->previous ? UpdateState_resting : UpdateState_altered;
return UpdateState_altered;
} }
void SimpleRigidBody::SetSubscription( ICustomBody::EventAction_Collision functionPointer ) void SimpleRigidBody::SetSubscription( ICustomBody::EventAction_Collision functionPointer )
@ -115,40 +136,40 @@ void SimpleRigidBody::SetGravityNormal( const Float3 &normalizedVector )
void SimpleRigidBody::SetMomentOfInertiaTensor_KeepVelocity( const Float4x4 &localI ) void SimpleRigidBody::SetMomentOfInertiaTensor_KeepVelocity( const Float4x4 &localI )
{ {
this->current.SetMomentOfInertia_KeepVelocity( localI ); this->rigid.SetMomentOfInertia_KeepVelocity( localI );
} }
void SimpleRigidBody::SetMomentOfInertiaTensor_KeepMomentum( const Float4x4 &localI ) void SimpleRigidBody::SetMomentOfInertiaTensor_KeepMomentum( const Float4x4 &localI )
{ {
this->current.SetMomentOfInertia_KeepMomentum( localI ); this->rigid.SetMomentOfInertia_KeepMomentum( localI );
} }
void SimpleRigidBody::SetMass_KeepVelocity( Float m ) void SimpleRigidBody::SetMass_KeepVelocity( Float m )
{ {
this->current.SetMass_KeepVelocity( m ); this->rigid.SetMass_KeepVelocity( m );
} }
void SimpleRigidBody::SetMass_KeepMomentum( Float m ) void SimpleRigidBody::SetMass_KeepMomentum( Float m )
{ {
this->current.SetMass_KeepMomentum( m ); this->rigid.SetMass_KeepMomentum( m );
} }
void SimpleRigidBody::SetCenter( const Float3 &worldPos ) void SimpleRigidBody::SetCenter( const Float3 &worldPos )
{ {
this->current.SetCenter( worldPos ); this->rigid.SetCenter( worldPos );
} }
void SimpleRigidBody::SetRotation( const Float4x4 &rotation ) void SimpleRigidBody::SetRotation( const Float4x4 &rotation )
{ {
this->current.SetRotation( rotation ); this->rigid.SetRotation( rotation );
} }
void SimpleRigidBody::SetOrientation( const Float4x4 &orientation ) void SimpleRigidBody::SetOrientation( const Float4x4 &orientation )
{ {
this->current.SetOrientation( orientation ); this->rigid.SetOrientation( orientation );
} }
void SimpleRigidBody::SetSize( const Float3 &size ) void SimpleRigidBody::SetSize( const Float3 &size )
{ {
this->current.SetSize( size ); this->rigid.SetSize( size );
} }

View File

@ -10,6 +10,7 @@ namespace Oyster { namespace Physics
{ {
public: public:
SimpleRigidBody(); SimpleRigidBody();
SimpleRigidBody( const API::SimpleBodyDescription &desc );
virtual ~SimpleRigidBody(); virtual ~SimpleRigidBody();
::Utility::DynamicMemory::UniquePointer<ICustomBody> Clone() const; ::Utility::DynamicMemory::UniquePointer<ICustomBody> Clone() const;
@ -41,7 +42,7 @@ namespace Oyster { namespace Physics
void SetSize( const ::Oyster::Math::Float3 &size ); void SetSize( const ::Oyster::Math::Float3 &size );
private: private:
::Oyster::Physics3D::RigidBody previous, current; ::Oyster::Physics3D::RigidBody rigid;
::Oyster::Math::Float3 gravityNormal; ::Oyster::Math::Float3 gravityNormal;
EventAction_Collision collisionAction; EventAction_Collision collisionAction;
bool ignoreGravity; bool ignoreGravity;

View File

@ -2,6 +2,7 @@
#include "PhysicsAPI_Impl.h" #include "PhysicsAPI_Impl.h"
using namespace ::Oyster::Physics; using namespace ::Oyster::Physics;
using namespace ::Oyster::Physics3D;
using namespace ::Oyster::Math3D; using namespace ::Oyster::Math3D;
using namespace ::Oyster::Collision3D; using namespace ::Oyster::Collision3D;
using namespace ::Utility::DynamicMemory; using namespace ::Utility::DynamicMemory;

View File

@ -45,6 +45,9 @@ namespace Oyster
class PHYSICS_DLL_USAGE API class PHYSICS_DLL_USAGE API
{ {
public: public:
struct SimpleBodyDescription;
struct SphericalBodyDescription;
typedef void (*EventAction_Destruction)( ::Utility::DynamicMemory::UniquePointer<ICustomBody> proto ); typedef void (*EventAction_Destruction)( ::Utility::DynamicMemory::UniquePointer<ICustomBody> proto );
/** Gets the Physics instance. */ /** Gets the Physics instance. */
@ -207,9 +210,17 @@ namespace Oyster
/******************************************************** /********************************************************
* Creates a new dynamically allocated object that can be used as a component for more complex ICustomBodies. * Creates a new dynamically allocated object that can be used as a component for more complex ICustomBodies.
* @param desc: @see API::SimpleBodyDescription
* @return A pointer along with the responsibility to delete. * @return A pointer along with the responsibility to delete.
********************************************************/ ********************************************************/
virtual ::Utility::DynamicMemory::UniquePointer<ICustomBody> CreateSimpleRigidBody() const = 0; virtual ::Utility::DynamicMemory::UniquePointer<ICustomBody> CreateRigidBody( const SimpleBodyDescription &desc ) const = 0;
/********************************************************
* Creates a new dynamically allocated object that can be used as a component for more complex ICustomBodies.
* @param desc: @see API::SphericalBodyDescription
* @return A pointer along with the responsibility to delete.
********************************************************/
virtual ::Utility::DynamicMemory::UniquePointer<ICustomBody> CreateRigidBody( const SphericalBodyDescription &desc ) const = 0;
protected: protected:
virtual ~API() {} virtual ~API() {}
@ -375,6 +386,50 @@ namespace Oyster
********************************************************/ ********************************************************/
virtual void SetSize( const ::Oyster::Math::Float3 &size ) = 0; virtual void SetSize( const ::Oyster::Math::Float3 &size ) = 0;
}; };
struct API::SimpleBodyDescription
{
::Oyster::Math::Float4x4 rotation;
::Oyster::Math::Float3 centerPosition;
::Oyster::Math::Float3 size;
::Oyster::Math::Float mass;
::Oyster::Math::Float4x4 inertiaTensor;
ICustomBody::EventAction_Collision subscription;
bool ignoreGravity;
SimpleBodyDescription()
{
this->rotation = ::Oyster::Math::Float4x4::identity;
this->centerPosition = ::Oyster::Math::Float3::null;
this->size = ::Oyster::Math::Float3( 1.0f );
this->mass = 12.0f;
this->inertiaTensor = ::Oyster::Math::Float4x4::identity;
this->subscription = NULL;
this->ignoreGravity = false;
}
};
struct API::SphericalBodyDescription
{
::Oyster::Math::Float4x4 rotation;
::Oyster::Math::Float3 centerPosition;
::Oyster::Math::Float radius;
::Oyster::Math::Float mass;
::Oyster::Math::Float4x4 inertiaTensor;
ICustomBody::EventAction_Collision subscription;
bool ignoreGravity;
SphericalBodyDescription()
{
this->rotation = ::Oyster::Math::Float4x4::identity;
this->centerPosition = ::Oyster::Math::Float3::null;
this->radius = 0.5f;
this->mass = 10.0f;
this->inertiaTensor = ::Oyster::Math::Float4x4::identity;
this->subscription = NULL;
this->ignoreGravity = false;
}
};
} }
} }
#endif #endif

View File

@ -9,9 +9,14 @@ using namespace ::Oyster::Collision3D;
using namespace ::Oyster::Physics3D; using namespace ::Oyster::Physics3D;
using namespace ::Oyster::Math3D; using namespace ::Oyster::Math3D;
RigidBody::RigidBody( const Box &b, Float m ) RigidBody::RigidBody( const Box &b, Float m, const Float4x4 &inertiaTensor )
: box(b), angularMomentum(0.0f), linearMomentum(0.0f), impulseTorqueSum(0.0f), impulseForceSum(0.0f)
{ // by Dan Andersson { // by Dan Andersson
this->box = b;
this->angularMomentum = Float3::null;
this->linearMomentum = Float3::null;
this->impulseTorqueSum = Float3::null;
this->impulseForceSum = Float3::null;
if( m != 0.0f ) if( m != 0.0f )
{ {
this->mass = m; this->mass = m;
@ -21,7 +26,14 @@ RigidBody::RigidBody( const Box &b, Float m )
this->mass = ::Utility::Value::numeric_limits<Float>::epsilon(); this->mass = ::Utility::Value::numeric_limits<Float>::epsilon();
} }
if( inertiaTensor.GetDeterminant() != 0.0f )
{
this->momentOfInertiaTensor = inertiaTensor;
}
else
{
this->momentOfInertiaTensor = Float4x4::identity; this->momentOfInertiaTensor = Float4x4::identity;
}
} }
RigidBody & RigidBody::operator = ( const RigidBody &body ) RigidBody & RigidBody::operator = ( const RigidBody &body )

View File

@ -20,7 +20,9 @@ namespace Oyster { namespace Physics3D
impulseTorqueSum, /** The impulse torque T (Nm) that will be consumed each update. (worldValue) */ impulseTorqueSum, /** The impulse torque T (Nm) that will be consumed each update. (worldValue) */
impulseForceSum; /** The impulse force F (N) that will be consumed each update. (worldValue) */ impulseForceSum; /** The impulse force F (N) that will be consumed each update. (worldValue) */
RigidBody( const ::Oyster::Collision3D::Box &box = ::Oyster::Collision3D::Box(), ::Oyster::Math::Float mass = 1.0f ); RigidBody( const ::Oyster::Collision3D::Box &box = ::Oyster::Collision3D::Box(),
::Oyster::Math::Float mass = 12.0f,
const ::Oyster::Math::Float4x4 &inertiaTensor = ::Oyster::Math::Float4x4::identity );
RigidBody & operator = ( const RigidBody &body ); RigidBody & operator = ( const RigidBody &body );