From 985dcf9691386f0ba5888fa46e5a4b9f0fa7dab9 Mon Sep 17 00:00:00 2001 From: Robin Engman Date: Tue, 11 Feb 2014 09:15:21 +0100 Subject: [PATCH] Set up and stop angular movement support added --- .../Implementation/PhysicsAPI_Impl.cpp | 36 +++++++--------- .../Implementation/PhysicsAPI_Impl.h | 4 ++ .../Implementation/SimpleRigidBody.cpp | 43 +++++++++++++++++++ .../Implementation/SimpleRigidBody.h | 8 +++- Code/GamePhysics/PhysicsAPI.h | 6 +++ 5 files changed, 74 insertions(+), 23 deletions(-) diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index 9026a60f..e68c860f 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -24,6 +24,8 @@ API_Impl::API_Impl() this->dispatcher = NULL; this->solver = NULL; this->dynamicsWorld = NULL; + + this->timeStep = 1.0f/120.0f; } API_Impl::~API_Impl() @@ -39,7 +41,7 @@ API_Impl::~API_Impl() delete this->broadphase; this->broadphase = NULL; - for(int i = 0; i < this->customBodies.size(); i++) + for(unsigned int i = 0; i < this->customBodies.size(); i++) { delete this->customBodies[i]; this->customBodies[i] = NULL; @@ -164,27 +166,33 @@ ICustomBody* API_Impl::AddCollisionCylinder(::Oyster::Math::Float3 halfSize, ::O return body; } +void API_Impl::SetTimeStep(float timeStep) +{ + this->timeStep = timeStep; +} + void API_Impl::UpdateWorld() { - this->dynamicsWorld->stepSimulation(1.0f/60.0f, 1.0f, 1.0f/60.0f); + this->dynamicsWorld->stepSimulation(this->timeStep, 1, this->timeStep); ICustomBody::State state; for(unsigned int i = 0; i < this->customBodies.size(); i++ ) { + SimpleRigidBody* simpleBody = dynamic_cast(this->customBodies[i]); btTransform trans; - dynamic_cast(this->customBodies[i])->GetMotionState()->getWorldTransform(trans); + simpleBody->GetMotionState()->getWorldTransform(trans); 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) + if(simpleBody->GetRigidBody()->getActivationState() == ACTIVE_TAG) { - dynamic_cast(this->customBodies[i])->CallSubscription_Move(); + simpleBody->CallSubscription_Move(); } } int numManifolds = this->dynamicsWorld->getDispatcher()->getNumManifolds(); - for (int i=0;idynamicsWorld->getDispatcher()->getManifoldByIndexInternal(i); const btCollisionObject* obA = contactManifold->getBody0(); @@ -195,18 +203,6 @@ void API_Impl::UpdateWorld() 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;jgetContactPoint(j); - if (pt.getDistance()<0.f) - { - const btVector3& ptA = pt.getPositionWorldOnA(); - const btVector3& ptB = pt.getPositionWorldOnB(); - const btVector3& normalOnB = pt.m_normalWorldOnB; - } - } } } @@ -257,9 +253,7 @@ namespace Oyster } void EventAction_AfterCollisionResponse( const ::Oyster::Physics::ICustomBody *proto, const ::Oyster::Physics::ICustomBody *deuter, ::Oyster::Math::Float kineticEnergyLoss ) - { /* Do nothing except returning business as usual. */ - - } + { /* Do nothing except returning business as usual. */ } void EventAction_Move( const ::Oyster::Physics::ICustomBody *object ) { /* Do nothing. */ } diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h index 28111d26..2d4c55ed 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h @@ -26,6 +26,8 @@ namespace Oyster 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 SetTimeStep(float timeStep); + void UpdateWorld(); void ApplyEffect( const Oyster::Collision3D::ICollideable& collideable, void* args, void(hitAction)(ICustomBody*, void*) ); @@ -37,6 +39,8 @@ namespace Oyster btSequentialImpulseConstraintSolver* solver; btDiscreteDynamicsWorld* dynamicsWorld; std::vector customBodies; + + float timeStep; }; namespace Default diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp index 879ff8bc..c2ae4cde 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp @@ -134,6 +134,49 @@ void SimpleRigidBody::SetRotation(Float3 eulerAngles) this->state.quaternion = Quaternion(Float3(trans.getRotation().x(), trans.getRotation().y(), trans.getRotation().z()), trans.getRotation().w()); } +void SimpleRigidBody::SetAngularFactor(Float factor) +{ + this->rigidBody->setAngularFactor(factor); +} + +void SimpleRigidBody::SetUpAndRight(::Oyster::Math::Float3 up, ::Oyster::Math::Float3 right) +{ + btMatrix3x3 rotation; + btVector3 upVector(up.x, up.y, up.z); + btVector3 rightVector(right.x, right.y, right.z); + rotation[1] = upVector.normalized(); + rotation[0] = rightVector.normalized(); + rotation[2] = upVector.cross(rightVector).normalized(); + + btQuaternion quaternion; + rotation.getRotation(quaternion); + this->state.quaternion = Quaternion(Float3(quaternion.x(), quaternion.y(), quaternion.z()), quaternion.w()); + + btTransform trans; + this->motionState->getWorldTransform(trans); + trans.setRotation(quaternion); + this->motionState->setWorldTransform(trans); +} + +void SimpleRigidBody::SetUpAndForward(::Oyster::Math::Float3 up, ::Oyster::Math::Float3 forward) +{ + btMatrix3x3 rotation; + btVector3 upVector(up.x, up.y, up.z); + btVector3 forwardVector(forward.x, forward.y, forward.z); + rotation[1] = upVector.normalized(); + rotation[0] = forwardVector.normalized(); + rotation[2] = upVector.cross(forwardVector).normalized(); + + btQuaternion quaternion; + rotation.getRotation(quaternion); + this->state.quaternion = Quaternion(Float3(quaternion.x(), quaternion.y(), quaternion.z()), quaternion.w()); + + btTransform trans; + this->motionState->getWorldTransform(trans); + trans.setRotation(quaternion); + this->motionState->setWorldTransform(trans); +} + Float4x4 SimpleRigidBody::GetRotation() const { return this->state.GetRotation(); diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.h b/Code/GamePhysics/Implementation/SimpleRigidBody.h index 55ae4fe3..f85b24df 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.h +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.h @@ -26,10 +26,14 @@ namespace Oyster void SetSubscription(EventAction_Move function); void SetLinearVelocity(Math::Float3 velocity); - void SetPosition(::Oyster::Math::Float3 position); + void SetPosition(Math::Float3 position); void SetRotation(Math::Float4 quaternion); - void SetRotation(::Oyster::Math::Quaternion quaternion); + void SetRotation(Math::Quaternion quaternion); void SetRotation(Math::Float3 eulerAngles); + void SetAngularFactor(Math::Float factor); + + void SetUpAndRight(::Oyster::Math::Float3 up, ::Oyster::Math::Float3 right); + void SetUpAndForward(::Oyster::Math::Float3 up, ::Oyster::Math::Float3 forward); Math::Float4x4 GetRotation() const; Math::Float4x4 GetOrientation() const; diff --git a/Code/GamePhysics/PhysicsAPI.h b/Code/GamePhysics/PhysicsAPI.h index f57fe130..767caa98 100644 --- a/Code/GamePhysics/PhysicsAPI.h +++ b/Code/GamePhysics/PhysicsAPI.h @@ -84,6 +84,8 @@ namespace Oyster 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 SetTimeStep(float timeStep) = 0; + virtual void UpdateWorld() = 0; @@ -135,6 +137,10 @@ namespace Oyster virtual void SetRotation(::Oyster::Math::Float4 quaternion) = 0; virtual void SetRotation(::Oyster::Math::Quaternion quaternion) = 0; virtual void SetRotation(::Oyster::Math::Float3 eulerAngles) = 0; + virtual void SetAngularFactor(::Oyster::Math::Float factor) = 0; + + virtual void SetUpAndRight(::Oyster::Math::Float3 up, ::Oyster::Math::Float3 right) = 0; + virtual void SetUpAndForward(::Oyster::Math::Float3 up, ::Oyster::Math::Float3 forward) = 0; ::Oyster::Math::Float4x4 GetRotation() const; ::Oyster::Math::Float4x4 GetOrientation() const;