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
|
|
|
{
|
|
|
|
this->rigid = RigidBody( Box(Float4x4::identity, Float3::null, Float3(1.0f)), 10.0f, Float4x4::identity );
|
|
|
|
this->gravityNormal = Float3::null;
|
|
|
|
this->collisionAction = Default::EventAction_Collision;
|
|
|
|
this->ignoreGravity = false;
|
2013-12-19 10:53:55 +01:00
|
|
|
this->scene = nullptr;
|
2013-11-28 12:13:14 +01:00
|
|
|
this->body = Sphere( Float3::null, 0.5f );
|
|
|
|
}
|
|
|
|
|
|
|
|
SphericalRigidBody::SphericalRigidBody( const API::SphericalBodyDescription &desc )
|
|
|
|
{
|
2013-12-18 14:16:13 +01:00
|
|
|
this->rigid = RigidBody( Box( desc.rotation, desc.centerPosition.xyz, Float3(2.0f * desc.radius) ),
|
2013-11-28 12:13:14 +01:00
|
|
|
desc.mass,
|
2013-12-16 08:58:15 +01:00
|
|
|
Formula::MomentOfInertia::CreateSphereMatrix( desc.mass, desc.radius ) );
|
2013-11-28 12:13:14 +01:00
|
|
|
this->gravityNormal = Float3::null;
|
|
|
|
|
|
|
|
if( desc.subscription )
|
|
|
|
{
|
|
|
|
this->collisionAction = desc.subscription;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->collisionAction = Default::EventAction_Collision;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->ignoreGravity = desc.ignoreGravity;
|
2013-12-19 10:53:55 +01:00
|
|
|
this->scene = nullptr;
|
2013-11-28 12:13:14 +01:00
|
|
|
this->body = Sphere( desc.centerPosition, desc.radius );
|
|
|
|
}
|
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-18 14:16:13 +01:00
|
|
|
this->rigid.GetMomentOfInertia(), this->rigid.box.boundingOffset,
|
|
|
|
this->rigid.box.center, AngularAxis(this->rigid.box.rotation),
|
|
|
|
Float4(this->rigid.linearMomentum, 0.0f), Float4(this->rigid.angularMomentum, 0.0f) );
|
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-18 14:16:13 +01:00
|
|
|
this->rigid.GetMomentOfInertia(), this->rigid.box.boundingOffset,
|
|
|
|
this->rigid.box.center, AngularAxis(this->rigid.box.rotation),
|
|
|
|
Float4(this->rigid.linearMomentum, 0.0f), Float4(this->rigid.angularMomentum, 0.0f) );
|
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-06 09:46:30 +01:00
|
|
|
this->rigid.box.boundingOffset = state.GetReach();
|
|
|
|
this->rigid.box.center = state.GetCenterPosition();
|
|
|
|
this->rigid.box.rotation = state.GetRotation();
|
2013-12-19 10:53:55 +01:00
|
|
|
this->rigid.angularMomentum = state.GetAngularMomentum().xyz;
|
|
|
|
this->rigid.linearMomentum = state.GetLinearMomentum().xyz;
|
|
|
|
this->rigid.impulseTorqueSum += state.GetAngularImpulse().xyz;
|
|
|
|
this->rigid.impulseForceSum += state.GetLinearImpulse().xyz;
|
|
|
|
this->rigid.restitutionCoeff = state.GetRestitutionCoeff();
|
|
|
|
this->rigid.frictionCoeff_Static = state.GetFrictionCoeff_Static();
|
|
|
|
this->rigid.frictionCoeff_Kinetic = state.GetFrictionCoeff_Kinetic();
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-12-18 08:57:27 +01:00
|
|
|
ICustomBody::SubscriptMessage SphericalRigidBody::CallSubscription( const ICustomBody *proto, const ICustomBody *deuter )
|
2013-11-29 09:21:44 +01:00
|
|
|
{
|
2013-12-18 08:57:27 +01:00
|
|
|
return this->collisionAction( proto, deuter );
|
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-18 08:57:27 +01:00
|
|
|
return Sphere( this->rigid.box.center, this->rigid.box.boundingOffset.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
|
|
|
|
{
|
|
|
|
return Sphere( this->rigid.box.center, this->rigid.box.boundingOffset.x ).Intersects( shape, worldPointOfContact );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SphericalRigidBody::Intersects( const ICustomBody &object, Float4 &worldPointOfContact ) const
|
2013-11-26 13:28:33 +01:00
|
|
|
{
|
2013-12-18 08:57:27 +01:00
|
|
|
return object.Intersects( Sphere(this->rigid.box.center, this->rigid.box.boundingOffset.x), worldPointOfContact );
|
2013-11-26 13:28:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Sphere & SphericalRigidBody::GetBoundingSphere( Sphere &targetMem ) const
|
|
|
|
{
|
|
|
|
return targetMem = this->body;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2013-12-19 11:15:49 +01:00
|
|
|
targetMem = worldPos - this->rigid.box.center;
|
|
|
|
Float magnitude = targetMem.GetMagnitude();
|
|
|
|
if( magnitude != 0.0f )
|
|
|
|
{ // sanity check
|
|
|
|
targetMem.Normalize();
|
|
|
|
}
|
|
|
|
return targetMem;
|
2013-11-26 13:28:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Float3 & SphericalRigidBody::GetGravityNormal( Float3 &targetMem ) const
|
|
|
|
{
|
|
|
|
return targetMem = this->gravityNormal;
|
|
|
|
}
|
|
|
|
|
2013-12-18 14:16:13 +01:00
|
|
|
//Float3 & SphericalRigidBody::GetCenter( Float3 &targetMem ) const
|
|
|
|
//{
|
|
|
|
// return targetMem = this->rigid.box.center;
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//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-12 10:02:35 +01:00
|
|
|
Float3 SphericalRigidBody::GetRigidLinearVelocity() const
|
|
|
|
{
|
|
|
|
return this->rigid.GetLinearVelocity();
|
|
|
|
}
|
|
|
|
|
2013-11-26 13:28:33 +01:00
|
|
|
UpdateState SphericalRigidBody::Update( Float timeStepLength )
|
|
|
|
{
|
2013-11-28 12:13:14 +01:00
|
|
|
this->rigid.Update_LeapFrog( timeStepLength );
|
|
|
|
this->body.center = this->rigid.GetCenter();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void SphericalRigidBody::SetSubscription( ICustomBody::EventAction_Collision functionPointer )
|
|
|
|
{
|
|
|
|
if( functionPointer )
|
|
|
|
{
|
|
|
|
this->collisionAction = functionPointer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->collisionAction = Default::EventAction_Collision;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
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 );
|
|
|
|
//}
|