2013-11-21 17:22:13 +01:00
|
|
|
#include "SimpleRigidBody.h"
|
2013-11-25 16:35:56 +01:00
|
|
|
#include "PhysicsAPI_Impl.h"
|
2013-11-21 17:22:13 +01:00
|
|
|
|
|
|
|
using namespace ::Oyster::Physics;
|
2013-11-28 11:58:46 +01:00
|
|
|
using namespace ::Oyster::Physics3D;
|
2013-11-25 16:35:56 +01:00
|
|
|
using namespace ::Oyster::Math3D;
|
2013-11-21 17:22:13 +01:00
|
|
|
using namespace ::Oyster::Collision3D;
|
|
|
|
using namespace ::Utility::DynamicMemory;
|
2013-11-25 16:35:56 +01:00
|
|
|
using namespace ::Utility::Value;
|
2013-11-21 17:22:13 +01:00
|
|
|
|
2013-11-26 13:27:34 +01:00
|
|
|
SimpleRigidBody::SimpleRigidBody()
|
2013-11-28 11:58:46 +01:00
|
|
|
{
|
2013-11-28 12:13:14 +01:00
|
|
|
this->rigid = RigidBody( Box(Float4x4::identity, Float3::null, Float3(1.0f)), 16.0f, Float4x4::identity );
|
2013-11-28 11:58:46 +01:00
|
|
|
this->gravityNormal = Float3::null;
|
|
|
|
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;
|
|
|
|
}
|
2013-11-21 17:22:13 +01:00
|
|
|
|
2013-11-25 16:35:56 +01:00
|
|
|
SimpleRigidBody::~SimpleRigidBody() {}
|
2013-11-21 17:22:13 +01:00
|
|
|
|
|
|
|
UniquePointer<ICustomBody> SimpleRigidBody::Clone() const
|
|
|
|
{
|
|
|
|
return new SimpleRigidBody( *this );
|
|
|
|
}
|
|
|
|
|
2013-12-06 09:46:30 +01:00
|
|
|
SimpleRigidBody::State SimpleRigidBody::GetState() const
|
|
|
|
{
|
|
|
|
return State( this->rigid.box.boundingOffset, this->rigid.box.center, AngularAxis(this->rigid.box.rotation).xyz );
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleRigidBody::State & SimpleRigidBody::GetState( SimpleRigidBody::State &targetMem ) const
|
|
|
|
{
|
|
|
|
return targetMem = State( this->rigid.box.boundingOffset, this->rigid.box.center, AngularAxis(this->rigid.box.rotation).xyz );
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleRigidBody::SetState( const SimpleRigidBody::State &state )
|
|
|
|
{ /** @todo TODO: temporary solution! Need to know it's occtree */
|
|
|
|
this->rigid.box.boundingOffset = state.GetReach();
|
|
|
|
this->rigid.box.center = state.GetCenterPosition();
|
|
|
|
this->rigid.box.rotation = state.GetRotation();
|
|
|
|
}
|
|
|
|
|
2013-11-29 09:21:44 +01:00
|
|
|
void SimpleRigidBody::CallSubscription( const ICustomBody *proto, const ICustomBody *deuter )
|
|
|
|
{
|
|
|
|
this->collisionAction( proto, deuter );
|
|
|
|
}
|
|
|
|
|
2013-11-26 13:27:34 +01:00
|
|
|
bool SimpleRigidBody::IsAffectedByGravity() const
|
|
|
|
{
|
|
|
|
return !this->ignoreGravity;
|
2013-11-21 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
2013-11-25 16:35:56 +01:00
|
|
|
bool SimpleRigidBody::Intersects( const ICustomBody &object, Float timeStepLength, Float &deltaWhen, Float3 &worldPointOfContact ) const
|
2013-11-21 17:22:13 +01:00
|
|
|
{
|
2013-11-28 11:58:46 +01:00
|
|
|
if( object.Intersects(this->rigid.box) )
|
2013-11-25 16:35:56 +01:00
|
|
|
{ //! @todo TODO: better implementation needed
|
|
|
|
deltaWhen = timeStepLength;
|
2013-11-28 11:58:46 +01:00
|
|
|
worldPointOfContact = Average( this->rigid.box.center, object.GetCenter() );
|
2013-11-25 16:35:56 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-21 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleRigidBody::Intersects( const ICollideable &shape ) const
|
|
|
|
{
|
2013-11-28 11:58:46 +01:00
|
|
|
return this->rigid.box.Intersects( shape );
|
2013-11-21 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Sphere & SimpleRigidBody::GetBoundingSphere( Sphere &targetMem ) const
|
|
|
|
{
|
2013-11-28 11:58:46 +01:00
|
|
|
return targetMem = Sphere( this->rigid.box.center, this->rigid.box.boundingOffset.GetMagnitude() );
|
2013-11-21 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Float3 & SimpleRigidBody::GetNormalAt( const Float3 &worldPos, Float3 &targetMem ) const
|
|
|
|
{
|
2013-11-25 16:35:56 +01:00
|
|
|
//! @todo TODO: better implementation needed
|
2013-11-28 11:58:46 +01:00
|
|
|
return targetMem = (worldPos - this->rigid.box.center).GetNormalized();
|
2013-11-21 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
2013-11-26 13:27:34 +01:00
|
|
|
Float3 & SimpleRigidBody::GetGravityNormal( Float3 &targetMem ) const
|
|
|
|
{
|
|
|
|
return targetMem = this->gravityNormal;
|
|
|
|
}
|
|
|
|
|
2013-11-21 17:22:13 +01:00
|
|
|
Float3 & SimpleRigidBody::GetCenter( Float3 &targetMem ) const
|
|
|
|
{
|
2013-11-28 11:58:46 +01:00
|
|
|
return targetMem = this->rigid.box.center;
|
2013-11-21 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Float4x4 & SimpleRigidBody::GetRotation( Float4x4 &targetMem ) const
|
|
|
|
{
|
2013-11-28 11:58:46 +01:00
|
|
|
return targetMem = this->rigid.box.rotation;
|
2013-11-21 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Float4x4 & SimpleRigidBody::GetOrientation( Float4x4 &targetMem ) const
|
|
|
|
{
|
2013-11-28 11:58:46 +01:00
|
|
|
return targetMem = this->rigid.GetOrientation();
|
2013-11-21 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Float4x4 & SimpleRigidBody::GetView( Float4x4 &targetMem ) const
|
|
|
|
{
|
2013-11-28 11:58:46 +01:00
|
|
|
return targetMem = this->rigid.GetView();
|
2013-11-21 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
2013-12-12 10:02:35 +01:00
|
|
|
Float3 SimpleRigidBody::GetRigidLinearVelocity() const
|
|
|
|
{
|
|
|
|
return this->rigid.GetLinearVelocity();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-21 17:22:13 +01:00
|
|
|
UpdateState SimpleRigidBody::Update( Float timeStepLength )
|
|
|
|
{
|
2013-11-28 11:58:46 +01:00
|
|
|
this->rigid.Update_LeapFrog( timeStepLength );
|
2013-11-25 16:35:56 +01:00
|
|
|
|
|
|
|
// compare previous and new state and return result
|
2013-11-28 11:58:46 +01:00
|
|
|
//return this->current == this->previous ? UpdateState_resting : UpdateState_altered;
|
|
|
|
return UpdateState_altered;
|
2013-11-28 10:26:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleRigidBody::SetSubscription( ICustomBody::EventAction_Collision functionPointer )
|
|
|
|
{
|
|
|
|
if( functionPointer )
|
|
|
|
{
|
|
|
|
this->collisionAction = functionPointer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->collisionAction = Default::EventAction_Collision;
|
|
|
|
}
|
2013-11-21 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
2013-11-26 13:27:34 +01:00
|
|
|
void SimpleRigidBody::SetGravity( bool ignore)
|
|
|
|
{
|
|
|
|
this->ignoreGravity = ignore;
|
|
|
|
this->gravityNormal = Float3::null;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleRigidBody::SetGravityNormal( const Float3 &normalizedVector )
|
|
|
|
{
|
|
|
|
this->gravityNormal = normalizedVector;
|
|
|
|
}
|
|
|
|
|
2013-11-21 17:22:13 +01:00
|
|
|
void SimpleRigidBody::SetMomentOfInertiaTensor_KeepVelocity( const Float4x4 &localI )
|
|
|
|
{
|
2013-11-28 11:58:46 +01:00
|
|
|
this->rigid.SetMomentOfInertia_KeepVelocity( localI );
|
2013-11-21 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleRigidBody::SetMomentOfInertiaTensor_KeepMomentum( const Float4x4 &localI )
|
|
|
|
{
|
2013-11-28 11:58:46 +01:00
|
|
|
this->rigid.SetMomentOfInertia_KeepMomentum( localI );
|
2013-11-21 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleRigidBody::SetMass_KeepVelocity( Float m )
|
|
|
|
{
|
2013-11-28 11:58:46 +01:00
|
|
|
this->rigid.SetMass_KeepVelocity( m );
|
2013-11-21 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleRigidBody::SetMass_KeepMomentum( Float m )
|
|
|
|
{
|
2013-11-28 11:58:46 +01:00
|
|
|
this->rigid.SetMass_KeepMomentum( m );
|
2013-11-21 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleRigidBody::SetCenter( const Float3 &worldPos )
|
|
|
|
{
|
2013-11-28 11:58:46 +01:00
|
|
|
this->rigid.SetCenter( worldPos );
|
2013-11-21 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleRigidBody::SetRotation( const Float4x4 &rotation )
|
|
|
|
{
|
2013-11-28 11:58:46 +01:00
|
|
|
this->rigid.SetRotation( rotation );
|
2013-11-21 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleRigidBody::SetOrientation( const Float4x4 &orientation )
|
|
|
|
{
|
2013-11-28 11:58:46 +01:00
|
|
|
this->rigid.SetOrientation( orientation );
|
2013-11-26 13:27:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleRigidBody::SetSize( const Float3 &size )
|
|
|
|
{
|
2013-11-28 11:58:46 +01:00
|
|
|
this->rigid.SetSize( size );
|
2013-12-03 15:11:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleRigidBody::SetMomentum( const Float3 &worldG )
|
|
|
|
{
|
|
|
|
this->rigid.SetLinearMomentum( worldG );
|
2013-11-21 17:22:13 +01:00
|
|
|
}
|