diff --git a/Code/GamePhysics/GamePhysics.vcxproj.filters b/Code/GamePhysics/GamePhysics.vcxproj.filters index 72b83568..694a8ec7 100644 --- a/Code/GamePhysics/GamePhysics.vcxproj.filters +++ b/Code/GamePhysics/GamePhysics.vcxproj.filters @@ -51,7 +51,7 @@ Source Files - Header Files\Implementation + Source Files \ No newline at end of file diff --git a/Code/GamePhysics/Implementation/Octree.cpp b/Code/GamePhysics/Implementation/Octree.cpp index 6a6416b8..baa431da 100644 --- a/Code/GamePhysics/Implementation/Octree.cpp +++ b/Code/GamePhysics/Implementation/Octree.cpp @@ -2,6 +2,9 @@ using namespace Oyster; using namespace Physics; +using namespace ::Utility::DynamicMemory; + +const unsigned int Octree::invalid_ref = ::Utility::Value::numeric_limits::max(); Octree::Octree(unsigned int bufferSize, unsigned char numLayers, Math::Float3 worldSize) { @@ -26,7 +29,7 @@ Octree& Octree::operator=(const Octree& orig) return *this; } -void Octree::AddObject(Utility::DynamicMemory::UniquePointer< ICustomBody > customBodyRef) +void Octree::AddObject(UniquePointer< ICustomBody > customBodyRef) { Data data; //Data* tempPtr = this->worldNode.dataPtr; @@ -54,15 +57,15 @@ void Octree::AddObject(Utility::DynamicMemory::UniquePointer< ICustomBody > cust }*/ } -void Octree::MoveToUpdateQueue(Utility::DynamicMemory::UniquePointer< ICustomBody > customBodyRef) +void Octree::MoveToUpdateQueue(UniquePointer< ICustomBody > customBodyRef) { /*this->leafData[this->mapReferences[customBodyRef]].queueRef = this->updateQueue.size(); this->updateQueue.push_back(&this->leafData[this->mapReferences[customBodyRef]]);*/ } -void Octree::DestroyObject(Utility::DynamicMemory::UniquePointer< ICustomBody > customBodyRef) +void Octree::DestroyObject(UniquePointer< ICustomBody > customBodyRef) { - std::map::iterator it = this->mapReferences.find(customBodyRef); + std::map::iterator it = this->mapReferences.find(customBodyRef); this->mapReferences.erase(it); @@ -116,4 +119,53 @@ void Octree::Visit(ICustomBody* customBodyRef, VistorAction hitAction ) ICustomBody* Octree::GetCustomBody(const unsigned int tempRef) { return this->leafData[tempRef].customBodyRef; +} + +UniquePointer Octree::Extract( const ICustomBody* objRef ) +{ // Dan Andersson + auto iter = this->mapReferences.find( objRef ); + if( iter != this->mapReferences.end() ) + { + return this->Extract( iter->second ); + } + else + { + return NULL; + } +} + +UniquePointer Octree::Extract( unsigned int tempRef ) +{ + if( tempRef != Octree::invalid_ref ) + { + //! @todo TODO: implement stub + return NULL; + } + else + { + return NULL; + } +} + +unsigned int Octree::GetTemporaryReferenceOf( const ICustomBody* objRef ) const +{ // Dan Andersson + auto iter = this->mapReferences.find( objRef ); + if( iter != this->mapReferences.end() ) + { + return iter->second; + } + else + { + return Octree::invalid_ref; + } +} + +void Octree::SetAsAltered( unsigned int tempRef ) +{ + //! @todo TODO: implement stub +} + +void Octree::EvaluatePosition( unsigned int tempRef ) +{ + //! @todo TODO: implement stub } \ No newline at end of file diff --git a/Code/GamePhysics/Implementation/Octree.h b/Code/GamePhysics/Implementation/Octree.h index 8d111329..5ef3b0a8 100644 --- a/Code/GamePhysics/Implementation/Octree.h +++ b/Code/GamePhysics/Implementation/Octree.h @@ -15,6 +15,8 @@ namespace Oyster class Octree { public: + static const unsigned int invalid_ref; + typedef void(*VistorAction)(Octree&, unsigned int, unsigned int); struct Data @@ -52,11 +54,17 @@ namespace Oyster ICustomBody* GetCustomBody(const unsigned int tempRef); + ::Utility::DynamicMemory::UniquePointer Extract( const ICustomBody* objRef ); + ::Utility::DynamicMemory::UniquePointer Extract( unsigned int tempRef ); // Dan vill ha + unsigned int GetTemporaryReferenceOf( const ICustomBody* objRef ) const; // Dan vill ha + void SetAsAltered( unsigned int tempRef ); // Dan vill ha + void EvaluatePosition( unsigned int tempRef ); // Dan vill ha + private: std::vector < Data > leafData; std::vector < Data* > updateQueue; - std::map< ICustomBody*, unsigned int > mapReferences; + std::map< const ICustomBody*, unsigned int > mapReferences; OctreeNode worldNode; }; diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index 6c9b212a..0374704f 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -42,21 +42,24 @@ API & API::Instance() } API_Impl::API_Impl() - : gravityConstant( Constant::gravity_constant ), - updateFrameLength( 1.0f / 120.0f ), - destructionAction( Default::EventAction_Destruction ) -{} +{ + this->gravityConstant = Constant::gravity_constant; + this->updateFrameLength = 1.0f / 120.0f; + this->destructionAction = Default::EventAction_Destruction; + this->worldScene = Octree(); +} API_Impl::~API_Impl() {} void API_Impl::Init( unsigned int numObjects, unsigned int numGravityWells , const Float3 &worldSize ) { - //! @todo TODO: implement stub + unsigned char numLayers = 4; //!< @todo TODO: calc numLayers from worldSize + this->worldScene = Octree( numObjects, numLayers, worldSize ); } -void API_Impl::SetDeltaTime( float deltaTime ) +void API_Impl::SetFrameTimeLength( float deltaTime ) { - updateFrameLength = deltaTime; + this->updateFrameLength = deltaTime; } void API_Impl::SetGravityConstant( float g ) @@ -98,68 +101,117 @@ void API_Impl::ReleaseFromLimbo( const ICustomBody* objRef ) void API_Impl::AddObject( ::Utility::DynamicMemory::UniquePointer handle ) { - /** @todo TODO: Fix this function.*/ + this->worldScene.AddObject( handle ); } -::Utility::DynamicMemory::UniquePointer API_Impl::ExtractObject( const ICustomBody* objRef ) +UniquePointer API_Impl::ExtractObject( const ICustomBody* objRef ) { - //! @todo TODO: implement stub - return NULL; + return this->worldScene.Extract( objRef ); } void API_Impl::DestroyObject( const ICustomBody* objRef ) { - /** @todo TODO: Fix this function.*/ + UniquePointer object = this->worldScene.Extract( objRef ); + if( object != NULL ) + { + this->destructionAction( object ); + } } void API_Impl::ApplyForceAt( const ICustomBody* objRef, const Float3 &worldPos, const Float3 &worldF ) { - //! @todo TODO: implement stub + unsigned int tempRef = this->worldScene.GetTemporaryReferenceOf( objRef ); + if( tempRef != this->worldScene.invalid_ref ) + { + //this->worldScene.GetCustomBody( tempRef )->Apply //!< @todo TODO: need function + this->worldScene.SetAsAltered( tempRef ); + } } void API_Impl::ApplyCollisionResponse( const ICustomBody* objRefA, const ICustomBody* objRefB, Float &deltaWhen, Float3 &worldPointOfContact ) { - //! @todo TODO: implement stub + unsigned int tempRef = this->worldScene.GetTemporaryReferenceOf( objRefA ); + if( tempRef != this->worldScene.invalid_ref ) + { + //! @todo TODO: implement stub + this->worldScene.SetAsAltered( tempRef ); + } } void API_Impl::SetMomentOfInertiaTensor_KeepVelocity( const ICustomBody* objRef, const Float4x4 &localI ) -{ - //! @todo TODO: implement stub +{ // deprecated + unsigned int tempRef = this->worldScene.GetTemporaryReferenceOf( objRef ); + if( tempRef != this->worldScene.invalid_ref ) + { + this->worldScene.GetCustomBody( tempRef )->SetMomentOfInertiaTensor_KeepVelocity( localI ); + } } void API_Impl::SetMomentOfInertiaTensor_KeepMomentum( const ICustomBody* objRef, const Float4x4 &localI ) -{ - //! @todo TODO: implement stub +{ // deprecated + unsigned int tempRef = this->worldScene.GetTemporaryReferenceOf( objRef ); + if( tempRef != this->worldScene.invalid_ref ) + { + this->worldScene.GetCustomBody( tempRef )->SetMomentOfInertiaTensor_KeepMomentum( localI ); + } } void API_Impl::SetMass_KeepVelocity( const ICustomBody* objRef, Float m ) -{ - //! @todo TODO: implement stub +{ // deprecated + unsigned int tempRef = this->worldScene.GetTemporaryReferenceOf( objRef ); + if( tempRef != this->worldScene.invalid_ref ) + { + this->worldScene.GetCustomBody( tempRef )->SetMass_KeepVelocity( m ); + } } void API_Impl::SetMass_KeepMomentum( const ICustomBody* objRef, Float m ) -{ - //! @todo TODO: implement stub +{ // deprecated + unsigned int tempRef = this->worldScene.GetTemporaryReferenceOf( objRef ); + if( tempRef != this->worldScene.invalid_ref ) + { + this->worldScene.GetCustomBody( tempRef )->SetMass_KeepMomentum( m ); + } } void API_Impl::SetCenter( const ICustomBody* objRef, const Float3 &worldPos ) { - //! @todo TODO: implement stub + unsigned int tempRef = this->worldScene.GetTemporaryReferenceOf( objRef ); + if( tempRef != this->worldScene.invalid_ref ) + { + //this->worldScene.GetCustomBody( tempRef )->Set //!< @todo TODO: need function + this->worldScene.EvaluatePosition( tempRef ); + } } void API_Impl::SetRotation( const ICustomBody* objRef, const Float4x4 &rotation ) { - //! @todo TODO: implement stub + unsigned int tempRef = this->worldScene.GetTemporaryReferenceOf( objRef ); + if( tempRef != this->worldScene.invalid_ref ) + { + this->worldScene.GetCustomBody( tempRef )->SetRotation( rotation ); + this->worldScene.EvaluatePosition( tempRef ); + } } void API_Impl::SetOrientation( const ICustomBody* objRef, const Float4x4 &orientation ) { - //! @todo TODO: implement stub + unsigned int tempRef = this->worldScene.GetTemporaryReferenceOf( objRef ); + if( tempRef != this->worldScene.invalid_ref ) + { + this->worldScene.GetCustomBody( tempRef )->SetOrientation( orientation ); + this->worldScene.EvaluatePosition( tempRef ); + } } void API_Impl::SetSize( const ICustomBody* objRef, const Float3 &size ) { - //! @todo TODO: implement stub + unsigned int tempRef = this->worldScene.GetTemporaryReferenceOf( objRef ); + if( tempRef != this->worldScene.invalid_ref ) + { + this->worldScene.GetCustomBody( tempRef )->SetSize( size ); + this->worldScene.EvaluatePosition( tempRef ); + } } UniquePointer API_Impl::CreateRigidBody( const API::SimpleBodyDescription &desc ) const @@ -172,15 +224,16 @@ UniquePointer API_Impl::CreateRigidBody( const API::SphericalBodyDe return new SphericalRigidBody( desc ); } -namespace Oyster { namespace Physics { namespace Default +namespace Oyster { namespace Physics { + namespace Default + { + void EventAction_Destruction( ::Utility::DynamicMemory::UniquePointer<::Oyster::Physics::ICustomBody> proto ) + { /* Do nothing except allowing the proto uniquePointer destroy itself. */ } - void EventAction_Destruction( ::Utility::DynamicMemory::UniquePointer<::Oyster::Physics::ICustomBody> proto ) - { /* Do nothing except allowing the proto uniquePointer destroy itself. */ } - - ::Oyster::Physics::ICustomBody::SubscriptMessage EventAction_Collision( const ::Oyster::Physics::ICustomBody *proto, const ::Oyster::Physics::ICustomBody *deuter ) - { /* Do nothing except returning business as usual. */ - return ::Oyster::Physics::ICustomBody::SubscriptMessage_none; + ::Oyster::Physics::ICustomBody::SubscriptMessage EventAction_Collision( const ::Oyster::Physics::ICustomBody *proto, const ::Oyster::Physics::ICustomBody *deuter ) + { /* Do nothing except returning business as usual. */ + return ::Oyster::Physics::ICustomBody::SubscriptMessage_none; + } } - -} } } \ No newline at end of file +} } \ No newline at end of file diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h index 7d6858f0..5de9c05a 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h @@ -2,6 +2,7 @@ #define PHYSICS_API_IMPL_H #include "../PhysicsAPI.h" +#include "Octree.h" namespace Oyster { @@ -15,7 +16,7 @@ namespace Oyster void Init( unsigned int numObjects, unsigned int numGravityWells , const ::Oyster::Math::Float3 &worldSize ); - void SetDeltaTime( float deltaTime ); + void SetFrameTimeLength( float deltaTime ); void SetGravityConstant( float g ); void SetSubscription( EventAction_Destruction functionPointer ); @@ -47,6 +48,7 @@ namespace Oyster private: ::Oyster::Math::Float gravityConstant, updateFrameLength; EventAction_Destruction destructionAction; + Octree worldScene; }; namespace Default diff --git a/Code/GamePhysics/PhysicsAPI.h b/Code/GamePhysics/PhysicsAPI.h index 6cba993c..5d44b695 100644 --- a/Code/GamePhysics/PhysicsAPI.h +++ b/Code/GamePhysics/PhysicsAPI.h @@ -64,7 +64,7 @@ namespace Oyster /******************************************************** * Sets the time length of each physics update frame. ********************************************************/ - virtual void SetDeltaTime( float seconds ) = 0; + virtual void SetFrameTimeLength( float seconds ) = 0; /******************************************************** * Sets the Gravityconstant in the physics that will be