From 05aa3cc9e91795e24f7f99e89c668dd5dd539436 Mon Sep 17 00:00:00 2001 From: Robin Engman Date: Mon, 10 Feb 2014 13:55:01 +0100 Subject: [PATCH] Added on move functionality --- Code/Game/GameLogic/Game.cpp | 2 +- .../Implementation/PhysicsAPI_Impl.cpp | 10 ++- .../Implementation/SimpleRigidBody.cpp | 36 ++++++++--- .../Implementation/SimpleRigidBody.h | 64 +++++++++++-------- Code/GamePhysics/PhysicsAPI.h | 2 + 5 files changed, 72 insertions(+), 42 deletions(-) diff --git a/Code/Game/GameLogic/Game.cpp b/Code/Game/GameLogic/Game.cpp index b76970b2..ceb45520 100644 --- a/Code/Game/GameLogic/Game.cpp +++ b/Code/Game/GameLogic/Game.cpp @@ -71,7 +71,7 @@ Game::PlayerData* Game::CreatePlayer() int i = InsertObject(this->players, (PlayerData*)0); this->players[i] = new PlayerData(); - //this->players[i]->player->GetRigidBody()->SetSubscription(Game::PhysicsOnMove); + this->players[i]->player->GetRigidBody()->SetSubscription(Game::PhysicsOnMove); return this->players[i]; } diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index a71c58e3..4c3a25a0 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -102,7 +102,7 @@ ICustomBody* API_Impl::AddCollisionBox(Float3 halfSize, ::Oyster::Math::Float4 r void API_Impl::UpdateWorld() { - this->dynamicsWorld->stepSimulation(1.0f/120.0f, 1.0f, 1.0f/120.0f); + this->dynamicsWorld->stepSimulation(1.0f/60.0f, 1.0f, 1.0f/60.0f); ICustomBody::State state; @@ -115,6 +115,10 @@ void API_Impl::UpdateWorld() state.centerPos = Float3(trans.getOrigin().x(), trans.getOrigin().y(), trans.getOrigin().z()); state.quaternion = Quaternion(Float3(trans.getRotation().x(), trans.getRotation().y(), trans.getRotation().z()), trans.getRotation().w()); + if(dynamic_cast(this->customBodies[i])->GetRigidBody()->getActivationState() == ACTIVE_TAG) + { + dynamic_cast(this->customBodies[i])->CallSubscription_Move(); + } this->customBodies[i]->SetState(state); } @@ -129,8 +133,8 @@ void API_Impl::UpdateWorld() ICustomBody* bodyA = (ICustomBody*)obA->getUserPointer(); ICustomBody* bodyB = (ICustomBody*)obB->getUserPointer(); - //dynamic_cast(bodyA)->CallSubsciptMessage(bodyA, bodyB, 0.0f); - //dynamic_cast(bodyB)->CallSubsciptMessage(bodyB, bodyA, 0.0f); + dynamic_cast(bodyA)->CallSubscription_AfterCollisionResponse(bodyA, bodyB, 0.0f); + dynamic_cast(bodyB)->CallSubscription_AfterCollisionResponse(bodyB, bodyA, 0.0f); int numContacts = contactManifold->getNumContacts(); for (int j=0;jstate.restitutionCoeff = 0.0f; this->state.reach = Float3(0.0f, 0.0f, 0.0f); - this->customTag = nullptr; -} - -SimpleRigidBody::SimpleRigidBody( const API::SimpleBodyDescription &desc ) -{ - this->collisionShape = NULL; - this->motionState = NULL; - this->rigidBody = NULL; + this->afterCollision = NULL; + this->onMovement = NULL; this->customTag = nullptr; } @@ -65,9 +59,26 @@ void SimpleRigidBody::SetSubscription(EventAction_AfterCollisionResponse functio this->afterCollision = function; } -void SimpleRigidBody::CallSubsciptMessage(ICustomBody* bodyA, ICustomBody* bodyB, Oyster::Math::Float kineticEnergyLoss) +void SimpleRigidBody::SetSubscription(EventAction_Move function) { - this->afterCollision(bodyA, bodyB, kineticEnergyLoss); + this->onMovement = function; +} + +void SimpleRigidBody::CallSubscription_AfterCollisionResponse(ICustomBody* bodyA, ICustomBody* bodyB, Oyster::Math::Float kineticEnergyLoss) +{ + if(this->onMovement) + this->afterCollision(bodyA, bodyB, kineticEnergyLoss); +} + +void SimpleRigidBody::CallSubscription_Move() +{ + if(this->onMovement) + this->onMovement(this); +} + +btCollisionShape* SimpleRigidBody::GetCollisionShape() const +{ + return this->collisionShape; } btDefaultMotionState* SimpleRigidBody::GetMotionState() const @@ -75,6 +86,11 @@ btDefaultMotionState* SimpleRigidBody::GetMotionState() const return this->motionState; } +btRigidBody* SimpleRigidBody::GetRigidBody() const +{ + return this->rigidBody; +} + SimpleRigidBody::State SimpleRigidBody::GetState() const { return this->state; diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.h b/Code/GamePhysics/Implementation/SimpleRigidBody.h index 1b699e9a..d3655048 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.h +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.h @@ -4,42 +4,50 @@ #include "..\PhysicsAPI.h" #include -namespace Oyster { namespace Physics -{ - class SimpleRigidBody : public ICustomBody +namespace Oyster +{ + namespace Physics { - public: - SimpleRigidBody(); - SimpleRigidBody( const API::SimpleBodyDescription &desc ); - virtual ~SimpleRigidBody(); + class SimpleRigidBody : public ICustomBody + { + public: + SimpleRigidBody(); + virtual ~SimpleRigidBody(); - void SetCollisionShape(btCollisionShape* shape); - void SetMotionState(btDefaultMotionState* motionState); - void SetRigidBody(btRigidBody* rigidBody); - - void SetSubscription(EventAction_AfterCollisionResponse function); - void CallSubsciptMessage(ICustomBody* bodyA, ICustomBody* bodyB, Math::Float kineticEnergyLoss); + void SetCollisionShape(btCollisionShape* shape); + void SetMotionState(btDefaultMotionState* motionState); + void SetRigidBody(btRigidBody* rigidBody); + + void SetSubscription(EventAction_AfterCollisionResponse function); + void SetSubscription(EventAction_Move function); - State GetState() const; - State & GetState( State &targetMem ) const; - void SetState( const State &state ); + void CallSubscription_AfterCollisionResponse(ICustomBody* bodyA, ICustomBody* bodyB, Math::Float kineticEnergyLoss); + void CallSubscription_Move(); - btDefaultMotionState* GetMotionState() const; + State GetState() const; + State & GetState( State &targetMem ) const; + void SetState( const State &state ); - void SetCustomTag( void *ref ); - void* GetCustomTag() const; + btCollisionShape* GetCollisionShape() const; + btDefaultMotionState* GetMotionState() const; + btRigidBody* GetRigidBody() const; - private: - btCollisionShape* collisionShape; - btDefaultMotionState* motionState; - btRigidBody* rigidBody; + void SetCustomTag( void *ref ); + void* GetCustomTag() const; - Struct::CustomBodyState state; + private: + btCollisionShape* collisionShape; + btDefaultMotionState* motionState; + btRigidBody* rigidBody; - EventAction_AfterCollisionResponse afterCollision; + Struct::CustomBodyState state; - void *customTag; - }; -} } + EventAction_AfterCollisionResponse afterCollision; + EventAction_Move onMovement; + + void *customTag; + }; + } +} #endif \ No newline at end of file diff --git a/Code/GamePhysics/PhysicsAPI.h b/Code/GamePhysics/PhysicsAPI.h index 9af0e256..c8bfde34 100644 --- a/Code/GamePhysics/PhysicsAPI.h +++ b/Code/GamePhysics/PhysicsAPI.h @@ -142,6 +142,8 @@ namespace Oyster virtual void SetState( const State &state ) = 0; virtual void SetSubscription(EventAction_AfterCollisionResponse function) = 0; + virtual void SetSubscription(EventAction_Move function) = 0; + /******************************************************** * @return the void pointer set by SetCustomTag.