2013-11-26 13:28:33 +01:00
|
|
|
#include "SphericalRigidBody.h"
|
|
|
|
#include "PhysicsAPI_Impl.h"
|
|
|
|
|
|
|
|
using namespace ::Oyster::Physics;
|
2013-11-28 11:58:46 +01:00
|
|
|
using namespace ::Oyster::Physics3D;
|
2013-11-26 13:28:33 +01:00
|
|
|
using namespace ::Oyster::Math3D;
|
|
|
|
using namespace ::Oyster::Collision3D;
|
|
|
|
using namespace ::Utility::DynamicMemory;
|
|
|
|
using namespace ::Utility::Value;
|
|
|
|
|
|
|
|
SphericalRigidBody::SphericalRigidBody()
|
2013-11-28 12:13:14 +01:00
|
|
|
{
|
2013-12-19 21:39:20 +01:00
|
|
|
this->rigid = RigidBody();
|
|
|
|
this->rigid.SetMass_KeepMomentum( 10.0f );
|
2013-11-28 12:13:14 +01:00
|
|
|
this->gravityNormal = Float3::null;
|
2014-01-29 11:22:04 +01:00
|
|
|
this->onCollision = Default::EventAction_BeforeCollisionResponse;
|
|
|
|
this->onCollisionResponse = Default::EventAction_AfterCollisionResponse;
|
2014-01-17 16:07:25 +01:00
|
|
|
this->onMovement = Default::EventAction_Move;
|
2013-12-19 10:53:55 +01:00
|
|
|
this->scene = nullptr;
|
2014-01-20 13:44:12 +01:00
|
|
|
this->customTag = nullptr;
|
|
|
|
this->ignoreGravity = this->isForwarded = false;
|
2013-11-28 12:13:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SphericalRigidBody::SphericalRigidBody( const API::SphericalBodyDescription &desc )
|
|
|
|
{
|
2013-12-19 21:39:20 +01:00
|
|
|
this->rigid = RigidBody();
|
2014-01-28 09:52:58 +01:00
|
|
|
//this->rigid.SetRotation( desc.rotation );
|
2013-12-19 21:39:20 +01:00
|
|
|
this->rigid.centerPos = desc.centerPosition;
|
|
|
|
this->rigid.boundingReach = Float4( desc.radius, desc.radius, desc.radius, 0.0f );
|
|
|
|
this->rigid.SetMass_KeepMomentum( desc.mass );
|
2014-01-23 19:13:02 +01:00
|
|
|
this->rigid.SetMomentOfInertia_KeepMomentum( MomentOfInertia::Sphere(desc.mass, desc.radius) );
|
2013-12-20 11:36:39 +01:00
|
|
|
this->deltaPos = Float4::null;
|
|
|
|
this->deltaAxis = Float4::null;
|
2013-12-19 21:39:20 +01:00
|
|
|
|
2013-11-28 12:13:14 +01:00
|
|
|
this->gravityNormal = Float3::null;
|
|
|
|
|
2014-01-17 16:07:25 +01:00
|
|
|
if( desc.subscription_onCollision )
|
2013-11-28 12:13:14 +01:00
|
|
|
{
|
2014-01-17 16:07:25 +01:00
|
|
|
this->onCollision = desc.subscription_onCollision;
|
2013-11-28 12:13:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-29 11:22:04 +01:00
|
|
|
this->onCollision = Default::EventAction_BeforeCollisionResponse;
|
2014-01-17 16:07:25 +01:00
|
|
|
}
|
|
|
|
|
2014-01-22 13:50:54 +01:00
|
|
|
if( desc.subscription_onCollisionResponse )
|
|
|
|
{
|
|
|
|
this->onCollisionResponse = desc.subscription_onCollisionResponse;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-29 11:22:04 +01:00
|
|
|
this->onCollisionResponse = Default::EventAction_AfterCollisionResponse;
|
2014-01-22 13:50:54 +01:00
|
|
|
}
|
|
|
|
|
2014-01-17 16:07:25 +01:00
|
|
|
if( desc.subscription_onMovement )
|
|
|
|
{
|
|
|
|
this->onMovement= desc.subscription_onMovement;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->onMovement = Default::EventAction_Move;
|
2013-11-28 12:13:14 +01:00
|
|
|
}
|
|
|
|
|
2013-12-19 10:53:55 +01:00
|
|
|
this->scene = nullptr;
|
2014-01-20 13:44:12 +01:00
|
|
|
this->customTag = nullptr;
|
|
|
|
this->ignoreGravity = desc.ignoreGravity;
|
2013-11-28 12:13:14 +01:00
|
|
|
}
|
2013-11-26 13:28:33 +01:00
|
|
|
|
|
|
|
SphericalRigidBody::~SphericalRigidBody() {}
|
|
|
|
|
|
|
|
UniquePointer<ICustomBody> SphericalRigidBody::Clone() const
|
|
|
|
{
|
|
|
|
return new SphericalRigidBody( *this );
|
|
|
|
}
|
|
|
|
|
2013-12-06 09:46:30 +01:00
|
|
|
SphericalRigidBody::State SphericalRigidBody::GetState() const
|
|
|
|
{
|
2013-12-19 10:03:56 +01:00
|
|
|
return State( this->rigid.GetMass(), this->rigid.restitutionCoeff,
|
|
|
|
this->rigid.frictionCoeff_Static, this->rigid.frictionCoeff_Kinetic,
|
2013-12-19 21:39:20 +01:00
|
|
|
this->rigid.GetMomentOfInertia(), this->rigid.boundingReach,
|
|
|
|
this->rigid.centerPos, this->rigid.axis,
|
|
|
|
this->rigid.momentum_Linear, this->rigid.momentum_Angular );
|
2013-12-06 09:46:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SphericalRigidBody::State & SphericalRigidBody::GetState( SphericalRigidBody::State &targetMem ) const
|
|
|
|
{
|
2013-12-19 10:03:56 +01:00
|
|
|
return targetMem = State( this->rigid.GetMass(), this->rigid.restitutionCoeff,
|
|
|
|
this->rigid.frictionCoeff_Static, this->rigid.frictionCoeff_Kinetic,
|
2013-12-19 21:39:20 +01:00
|
|
|
this->rigid.GetMomentOfInertia(), this->rigid.boundingReach,
|
|
|
|
this->rigid.centerPos, this->rigid.axis,
|
|
|
|
this->rigid.momentum_Linear, this->rigid.momentum_Angular );
|
2013-12-06 09:46:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SphericalRigidBody::SetState( const SphericalRigidBody::State &state )
|
2013-12-19 10:53:55 +01:00
|
|
|
{
|
2013-12-19 21:39:20 +01:00
|
|
|
this->rigid.centerPos = state.GetCenterPosition();
|
2014-01-31 15:58:02 +01:00
|
|
|
this->rigid.axis = state.GetAngularAxis();
|
2013-12-19 21:39:20 +01:00
|
|
|
this->rigid.boundingReach = state.GetReach();
|
|
|
|
this->rigid.momentum_Linear = state.GetLinearMomentum();
|
|
|
|
this->rigid.momentum_Angular = state.GetAngularMomentum();
|
|
|
|
this->rigid.impulse_Linear += state.GetLinearImpulse();
|
|
|
|
this->rigid.impulse_Angular += state.GetAngularImpulse();
|
|
|
|
this->rigid.restitutionCoeff = state.GetRestitutionCoeff();
|
|
|
|
this->rigid.frictionCoeff_Static = state.GetFrictionCoeff_Static();
|
2013-12-19 10:53:55 +01:00
|
|
|
this->rigid.frictionCoeff_Kinetic = state.GetFrictionCoeff_Kinetic();
|
2014-01-14 11:58:53 +01:00
|
|
|
this->rigid.SetMass_KeepMomentum( state.GetMass() );
|
|
|
|
this->rigid.SetMomentOfInertia_KeepMomentum( state.GetMomentOfInertia() );
|
2014-01-31 15:15:08 +01:00
|
|
|
this->rigid.gravityNormal = state.GetGravityNormal();
|
2013-12-19 10:53:55 +01:00
|
|
|
|
2013-12-20 11:36:39 +01:00
|
|
|
if( state.IsForwarded() )
|
|
|
|
{
|
2014-01-28 09:52:58 +01:00
|
|
|
this->deltaPos += Float4(state.GetForward_DeltaPos(), 0);
|
|
|
|
this->deltaAxis += Float4(state.GetForward_DeltaAxis());
|
2013-12-20 11:36:39 +01:00
|
|
|
this->isForwarded = false;
|
|
|
|
}
|
|
|
|
|
2013-12-19 10:53:55 +01:00
|
|
|
if( this->scene )
|
|
|
|
{
|
|
|
|
if( state.IsSpatiallyAltered() )
|
|
|
|
{
|
|
|
|
unsigned int tempRef = this->scene->GetTemporaryReferenceOf( this );
|
|
|
|
this->scene->SetAsAltered( tempRef );
|
|
|
|
this->scene->EvaluatePosition( tempRef );
|
|
|
|
}
|
|
|
|
else if( state.IsDisturbed() )
|
|
|
|
{
|
|
|
|
this->scene->SetAsAltered( this->scene->GetTemporaryReferenceOf(this) );
|
|
|
|
}
|
|
|
|
}
|
2013-12-06 09:46:30 +01:00
|
|
|
}
|
|
|
|
|
2014-01-29 11:22:04 +01:00
|
|
|
ICustomBody::SubscriptMessage SphericalRigidBody::CallSubscription_BeforeCollisionResponse( const ICustomBody *deuter )
|
2014-01-17 16:07:25 +01:00
|
|
|
{
|
|
|
|
return this->onCollision( this, deuter );
|
|
|
|
}
|
|
|
|
|
2014-01-29 11:22:04 +01:00
|
|
|
void SphericalRigidBody::CallSubscription_AfterCollisionResponse( const ICustomBody *deuter, Float kineticEnergyLoss )
|
2014-01-22 13:50:54 +01:00
|
|
|
{
|
|
|
|
this->onCollisionResponse( this, deuter, kineticEnergyLoss);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-17 16:07:25 +01:00
|
|
|
void SphericalRigidBody::CallSubscription_Move()
|
2013-11-29 09:21:44 +01:00
|
|
|
{
|
2014-01-17 16:07:25 +01:00
|
|
|
this->onMovement( this );
|
2013-11-29 09:21:44 +01:00
|
|
|
}
|
|
|
|
|
2013-11-26 13:28:33 +01:00
|
|
|
bool SphericalRigidBody::IsAffectedByGravity() const
|
|
|
|
{
|
|
|
|
return !this->ignoreGravity;
|
|
|
|
}
|
|
|
|
|
2013-12-18 08:57:27 +01:00
|
|
|
bool SphericalRigidBody::Intersects( const ICollideable &shape ) const
|
2013-11-26 13:28:33 +01:00
|
|
|
{
|
2013-12-19 21:39:20 +01:00
|
|
|
return Sphere( this->rigid.centerPos, this->rigid.boundingReach.x ).Intersects( shape );
|
2013-11-26 13:28:33 +01:00
|
|
|
}
|
|
|
|
|
2013-12-18 08:57:27 +01:00
|
|
|
bool SphericalRigidBody::Intersects( const ICollideable &shape, Float4 &worldPointOfContact ) const
|
|
|
|
{
|
2013-12-19 21:39:20 +01:00
|
|
|
return Sphere( this->rigid.centerPos, this->rigid.boundingReach.x ).Intersects( shape, worldPointOfContact );
|
2013-12-18 08:57:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SphericalRigidBody::Intersects( const ICustomBody &object, Float4 &worldPointOfContact ) const
|
2013-11-26 13:28:33 +01:00
|
|
|
{
|
2013-12-19 21:39:20 +01:00
|
|
|
return object.Intersects( Sphere(this->rigid.centerPos, this->rigid.boundingReach.x), worldPointOfContact );
|
2013-11-26 13:28:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Sphere & SphericalRigidBody::GetBoundingSphere( Sphere &targetMem ) const
|
|
|
|
{
|
2014-01-20 13:44:12 +01:00
|
|
|
return targetMem = Sphere( this->rigid.centerPos, this->rigid.boundingReach.x );
|
2013-11-26 13:28:33 +01:00
|
|
|
}
|
|
|
|
|
2013-12-18 08:57:27 +01:00
|
|
|
Float4 & SphericalRigidBody::GetNormalAt( const Float4 &worldPos, Float4 &targetMem ) const
|
2013-11-26 13:28:33 +01:00
|
|
|
{
|
2014-01-29 10:54:59 +01:00
|
|
|
targetMem = Float4( worldPos.xyz - this->rigid.centerPos, 0);
|
2013-12-19 11:15:49 +01:00
|
|
|
Float magnitude = targetMem.GetMagnitude();
|
|
|
|
if( magnitude != 0.0f )
|
|
|
|
{ // sanity check
|
|
|
|
targetMem.Normalize();
|
|
|
|
}
|
2013-12-19 11:21:14 +01:00
|
|
|
|
2013-12-19 11:15:49 +01:00
|
|
|
return targetMem;
|
2013-11-26 13:28:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Float3 & SphericalRigidBody::GetGravityNormal( Float3 &targetMem ) const
|
|
|
|
{
|
|
|
|
return targetMem = this->gravityNormal;
|
|
|
|
}
|
|
|
|
|
2014-01-20 13:44:12 +01:00
|
|
|
void * SphericalRigidBody::GetCustomTag() const
|
|
|
|
{
|
|
|
|
return this->customTag;
|
|
|
|
}
|
|
|
|
|
2013-12-18 14:16:13 +01:00
|
|
|
//Float3 & SphericalRigidBody::GetCenter( Float3 &targetMem ) const
|
|
|
|
//{
|
2013-12-19 21:39:20 +01:00
|
|
|
// return targetMem = this->rigid.centerPos;
|
2013-12-18 14:16:13 +01:00
|
|
|
//}
|
|
|
|
//
|
|
|
|
//Float4x4 & SphericalRigidBody::GetRotation( Float4x4 &targetMem ) const
|
|
|
|
//{
|
|
|
|
// return targetMem = this->rigid.box.rotation;
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//Float4x4 & SphericalRigidBody::GetOrientation( Float4x4 &targetMem ) const
|
|
|
|
//{
|
|
|
|
// return targetMem = this->rigid.GetOrientation();
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//Float4x4 & SphericalRigidBody::GetView( Float4x4 &targetMem ) const
|
|
|
|
//{
|
|
|
|
// return targetMem = this->rigid.GetView();
|
|
|
|
//}
|
2013-11-26 13:28:33 +01:00
|
|
|
|
2013-12-20 12:13:12 +01:00
|
|
|
//Float3 SphericalRigidBody::GetRigidLinearVelocity() const
|
|
|
|
//{
|
|
|
|
// return this->rigid.GetLinearVelocity();
|
|
|
|
//}
|
2013-12-12 10:02:35 +01:00
|
|
|
|
2013-11-26 13:28:33 +01:00
|
|
|
UpdateState SphericalRigidBody::Update( Float timeStepLength )
|
|
|
|
{
|
2013-12-20 11:36:39 +01:00
|
|
|
if( this->isForwarded )
|
|
|
|
{
|
2014-01-28 09:52:58 +01:00
|
|
|
this->rigid.Move( this->deltaPos.xyz, this->deltaAxis.xyz );
|
2013-12-20 11:36:39 +01:00
|
|
|
this->deltaPos = Float4::null;
|
|
|
|
this->deltaAxis = Float4::null;
|
|
|
|
this->isForwarded = false;
|
|
|
|
}
|
|
|
|
|
2013-11-28 12:13:14 +01:00
|
|
|
this->rigid.Update_LeapFrog( timeStepLength );
|
2013-11-26 13:28:33 +01:00
|
|
|
|
|
|
|
// compare previous and new state and return result
|
2013-11-28 12:13:14 +01:00
|
|
|
//return this->current == this->previous ? UpdateState_resting : UpdateState_altered;
|
|
|
|
return UpdateState_altered;
|
2013-11-28 10:26:29 +01:00
|
|
|
}
|
|
|
|
|
2013-12-20 11:36:39 +01:00
|
|
|
void SphericalRigidBody::Predict( ::Oyster::Math::Float4 &outDeltaPos, ::Oyster::Math::Float4 &outDeltaAxis, const ::Oyster::Math::Float4 &actingLinearImpulse, const ::Oyster::Math::Float4 &actingAngularImpulse, ::Oyster::Math::Float deltaTime )
|
|
|
|
{
|
2014-01-28 09:52:58 +01:00
|
|
|
this->rigid.Predict_LeapFrog( outDeltaPos.xyz, outDeltaAxis.xyz, actingLinearImpulse.xyz, actingAngularImpulse.xyz, deltaTime );
|
2013-12-20 11:36:39 +01:00
|
|
|
}
|
|
|
|
|
2014-01-29 11:22:04 +01:00
|
|
|
void SphericalRigidBody::SetSubscription( ICustomBody::EventAction_BeforeCollisionResponse functionPointer )
|
2013-11-28 10:26:29 +01:00
|
|
|
{
|
|
|
|
if( functionPointer )
|
|
|
|
{
|
2014-01-17 16:07:25 +01:00
|
|
|
this->onCollision = functionPointer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-29 11:22:04 +01:00
|
|
|
this->onCollision = Default::EventAction_BeforeCollisionResponse;
|
2014-01-17 16:07:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-29 11:22:04 +01:00
|
|
|
void SphericalRigidBody::SetSubscription( ICustomBody::EventAction_AfterCollisionResponse functionPointer )
|
2014-01-22 13:50:54 +01:00
|
|
|
{
|
|
|
|
if( functionPointer )
|
|
|
|
{
|
|
|
|
this->onCollisionResponse = functionPointer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-29 11:22:04 +01:00
|
|
|
this->onCollisionResponse = Default::EventAction_AfterCollisionResponse;
|
2014-01-22 13:50:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-17 16:07:25 +01:00
|
|
|
void SphericalRigidBody::SetSubscription( ICustomBody::EventAction_Move functionPointer )
|
|
|
|
{
|
|
|
|
if( functionPointer )
|
|
|
|
{
|
|
|
|
this->onMovement = functionPointer;
|
2013-11-28 10:26:29 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-17 16:07:25 +01:00
|
|
|
this->onMovement = Default::EventAction_Move;
|
2013-11-28 10:26:29 +01:00
|
|
|
}
|
2013-11-26 13:28:33 +01:00
|
|
|
}
|
|
|
|
|
2013-12-19 10:53:55 +01:00
|
|
|
void SphericalRigidBody::SetScene( void *scene )
|
|
|
|
{
|
|
|
|
this->scene = (Octree*)scene;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SphericalRigidBody::SetGravity( bool ignore )
|
2013-11-26 13:28:33 +01:00
|
|
|
{
|
|
|
|
this->ignoreGravity = ignore;
|
|
|
|
this->gravityNormal = Float3::null;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SphericalRigidBody::SetGravityNormal( const Float3 &normalizedVector )
|
|
|
|
{
|
|
|
|
this->gravityNormal = normalizedVector;
|
|
|
|
}
|
|
|
|
|
2014-01-20 13:44:12 +01:00
|
|
|
void SphericalRigidBody::SetCustomTag( void *ref )
|
|
|
|
{
|
|
|
|
this->customTag = ref;
|
|
|
|
}
|
|
|
|
|
2013-12-18 14:16:13 +01:00
|
|
|
//void SphericalRigidBody::SetMomentOfInertiaTensor_KeepVelocity( const Float4x4 &localI )
|
|
|
|
//{
|
|
|
|
// this->rigid.SetMomentOfInertia_KeepVelocity( localI );
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//void SphericalRigidBody::SetMomentOfInertiaTensor_KeepMomentum( const Float4x4 &localI )
|
|
|
|
//{
|
|
|
|
// this->rigid.SetMomentOfInertia_KeepMomentum( localI );
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//void SphericalRigidBody::SetMass_KeepVelocity( Float m )
|
|
|
|
//{
|
|
|
|
// this->rigid.SetMass_KeepVelocity( m );
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//void SphericalRigidBody::SetMass_KeepMomentum( Float m )
|
|
|
|
//{
|
|
|
|
// this->rigid.SetMass_KeepMomentum( m );
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//void SphericalRigidBody::SetCenter( const Float3 &worldPos )
|
|
|
|
//{
|
|
|
|
// this->rigid.SetCenter( worldPos );
|
|
|
|
// this->body.center = worldPos;
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//void SphericalRigidBody::SetRotation( const Float4x4 &rotation )
|
|
|
|
//{
|
|
|
|
// this->rigid.SetRotation( rotation );
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//void SphericalRigidBody::SetOrientation( const Float4x4 &orientation )
|
|
|
|
//{
|
|
|
|
// this->rigid.SetOrientation( orientation );
|
|
|
|
// this->body.center = orientation.v[3].xyz;
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//void SphericalRigidBody::SetSize( const Float3 &size )
|
|
|
|
//{
|
|
|
|
// this->rigid.SetSize( size );
|
|
|
|
// this->body.radius = 0.5f * Min( Min( size.x, size.y ), size.z ); // inline Min( FloatN )?
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//void SphericalRigidBody::SetMomentum( const Float3 &worldG )
|
|
|
|
//{
|
|
|
|
// this->rigid.SetLinearMomentum( worldG );
|
|
|
|
//}
|