From 1aa66dc79fc8c5646ad9d5daf878a855bce913ff Mon Sep 17 00:00:00 2001 From: Robin Engman Date: Tue, 11 Feb 2014 13:53:44 +0100 Subject: [PATCH 1/3] Quaternion to angular axis complete --- .../Implementation/SimpleRigidBody.cpp | 22 +++++++++++++++++++ .../Implementation/SimpleRigidBody.h | 1 + Code/GamePhysics/PhysicsAPI.h | 9 ++++---- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp index 1201cad8..58f598ed 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp @@ -186,6 +186,28 @@ Float4x4 SimpleRigidBody::GetRotation() const { return this->state.GetRotation(); } +Float4 SimpleRigidBody::GetRotationAsAngularAxis() +{ + Float4 axis = Float4::null; + Float s = sqrtf(1 - this->state.quaternion.real*this->state.quaternion.real); + + axis.w = 2*acos(this->state.quaternion.real*this->state.quaternion.real); + + if(1 - this->state.quaternion.real > 0.001f) + { + axis.x = this->state.quaternion.imaginary.x/s; + axis.y = this->state.quaternion.imaginary.y/s; + axis.z = this->state.quaternion.imaginary.z/s; + } + else + { + axis.x = this->state.quaternion.imaginary.x; + axis.y = this->state.quaternion.imaginary.y; + axis.z = this->state.quaternion.imaginary.z; + } + + return axis; +} Float4x4 SimpleRigidBody::GetOrientation() const { diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.h b/Code/GamePhysics/Implementation/SimpleRigidBody.h index 70709c83..039345eb 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.h +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.h @@ -38,6 +38,7 @@ namespace Oyster void SetUpAndForward(::Oyster::Math::Float3 up, ::Oyster::Math::Float3 forward); Math::Float4x4 GetRotation() const; + Math::Float4 GetRotationAsAngularAxis(); Math::Float4x4 GetOrientation() const; Math::Float4x4 GetView() const; Math::Float4x4 GetView( const ::Oyster::Math::Float3 &offset ) const; diff --git a/Code/GamePhysics/PhysicsAPI.h b/Code/GamePhysics/PhysicsAPI.h index ff2ba374..46f663d7 100644 --- a/Code/GamePhysics/PhysicsAPI.h +++ b/Code/GamePhysics/PhysicsAPI.h @@ -146,10 +146,11 @@ namespace Oyster 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; - ::Oyster::Math::Float4x4 GetView() const; - ::Oyster::Math::Float4x4 GetView( const ::Oyster::Math::Float3 &offset ) const; + virtual ::Oyster::Math::Float4x4 GetRotation() const = 0; + virtual ::Oyster::Math::Float4 GetRotationAsAngularAxis() = 0; + virtual ::Oyster::Math::Float4x4 GetOrientation() const = 0; + virtual ::Oyster::Math::Float4x4 GetView() const = 0; + virtual ::Oyster::Math::Float4x4 GetView(const ::Oyster::Math::Float3 &offset) const = 0; /******************************************************** * @return the void pointer set by SetCustomTag. From 5d245d71515ac07923f7c4f58b09376dc12d6d01 Mon Sep 17 00:00:00 2001 From: Robin Engman Date: Tue, 11 Feb 2014 14:00:38 +0100 Subject: [PATCH 2/3] Added quaternion to angular axis in Math --- Code/OysterMath/OysterMath.cpp | 46 ++++++++++++++++++++++++++++++++++ Code/OysterMath/OysterMath.h | 6 +++++ 2 files changed, 52 insertions(+) diff --git a/Code/OysterMath/OysterMath.cpp b/Code/OysterMath/OysterMath.cpp index bbaccf11..7f7b1393 100644 --- a/Code/OysterMath/OysterMath.cpp +++ b/Code/OysterMath/OysterMath.cpp @@ -96,6 +96,52 @@ namespace Oyster { namespace Math3D // return ::LinearAlgebra3D::ExtractAngularAxis( orientationMatrix ); //} + Float4 QuaternionToAngularAxis(Float4 quaternion) + { + Float4 axis = Float4::null; + Float s = sqrtf(1 - quaternion.w*quaternion.w); + + axis.w = 2*acos(quaternion.w*quaternion.w); + + if(1 - quaternion.w > 0.001f) + { + axis.x = quaternion.x/s; + axis.y = quaternion.y/s; + axis.z = quaternion.z/s; + } + else + { + axis.x = quaternion.x; + axis.y = quaternion.y; + axis.z = quaternion.z; + } + + return axis; + } + + Float4 QuaternionToAngularAxis(Quaternion quaternion) + { + Float4 axis = Float4::null; + Float s = sqrtf(1 - quaternion.real*quaternion.real); + + axis.w = 2*acos(quaternion.real*quaternion.real); + + if(1 - quaternion.real > 0.001f) + { + axis.x = quaternion.imaginary.x/s; + axis.y = quaternion.imaginary.y/s; + axis.z = quaternion.imaginary.z/s; + } + else + { + axis.x = quaternion.imaginary.x; + axis.y = quaternion.imaginary.y; + axis.z = quaternion.imaginary.z; + } + + return axis; + } + Float4x4 & TranslationMatrix( const Float3 &position, Float4x4 &targetMem ) { return ::LinearAlgebra3D::TranslationMatrix( position, targetMem ); diff --git a/Code/OysterMath/OysterMath.h b/Code/OysterMath/OysterMath.h index 23ee06db..6e8a66de 100644 --- a/Code/OysterMath/OysterMath.h +++ b/Code/OysterMath/OysterMath.h @@ -149,6 +149,12 @@ namespace Oyster { namespace Math3D //! Oyster's native math library specialized ////! Extracts the angularAxis from orientationMatrix //Float4 ExtractAngularAxis( const Float4x4 &orientationMatrix ); + //! Converts a quaternion as Float4 to angular axis as Float4 + Float4 QuaternionToAngularAxis(Float4 quaternion); + + //! Converts a quaternion to angular axis as Float4 + Float4 QuaternionToAngularAxis(Quaternion quaternion); + //! Sets and returns targetMem to a translationMatrix with position as translation. Float4x4 & TranslationMatrix( const Float3 &position, Float4x4 &targetMem = Float4x4() ); From 754199441976eb214097e263c20c8f9af71b00ba Mon Sep 17 00:00:00 2001 From: Robin Engman Date: Wed, 12 Feb 2014 08:50:10 +0100 Subject: [PATCH 3/3] Added support for applying impulse --- Code/GamePhysics/Implementation/SimpleRigidBody.cpp | 5 +++++ Code/GamePhysics/Implementation/SimpleRigidBody.h | 2 ++ Code/GamePhysics/PhysicsAPI.h | 2 ++ 3 files changed, 9 insertions(+) diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp index 58f598ed..7cd2f2b6 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp @@ -67,6 +67,11 @@ void SimpleRigidBody::SetState( const SimpleRigidBody::State &state ) this->state = state; } +void SimpleRigidBody::ApplyImpulse(Float3 impulse) +{ + this->rigidBody->applyImpulse(btVector3(impulse.x, impulse.y, impulse.z), btVector3(0.0f, 0.0f, 0.0f)); +} + void SimpleRigidBody::SetCollisionShape(btCollisionShape* shape) { this->collisionShape = shape; diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.h b/Code/GamePhysics/Implementation/SimpleRigidBody.h index 039345eb..3663c45c 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.h +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.h @@ -18,6 +18,8 @@ namespace Oyster State& GetState( State &targetMem ) const; void SetState( const State &state ); + void ApplyImpulse(Math::Float3 impulse); + void SetCollisionShape(btCollisionShape* shape); void SetMotionState(btDefaultMotionState* motionState); void SetRigidBody(btRigidBody* rigidBody); diff --git a/Code/GamePhysics/PhysicsAPI.h b/Code/GamePhysics/PhysicsAPI.h index 46f663d7..d971fe79 100644 --- a/Code/GamePhysics/PhysicsAPI.h +++ b/Code/GamePhysics/PhysicsAPI.h @@ -134,6 +134,8 @@ namespace Oyster virtual void SetSubscription(EventAction_AfterCollisionResponse function) = 0; virtual void SetSubscription(EventAction_Move function) = 0; + virtual void ApplyImpulse(::Oyster::Math::Float3 impulse) = 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;