From 7075e6add62c905296ce6347b29d60007c308e66 Mon Sep 17 00:00:00 2001 From: Robin Engman Date: Tue, 11 Feb 2014 13:36:14 +0100 Subject: [PATCH] Gravity well --- .../Implementation/PhysicsAPI_Impl.cpp | 18 ++++++++++++++++++ .../Implementation/PhysicsAPI_Impl.h | 6 ++++++ Code/GamePhysics/PhysicsAPI.h | 2 ++ 3 files changed, 26 insertions(+) diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index e68c860f..43e86a30 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -26,6 +26,9 @@ API_Impl::API_Impl() this->dynamicsWorld = NULL; this->timeStep = 1.0f/120.0f; + + this->gravityPoint = Float3(0.0f, 0.0f, 0.0f); + this->gravity = 10.0f; } API_Impl::~API_Impl() @@ -48,6 +51,16 @@ API_Impl::~API_Impl() } } +void API_Impl::SetGravityPoint(::Oyster::Math::Float3 gravityPoint) +{ + this->gravityPoint = gravityPoint; +} + +void API_Impl::SetGravity(float gravity) +{ + this->gravity = gravity; +} + // Bullet physics ICustomBody* API_Impl::AddCollisionSphere(float radius, ::Oyster::Math::Float4 rotation, ::Oyster::Math::Float3 position, float mass, float restitution, float staticFriction, float dynamicFriction) { @@ -173,6 +186,11 @@ void API_Impl::SetTimeStep(float timeStep) void API_Impl::UpdateWorld() { + for(unsigned int i = 0; i < this->customBodies.size(); i++ ) + { + this->customBodies[i]->SetGravity(-(this->customBodies[i]->GetState().centerPos - this->gravityPoint).GetNormalized()*this->gravity); + } + this->dynamicsWorld->stepSimulation(this->timeStep, 1, this->timeStep); ICustomBody::State state; diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h index 2d4c55ed..3cef6373 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h @@ -21,6 +21,9 @@ namespace Oyster void MoveToLimbo( const ICustomBody* objRef ); void ReleaseFromLimbo( const ICustomBody* objRef ); + void SetGravityPoint(::Oyster::Math::Float3 gravityPoint); + void SetGravity(float gravity); + // Bullet physics ICustomBody* AddCollisionSphere(float radius, ::Oyster::Math::Float4 rotation, ::Oyster::Math::Float3 position, float mass, float restitution, float staticFriction, float dynamicFriction); ICustomBody* AddCollisionBox(::Oyster::Math::Float3 halfSize, ::Oyster::Math::Float4 rotation, ::Oyster::Math::Float3 position, float mass, float restitution, float staticFriction, float dynamicFriction); @@ -41,6 +44,9 @@ namespace Oyster std::vector customBodies; float timeStep; + + ::Oyster::Math::Float3 gravityPoint; + float gravity; }; namespace Default diff --git a/Code/GamePhysics/PhysicsAPI.h b/Code/GamePhysics/PhysicsAPI.h index d44cb2db..ff2ba374 100644 --- a/Code/GamePhysics/PhysicsAPI.h +++ b/Code/GamePhysics/PhysicsAPI.h @@ -78,6 +78,8 @@ namespace Oyster ********************************************************/ virtual void ReleaseFromLimbo( const ICustomBody* objRef ) = 0; + virtual void SetGravityPoint(::Oyster::Math::Float3 gravityPoint) = 0; + virtual void SetGravity(float gravity) = 0; // Bullet physics virtual ICustomBody* AddCollisionSphere(float radius, ::Oyster::Math::Float4 rotation, ::Oyster::Math::Float3 position, float mass, float restitution, float staticFriction, float dynamicFriction) = 0;