From 42418257cb0c122af17c4d4c91c34a4da19adb6d Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Thu, 19 Dec 2013 10:53:55 +0100 Subject: [PATCH] CustomBodies now aware of it's scene + other minor changes --- Code/GamePhysics/Implementation/Octree.cpp | 1 + .../Implementation/PhysicsAPI_Impl.cpp | 7 ++-- .../Implementation/SimpleRigidBody.cpp | 30 ++++++++++++++++- .../Implementation/SimpleRigidBody.h | 3 ++ .../Implementation/SphericalRigidBody.cpp | 32 +++++++++++++++++-- .../Implementation/SphericalRigidBody.h | 3 ++ Code/GamePhysics/PhysicsAPI.h | 7 ++++ 7 files changed, 76 insertions(+), 7 deletions(-) diff --git a/Code/GamePhysics/Implementation/Octree.cpp b/Code/GamePhysics/Implementation/Octree.cpp index b841b49d..a1ccdbd8 100644 --- a/Code/GamePhysics/Implementation/Octree.cpp +++ b/Code/GamePhysics/Implementation/Octree.cpp @@ -31,6 +31,7 @@ Octree& Octree::operator=(const Octree& orig) void Octree::AddObject(UniquePointer< ICustomBody > customBodyRef) { + customBodyRef->SetScene( this ); Data data; //Data* tempPtr = this->worldNode.dataPtr; diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index 9d30a7e4..24d46d0b 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -44,10 +44,9 @@ namespace protoState.GetMass(), protoG_Magnitude );; Float4 sumJ = normal*impulse; - sumJ -= Formula::CollisionResponse::Friction( impulse, normal, protoState.GetLinearMomentum(), - protoState.GetFrictionCoeff(), 0.2f, protoState.GetMass(), - deuterState.GetLinearMomentum(), deuterState.GetFrictionCoeff(), - 0.2f, deuterState.GetMass()); + sumJ -= Formula::CollisionResponse::Friction( impulse, normal, + protoState.GetLinearMomentum(), protoState.GetFrictionCoeff_Static(), protoState.GetFrictionCoeff_Kinetic(), protoState.GetMass(), + deuterState.GetLinearMomentum(), deuterState.GetFrictionCoeff_Static(), deuterState.GetFrictionCoeff_Kinetic(), deuterState.GetMass()); // calc from perspective of proto proto->GetNormalAt( worldPointOfContact, normal ); diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp index 16d7113d..dff17bac 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp @@ -14,6 +14,7 @@ SimpleRigidBody::SimpleRigidBody() this->gravityNormal = Float3::null; this->collisionAction = Default::EventAction_Collision; this->ignoreGravity = false; + this->scene = nullptr; } SimpleRigidBody::SimpleRigidBody( const API::SimpleBodyDescription &desc ) @@ -33,6 +34,7 @@ SimpleRigidBody::SimpleRigidBody( const API::SimpleBodyDescription &desc ) } this->ignoreGravity = desc.ignoreGravity; + this->scene = nullptr; } SimpleRigidBody::~SimpleRigidBody() {} @@ -61,10 +63,31 @@ SimpleRigidBody::State & SimpleRigidBody::GetState( SimpleRigidBody::State &targ } 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(); + 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) ); + } + } } ICustomBody::SubscriptMessage SimpleRigidBody::CallSubscription( const ICustomBody *proto, const ICustomBody *deuter ) @@ -187,6 +210,11 @@ UpdateState SimpleRigidBody::Update( Float timeStepLength ) return UpdateState_altered; } +void SimpleRigidBody::SetScene( void *scene ) +{ + this->scene = (Octree*)scene; +} + void SimpleRigidBody::SetSubscription( ICustomBody::EventAction_Collision functionPointer ) { if( functionPointer ) diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.h b/Code/GamePhysics/Implementation/SimpleRigidBody.h index c314d09d..3929669b 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.h +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.h @@ -3,6 +3,7 @@ #include "..\PhysicsAPI.h" #include "RigidBody.h" +#include "Octree.h" namespace Oyster { namespace Physics { @@ -36,6 +37,7 @@ namespace Oyster { namespace Physics UpdateState Update( ::Oyster::Math::Float timeStepLength ); + void SetScene( void *scene ); void SetSubscription( EventAction_Collision functionPointer ); void SetGravity( bool ignore); void SetGravityNormal( const ::Oyster::Math::Float3 &normalizedVector ); @@ -53,6 +55,7 @@ namespace Oyster { namespace Physics ::Oyster::Physics3D::RigidBody rigid; ::Oyster::Math::Float3 gravityNormal; EventAction_Collision collisionAction; + Octree *scene; bool ignoreGravity; }; } } diff --git a/Code/GamePhysics/Implementation/SphericalRigidBody.cpp b/Code/GamePhysics/Implementation/SphericalRigidBody.cpp index a3ccfa77..9dcfaa32 100644 --- a/Code/GamePhysics/Implementation/SphericalRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SphericalRigidBody.cpp @@ -14,6 +14,7 @@ SphericalRigidBody::SphericalRigidBody() this->gravityNormal = Float3::null; this->collisionAction = Default::EventAction_Collision; this->ignoreGravity = false; + this->scene = nullptr; this->body = Sphere( Float3::null, 0.5f ); } @@ -34,6 +35,7 @@ SphericalRigidBody::SphericalRigidBody( const API::SphericalBodyDescription &des } this->ignoreGravity = desc.ignoreGravity; + this->scene = nullptr; this->body = Sphere( desc.centerPosition, desc.radius ); } @@ -63,10 +65,31 @@ SphericalRigidBody::State & SphericalRigidBody::GetState( SphericalRigidBody::St } void SphericalRigidBody::SetState( const SphericalRigidBody::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(); + 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) ); + } + } } ICustomBody::SubscriptMessage SphericalRigidBody::CallSubscription( const ICustomBody *proto, const ICustomBody *deuter ) @@ -156,7 +179,12 @@ void SphericalRigidBody::SetSubscription( ICustomBody::EventAction_Collision fun } } -void SphericalRigidBody::SetGravity( bool ignore) +void SphericalRigidBody::SetScene( void *scene ) +{ + this->scene = (Octree*)scene; +} + +void SphericalRigidBody::SetGravity( bool ignore ) { this->ignoreGravity = ignore; this->gravityNormal = Float3::null; diff --git a/Code/GamePhysics/Implementation/SphericalRigidBody.h b/Code/GamePhysics/Implementation/SphericalRigidBody.h index 3136f4e7..4c2d74e5 100644 --- a/Code/GamePhysics/Implementation/SphericalRigidBody.h +++ b/Code/GamePhysics/Implementation/SphericalRigidBody.h @@ -4,6 +4,7 @@ #include "..\PhysicsAPI.h" #include "RigidBody.h" #include "Sphere.h" +#include "Octree.h" namespace Oyster { namespace Physics { @@ -37,6 +38,7 @@ namespace Oyster { namespace Physics UpdateState Update( ::Oyster::Math::Float timeStepLength ); + void SetScene( void *scene ); void SetSubscription( EventAction_Collision functionPointer ); void SetGravity( bool ignore); void SetGravityNormal( const ::Oyster::Math::Float3 &normalizedVector ); @@ -55,6 +57,7 @@ namespace Oyster { namespace Physics ::Oyster::Math::Float3 gravityNormal; EventAction_Collision collisionAction; bool ignoreGravity; + Octree *scene; ::Oyster::Collision3D::Sphere body; }; } } diff --git a/Code/GamePhysics/PhysicsAPI.h b/Code/GamePhysics/PhysicsAPI.h index 945247b0..3e91a103 100644 --- a/Code/GamePhysics/PhysicsAPI.h +++ b/Code/GamePhysics/PhysicsAPI.h @@ -338,6 +338,13 @@ namespace Oyster ********************************************************/ virtual UpdateState Update( ::Oyster::Math::Float timeStepLength ) = 0; + /******************************************************** + * Sets which scene this ICustomBody is within. + * Reserved to only be used by the scene. + * @todo TODO: create an IScene interface + ********************************************************/ + virtual void SetScene( void *scene ) = 0; + /******************************************************** * Sets the function that will be called by the engine * whenever a collision occurs.