From c18eb668aadf5a015a6baa919582e691a731a59c Mon Sep 17 00:00:00 2001 From: Robin Engman Date: Mon, 10 Feb 2014 14:18:45 +0100 Subject: [PATCH 1/3] Added more data for bodies and added cylinder --- .../Implementation/PhysicsAPI_Impl.cpp | 32 +++++++++++++++++-- .../Implementation/PhysicsAPI_Impl.h | 5 +-- .../Implementation/SimpleRigidBody.cpp | 21 ++++++++++++ .../Implementation/SimpleRigidBody.h | 4 +++ Code/GamePhysics/PhysicsAPI.h | 9 ++++-- Code/GamePhysics/PhysicsStructs.h | 7 +++- 6 files changed, 70 insertions(+), 8 deletions(-) diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index 4c3a25a0..7d70e146 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -47,7 +47,7 @@ API_Impl::~API_Impl() } // Bullet physics -ICustomBody* API_Impl::AddCollisionSphere(float radius, ::Oyster::Math::Float4 rotation, ::Oyster::Math::Float3 position, float mass) +ICustomBody* API_Impl::AddCollisionSphere(float radius, ::Oyster::Math::Float4 rotation, ::Oyster::Math::Float3 position, float mass, float restitution, float staticFriction, float dynamicFriction) { SimpleRigidBody* body = new SimpleRigidBody; @@ -73,7 +73,8 @@ ICustomBody* API_Impl::AddCollisionSphere(float radius, ::Oyster::Math::Float4 r return body; } -ICustomBody* API_Impl::AddCollisionBox(Float3 halfSize, ::Oyster::Math::Float4 rotation, ::Oyster::Math::Float3 position, float mass) + +ICustomBody* API_Impl::AddCollisionBox(Float3 halfSize, ::Oyster::Math::Float4 rotation, ::Oyster::Math::Float3 position, float mass, float restitution, float staticFriction, float dynamicFriction) { SimpleRigidBody* body = new SimpleRigidBody; @@ -100,6 +101,33 @@ ICustomBody* API_Impl::AddCollisionBox(Float3 halfSize, ::Oyster::Math::Float4 r return body; } +ICustomBody* API_Impl::AddCollisionCylinder(::Oyster::Math::Float3 halfSize, ::Oyster::Math::Float4 rotation, ::Oyster::Math::Float3 position, float mass, float restitution, float staticFriction, float dynamicFriction) +{ + SimpleRigidBody* body = new SimpleRigidBody; + + // Add collision shape + btCollisionShape* collisionShape = new btCylinderShape(btVector3(halfSize.x, halfSize.y, halfSize.z)); + body->SetCollisionShape(collisionShape); + + // Add motion state + btDefaultMotionState* motionState = new btDefaultMotionState(btTransform(btQuaternion(rotation.x, rotation.y, rotation.z, rotation.w),btVector3(position.x, position.y, position.z))); + body->SetMotionState(motionState); + + // Add rigid body + btVector3 fallInertia(0, 0, 0); + collisionShape->calculateLocalInertia(mass, fallInertia); + btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass, motionState, collisionShape, fallInertia); + btRigidBody* rigidBody = new btRigidBody(rigidBodyCI); + rigidBody->setUserPointer(body); + body->SetRigidBody(rigidBody); + + // Add rigid body to world + this->dynamicsWorld->addRigidBody(rigidBody); + this->customBodies.push_back(body); + + return body; +} + void API_Impl::UpdateWorld() { this->dynamicsWorld->stepSimulation(1.0f/60.0f, 1.0f, 1.0f/60.0f); diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h index 03d1d3c6..28111d26 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h @@ -22,8 +22,9 @@ namespace Oyster void ReleaseFromLimbo( const ICustomBody* objRef ); // Bullet physics - ICustomBody* AddCollisionSphere(float radius, ::Oyster::Math::Float4 rotation, ::Oyster::Math::Float3 position, float mass); - ICustomBody* AddCollisionBox(::Oyster::Math::Float3 halfSize, ::Oyster::Math::Float4 rotation, ::Oyster::Math::Float3 position, float mass); + 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); + ICustomBody* AddCollisionCylinder(::Oyster::Math::Float3 halfSize, ::Oyster::Math::Float4 rotation, ::Oyster::Math::Float3 position, float mass, float restitution, float staticFriction, float dynamicFriction); void UpdateWorld(); diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp index e5fea58b..9639a424 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp @@ -64,6 +64,27 @@ void SimpleRigidBody::SetSubscription(EventAction_Move function) this->onMovement = function; } +void SimpleRigidBody::SetLinearVelocity(Float3 velocity) +{ + this->rigidBody->setLinearVelocity(btVector3(velocity.x, velocity.y, velocity.z)); +} + +void SimpleRigidBody::SetRotation(Float4 quaternion) +{ + btTransform trans; + this->motionState->getWorldTransform(trans); + trans.setRotation(btQuaternion(quaternion.x, quaternion.y, quaternion.z, quaternion.w)); + this->motionState->setWorldTransform(trans); +} + +void SimpleRigidBody::SetRotation(Float3 eulerAngles) +{ + btTransform trans; + this->motionState->getWorldTransform(trans); + trans.setRotation(btQuaternion(eulerAngles.x, eulerAngles.y, eulerAngles.z)); + this->motionState->setWorldTransform(trans); +} + void SimpleRigidBody::CallSubscription_AfterCollisionResponse(ICustomBody* bodyA, ICustomBody* bodyB, Oyster::Math::Float kineticEnergyLoss) { if(this->onMovement) diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.h b/Code/GamePhysics/Implementation/SimpleRigidBody.h index d3655048..25fecfad 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.h +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.h @@ -21,6 +21,10 @@ namespace Oyster void SetSubscription(EventAction_AfterCollisionResponse function); void SetSubscription(EventAction_Move function); + void SetLinearVelocity(Math::Float3 velocity); + void SetRotation(Math::Float4 quaternion); + void SetRotation(Math::Float3 eulerAngles); + void CallSubscription_AfterCollisionResponse(ICustomBody* bodyA, ICustomBody* bodyB, Math::Float kineticEnergyLoss); void CallSubscription_Move(); diff --git a/Code/GamePhysics/PhysicsAPI.h b/Code/GamePhysics/PhysicsAPI.h index c8bfde34..84f77b7d 100644 --- a/Code/GamePhysics/PhysicsAPI.h +++ b/Code/GamePhysics/PhysicsAPI.h @@ -80,9 +80,9 @@ namespace Oyster // Bullet physics - virtual ICustomBody* AddCollisionSphere(float radius, ::Oyster::Math::Float4 rotation, ::Oyster::Math::Float3 position, float mass) = 0; - - virtual ICustomBody* AddCollisionBox(::Oyster::Math::Float3 halfSize, ::Oyster::Math::Float4 rotation, ::Oyster::Math::Float3 position, float mass) = 0; + virtual ICustomBody* AddCollisionSphere(float radius, ::Oyster::Math::Float4 rotation, ::Oyster::Math::Float3 position, float mass, float restitution, float staticFriction, float dynamicFriction) = 0; + virtual ICustomBody* AddCollisionBox(::Oyster::Math::Float3 halfSize, ::Oyster::Math::Float4 rotation, ::Oyster::Math::Float3 position, float mass, float restitution, float staticFriction, float dynamicFriction) = 0; + virtual ICustomBody* AddCollisionCylinder(::Oyster::Math::Float3 halfSize, ::Oyster::Math::Float4 rotation, ::Oyster::Math::Float3 position, float mass, float restitution, float staticFriction, float dynamicFriction) = 0; virtual void UpdateWorld() = 0; @@ -144,6 +144,9 @@ namespace Oyster virtual void SetSubscription(EventAction_AfterCollisionResponse function) = 0; virtual void SetSubscription(EventAction_Move function) = 0; + virtual void SetLinearVelocity(::Oyster::Math::Float3 velocity) = 0; + virtual void SetRotation(::Oyster::Math::Float4 quaternion) = 0; + virtual void SetRotation(::Oyster::Math::Float3 eulerAngles) = 0; /******************************************************** * @return the void pointer set by SetCustomTag. diff --git a/Code/GamePhysics/PhysicsStructs.h b/Code/GamePhysics/PhysicsStructs.h index 5dc7d6f1..f68491d5 100644 --- a/Code/GamePhysics/PhysicsStructs.h +++ b/Code/GamePhysics/PhysicsStructs.h @@ -30,8 +30,13 @@ namespace Oyster ::Oyster::Math::Float4x4 GetOrientation() const; ::Oyster::Math::Float4x4 GetView() const; ::Oyster::Math::Float4x4 GetView( const ::Oyster::Math::Float3 &offset ) const; - void CustomBodyState::ApplyFriction( const ::Oyster::Math::Float3 &j); + ::Oyster::Math::Float GetMass() const; + ::Oyster::Math::Float GetStaticFriction() const; + ::Oyster::Math::Float GetDynamicFriction() const; + ::Oyster::Math::Float GetRestitution() const; + + // Variables for state ::Oyster::Math::Float mass, restitutionCoeff, staticFrictionCoeff, dynamicFrictionCoeff; ::Oyster::Math::Float3 reach, centerPos; From 8547d17d0f567bc689aa976384359f27d90a7f10 Mon Sep 17 00:00:00 2001 From: Robin Engman Date: Mon, 10 Feb 2014 14:22:10 +0100 Subject: [PATCH 2/3] Friction and restitution is now set --- Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index 7d70e146..3c43fb29 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -64,6 +64,8 @@ ICustomBody* API_Impl::AddCollisionSphere(float radius, ::Oyster::Math::Float4 r collisionShape->calculateLocalInertia(mass, fallInertia); btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass, motionState, collisionShape, fallInertia); btRigidBody* rigidBody = new btRigidBody(rigidBodyCI); + rigidBody->setFriction(staticFriction); + rigidBody->setRestitution(restitution); rigidBody->setUserPointer(body); body->SetRigidBody(rigidBody); @@ -91,6 +93,8 @@ ICustomBody* API_Impl::AddCollisionBox(Float3 halfSize, ::Oyster::Math::Float4 r collisionShape->calculateLocalInertia(mass, fallInertia); btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass, motionState, collisionShape, fallInertia); btRigidBody* rigidBody = new btRigidBody(rigidBodyCI); + rigidBody->setFriction(staticFriction); + rigidBody->setRestitution(restitution); rigidBody->setUserPointer(body); body->SetRigidBody(rigidBody); @@ -118,6 +122,8 @@ ICustomBody* API_Impl::AddCollisionCylinder(::Oyster::Math::Float3 halfSize, ::O collisionShape->calculateLocalInertia(mass, fallInertia); btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass, motionState, collisionShape, fallInertia); btRigidBody* rigidBody = new btRigidBody(rigidBodyCI); + rigidBody->setFriction(staticFriction); + rigidBody->setRestitution(restitution); rigidBody->setUserPointer(body); body->SetRigidBody(rigidBody); From bd8722f107eca4026f47988515ca84ddf4503f78 Mon Sep 17 00:00:00 2001 From: Robin Engman Date: Mon, 10 Feb 2014 14:39:45 +0100 Subject: [PATCH 3/3] Options for rigid bodies are now set directly --- .../Implementation/PhysicsAPI_Impl.cpp | 8 +-- .../Implementation/SimpleRigidBody.cpp | 56 +++++++++++++------ .../Implementation/SimpleRigidBody.h | 11 ++-- Code/GamePhysics/PhysicsAPI.h | 24 +++----- Code/GamePhysics/PhysicsStructs.h | 6 -- 5 files changed, 56 insertions(+), 49 deletions(-) diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index 3c43fb29..7f3a53a7 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -142,19 +142,15 @@ void API_Impl::UpdateWorld() for(unsigned int i = 0; i < this->customBodies.size(); i++ ) { - this->customBodies[i]->GetState(state); - btTransform trans; dynamic_cast(this->customBodies[i])->GetMotionState()->getWorldTransform(trans); - 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()); + this->customBodies[i]->SetPosition(Float3(trans.getOrigin().x(), trans.getOrigin().y(), trans.getOrigin().z())); + this->customBodies[i]->SetRotation(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); } int numManifolds = this->dynamicsWorld->getDispatcher()->getNumManifolds(); diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp index 9639a424..0621c2c6 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp @@ -69,12 +69,31 @@ void SimpleRigidBody::SetLinearVelocity(Float3 velocity) this->rigidBody->setLinearVelocity(btVector3(velocity.x, velocity.y, velocity.z)); } +void SimpleRigidBody::SetPosition(::Oyster::Math::Float3 position) +{ + btTransform trans; + this->motionState->getWorldTransform(trans); + trans.setOrigin(btVector3(position.x, position.y, position.z)); + this->motionState->setWorldTransform(trans); + this->state.centerPos = position; +} + void SimpleRigidBody::SetRotation(Float4 quaternion) { btTransform trans; this->motionState->getWorldTransform(trans); trans.setRotation(btQuaternion(quaternion.x, quaternion.y, quaternion.z, quaternion.w)); this->motionState->setWorldTransform(trans); + this->state.quaternion = Quaternion(quaternion.xyz, quaternion.w); +} + +void SimpleRigidBody::SetRotation(::Oyster::Math::Quaternion quaternion) +{ + btTransform trans; + this->motionState->getWorldTransform(trans); + trans.setRotation(btQuaternion(quaternion.imaginary.x, quaternion.imaginary.y, quaternion.imaginary.z, quaternion.real)); + this->motionState->setWorldTransform(trans); + this->state.quaternion = quaternion; } void SimpleRigidBody::SetRotation(Float3 eulerAngles) @@ -83,6 +102,27 @@ void SimpleRigidBody::SetRotation(Float3 eulerAngles) this->motionState->getWorldTransform(trans); trans.setRotation(btQuaternion(eulerAngles.x, eulerAngles.y, eulerAngles.z)); this->motionState->setWorldTransform(trans); + this->state.quaternion = Quaternion(Float3(trans.getRotation().x(), trans.getRotation().y(), trans.getRotation().z()), trans.getRotation().w()); +} + +Float4x4 SimpleRigidBody::GetRotation() const +{ + return this->state.GetRotation(); +} + +Float4x4 SimpleRigidBody::GetOrientation() const +{ + return this->state.GetOrientation(); +} + +Float4x4 SimpleRigidBody::GetView() const +{ + return this->state.GetView(); +} + +Float4x4 SimpleRigidBody::GetView( const ::Oyster::Math::Float3 &offset ) const +{ + return this->state.GetView(offset); } void SimpleRigidBody::CallSubscription_AfterCollisionResponse(ICustomBody* bodyA, ICustomBody* bodyB, Oyster::Math::Float kineticEnergyLoss) @@ -112,22 +152,6 @@ btRigidBody* SimpleRigidBody::GetRigidBody() const return this->rigidBody; } -SimpleRigidBody::State SimpleRigidBody::GetState() const -{ - return this->state; -} - -SimpleRigidBody::State & SimpleRigidBody::GetState( SimpleRigidBody::State &targetMem ) const -{ - targetMem = this->state; - return targetMem; -} - -void SimpleRigidBody::SetState( const SimpleRigidBody::State &state ) -{ - this->state = state; -} - void * SimpleRigidBody::GetCustomTag() const { return this->customTag; diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.h b/Code/GamePhysics/Implementation/SimpleRigidBody.h index 25fecfad..caca6313 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.h +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.h @@ -22,16 +22,19 @@ namespace Oyster void SetSubscription(EventAction_Move function); void SetLinearVelocity(Math::Float3 velocity); + void SetPosition(::Oyster::Math::Float3 position); void SetRotation(Math::Float4 quaternion); + void SetRotation(::Oyster::Math::Quaternion quaternion); void SetRotation(Math::Float3 eulerAngles); + Math::Float4x4 GetRotation() const; + Math::Float4x4 GetOrientation() const; + Math::Float4x4 GetView() const; + Math::Float4x4 GetView( const ::Oyster::Math::Float3 &offset ) const; + void CallSubscription_AfterCollisionResponse(ICustomBody* bodyA, ICustomBody* bodyB, Math::Float kineticEnergyLoss); void CallSubscription_Move(); - State GetState() const; - State & GetState( State &targetMem ) const; - void SetState( const State &state ); - btCollisionShape* GetCollisionShape() const; btDefaultMotionState* GetMotionState() const; btRigidBody* GetRigidBody() const; diff --git a/Code/GamePhysics/PhysicsAPI.h b/Code/GamePhysics/PhysicsAPI.h index 84f77b7d..608bf722 100644 --- a/Code/GamePhysics/PhysicsAPI.h +++ b/Code/GamePhysics/PhysicsAPI.h @@ -123,31 +123,21 @@ namespace Oyster virtual ~ICustomBody() {}; - /******************************************************** - * Gets the current state of the rigid body - * @return the current state of the rigid body - ********************************************************/ - virtual State GetState() const = 0; - - /******************************************************** - * Gets the current state of the rigid body - * @param targetMem: The state is copied into targetMem - * @return the current state of the rigid body - ********************************************************/ - virtual State & GetState( State &targetMem ) const = 0; - - /******************************************************** - * Sets the current state of the rigid body - ********************************************************/ - virtual void SetState( const State &state ) = 0; virtual void SetSubscription(EventAction_AfterCollisionResponse function) = 0; virtual void SetSubscription(EventAction_Move function) = 0; virtual void SetLinearVelocity(::Oyster::Math::Float3 velocity) = 0; + virtual void SetPosition(::Oyster::Math::Float3 position) = 0; virtual void SetRotation(::Oyster::Math::Float4 quaternion) = 0; + virtual void SetRotation(::Oyster::Math::Quaternion quaternion) = 0; virtual void SetRotation(::Oyster::Math::Float3 eulerAngles) = 0; + ::Oyster::Math::Float4x4 GetRotation() const; + ::Oyster::Math::Float4x4 GetOrientation() const; + ::Oyster::Math::Float4x4 GetView() const; + ::Oyster::Math::Float4x4 GetView( const ::Oyster::Math::Float3 &offset ) const; + /******************************************************** * @return the void pointer set by SetCustomTag. * nullptr if none is set. diff --git a/Code/GamePhysics/PhysicsStructs.h b/Code/GamePhysics/PhysicsStructs.h index f68491d5..4c4ae797 100644 --- a/Code/GamePhysics/PhysicsStructs.h +++ b/Code/GamePhysics/PhysicsStructs.h @@ -30,12 +30,6 @@ namespace Oyster ::Oyster::Math::Float4x4 GetOrientation() const; ::Oyster::Math::Float4x4 GetView() const; ::Oyster::Math::Float4x4 GetView( const ::Oyster::Math::Float3 &offset ) const; - - ::Oyster::Math::Float GetMass() const; - ::Oyster::Math::Float GetStaticFriction() const; - ::Oyster::Math::Float GetDynamicFriction() const; - ::Oyster::Math::Float GetRestitution() const; - // Variables for state ::Oyster::Math::Float mass, restitutionCoeff, staticFrictionCoeff, dynamicFrictionCoeff;