From 7834264e5fc469e125d7b8cd3d3ab41facfbde83 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Fri, 10 Jan 2014 08:53:20 +0100 Subject: [PATCH 01/18] Physics proj .. show all files should be set to false That is how we in Physics rolls --- Code/OysterPhysics3D/OysterPhysics3D.vcxproj.user | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Code/OysterPhysics3D/OysterPhysics3D.vcxproj.user b/Code/OysterPhysics3D/OysterPhysics3D.vcxproj.user index 9a0b0ae0..3f030911 100644 --- a/Code/OysterPhysics3D/OysterPhysics3D.vcxproj.user +++ b/Code/OysterPhysics3D/OysterPhysics3D.vcxproj.user @@ -1,6 +1,6 @@  - true + false \ No newline at end of file From 502513d4f8f302c71ba9ca459099d6076d9c7418 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Mon, 13 Jan 2014 10:25:20 +0100 Subject: [PATCH 02/18] Work asssignment split line --- Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index f917407b..0d0d31b2 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -73,6 +73,9 @@ namespace } protoState.ApplyForwarding( forwardedDeltaPos, forwardedDeltaAxis ); + + /////// Dan below / Robin Above + protoState.ApplyImpulse( bounce, worldPointOfContact, normal ); proto->SetState( protoState ); } From 727b2acb822429214de3936ab8762b9f7505d5a1 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Tue, 14 Jan 2014 09:02:46 +0100 Subject: [PATCH 03/18] angular collision response fix Incorrect formula found and corrected --- Code/GamePhysics/PhysicsStructs-Impl.h | 4 +++- Code/OysterPhysics3D/OysterPhysics3D.h | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Code/GamePhysics/PhysicsStructs-Impl.h b/Code/GamePhysics/PhysicsStructs-Impl.h index 1bdf07b6..9b13cacf 100644 --- a/Code/GamePhysics/PhysicsStructs-Impl.h +++ b/Code/GamePhysics/PhysicsStructs-Impl.h @@ -147,7 +147,9 @@ namespace Oyster inline ::Oyster::Math::Float4 CustomBodyState::GetLinearMomentum( const ::Oyster::Math::Float4 &at ) const { //return this->linearMomentum + ::Oyster::Physics3D::Formula::TangentialLinearMomentum( this->angularMomentum, at - this->centerPos ); // C3083 error? - return this->linearMomentum + ::Oyster::Math::Float4( this->angularMomentum.xyz.Cross((at - this->centerPos).xyz), 0.0f ); + + ::Oyster::Math::Float4 offset = at - this->centerPos; + return this->linearMomentum + ( ::Oyster::Math::Float4(this->angularMomentum.xyz.Cross(offset.xyz), 0.0f) /= offset.Dot(offset) ); } inline const ::Oyster::Math::Float4 & CustomBodyState::GetAngularMomentum() const diff --git a/Code/OysterPhysics3D/OysterPhysics3D.h b/Code/OysterPhysics3D/OysterPhysics3D.h index f70d228f..84058387 100644 --- a/Code/OysterPhysics3D/OysterPhysics3D.h +++ b/Code/OysterPhysics3D/OysterPhysics3D.h @@ -63,7 +63,7 @@ namespace Oyster { namespace Physics3D ******************************************************************/ inline ::Oyster::Math::Float4 TangentialLinearMomentum( const ::Oyster::Math::Float4 &angularMomentum, const ::Oyster::Math::Float4 &worldOffset ) { - return ::Oyster::Math::Float4( angularMomentum.xyz.Cross(worldOffset.xyz), 0.0f ); + return ::Oyster::Math::Float4( angularMomentum.xyz.Cross(worldOffset.xyz), 0.0f ) /= worldOffset.Dot( worldOffset ); } /****************************************************************** From ee5e5ff8cee7bedb50484fcdcbf73cf9cee0d93b Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Tue, 14 Jan 2014 09:12:37 +0100 Subject: [PATCH 04/18] linear collision response fix moved improved version of Robin's implementation --- .../Implementation/PhysicsAPI_Impl.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index 0d0d31b2..c61f9ce9 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -38,6 +38,23 @@ namespace Float protoG_Magnitude = protoG.Dot( normal ), deuterG_Magnitude = deuterG.Dot( normal ); + // if they are not relatively moving towards eachother, there is no collision + Float deltaPos = normal.Dot( deuterState.GetCenterPosition() - protoState.GetCenterPosition() ); + if( deltaPos < 0.0f ) + { + if( protoG_Magnitude >= deuterG_Magnitude ) + { + break; + } + } + else if( deltaPos > 0.0f ) + { + if( protoG_Magnitude <= deuterG_Magnitude ) + { + break; + } + } + // bounce Float4 bounceD = normal * -Formula::CollisionResponse::Bounce( deuterState.GetRestitutionCoeff(), deuterState.GetMass(), deuterG_Magnitude, From 2314935f48f0061e0e56647cec4d90918ac79376 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Tue, 14 Jan 2014 09:14:17 +0100 Subject: [PATCH 05/18] linear coll. resp. fix add fix didn't cover all cases. Now it do --- Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index c61f9ce9..0d1eaf5d 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -54,6 +54,10 @@ namespace break; } } + else + { + break; + } // bounce Float4 bounceD = normal * -Formula::CollisionResponse::Bounce( deuterState.GetRestitutionCoeff(), From f2c6cc53066380210b5a87f98d93291445cc68ed Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Tue, 14 Jan 2014 09:16:54 +0100 Subject: [PATCH 06/18] Revert "Work asssignment split line" This reverts commit 8ecd2633180d909b47a149e591a8ffa9bc835a49. --- Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index 0d1eaf5d..1115e7e9 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -94,9 +94,6 @@ namespace } protoState.ApplyForwarding( forwardedDeltaPos, forwardedDeltaAxis ); - - /////// Dan below / Robin Above - protoState.ApplyImpulse( bounce, worldPointOfContact, normal ); proto->SetState( protoState ); } From 34dc27655baa3dbe32352cfb6694f192fba6583a Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Tue, 14 Jan 2014 10:34:22 +0100 Subject: [PATCH 07/18] Tangiential formula fixes --- Code/GamePhysics/PhysicsStructs-Impl.h | 20 ++++++++------------ Code/OysterPhysics3D/OysterPhysics3D.h | 8 ++++---- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/Code/GamePhysics/PhysicsStructs-Impl.h b/Code/GamePhysics/PhysicsStructs-Impl.h index 9b13cacf..c74c6537 100644 --- a/Code/GamePhysics/PhysicsStructs-Impl.h +++ b/Code/GamePhysics/PhysicsStructs-Impl.h @@ -2,6 +2,7 @@ #define PHYSICS_STRUCTS_IMPL_H #include "PhysicsStructs.h" +#include "OysterPhysics3D.h" namespace Oyster { @@ -146,10 +147,7 @@ namespace Oyster inline ::Oyster::Math::Float4 CustomBodyState::GetLinearMomentum( const ::Oyster::Math::Float4 &at ) const { - //return this->linearMomentum + ::Oyster::Physics3D::Formula::TangentialLinearMomentum( this->angularMomentum, at - this->centerPos ); // C3083 error? - - ::Oyster::Math::Float4 offset = at - this->centerPos; - return this->linearMomentum + ( ::Oyster::Math::Float4(this->angularMomentum.xyz.Cross(offset.xyz), 0.0f) /= offset.Dot(offset) ); + return this->linearMomentum + ::Oyster::Physics3D::Formula::TangentialLinearMomentum( this->angularMomentum, at - this->centerPos ); } inline const ::Oyster::Math::Float4 & CustomBodyState::GetAngularMomentum() const @@ -214,11 +212,9 @@ namespace Oyster if( tensor.GetDeterminant() != 0.0f ) { // sanity block! ::Oyster::Math::Float4x4 rotation = ::Oyster::Math3D::RotationMatrix(this->angularAxis.xyz); - //::Oyster::Math::Float4 w = ::Oyster::Physics3D::Formula::AngularVelocity( (rotation * this->inertiaTensor).GetInverse(), this->angularMomentum ); // C3083 error? - ::Oyster::Math::Float4 w = (rotation * this->inertiaTensor).GetInverse() * this->angularMomentum; + ::Oyster::Math::Float4 w = ::Oyster::Physics3D::Formula::AngularVelocity( (rotation * this->inertiaTensor).GetInverse(), this->angularMomentum ); this->inertiaTensor = tensor; - //this->angularMomentum = ::Oyster::Physics3D::Formula::AngularMomentum( rotation * tensor, w ); // C3083 error? - this->angularMomentum = rotation * tensor * w; + this->angularMomentum = ::Oyster::Physics3D::Formula::AngularMomentum( rotation * tensor, w ); } } @@ -307,10 +303,10 @@ namespace Oyster inline void CustomBodyState::ApplyImpulse( const ::Oyster::Math::Float4 &j, const ::Oyster::Math::Float4 &at, const ::Oyster::Math::Float4 &normal ) { - //::Oyster::Math::Float4 tangentialImpulse = ::Oyster::Physics3D::Formula::AngularMomentum( j, at - this->centerPos ); // C3083 error? - ::Oyster::Math::Float4 tangentialImpulse = ::Oyster::Math::Float4( (at - this->centerPos).xyz.Cross(j.xyz), 0.0f ); - this->linearImpulse += j - tangentialImpulse; - this->angularImpulse += tangentialImpulse; + ::Oyster::Math::Float4 offset = at - this->centerPos; + ::Oyster::Math::Float4 deltaAngularImpulse = ::Oyster::Physics3D::Formula::AngularMomentum( j, offset ); + this->linearImpulse += j - ::Oyster::Physics3D::Formula::TangentialLinearMomentum( deltaAngularImpulse, offset ); + this->angularImpulse += deltaAngularImpulse; this->isDisturbed = true; } diff --git a/Code/OysterPhysics3D/OysterPhysics3D.h b/Code/OysterPhysics3D/OysterPhysics3D.h index 84058387..970d739e 100644 --- a/Code/OysterPhysics3D/OysterPhysics3D.h +++ b/Code/OysterPhysics3D/OysterPhysics3D.h @@ -126,7 +126,7 @@ namespace Oyster { namespace Physics3D ******************************************************************/ inline ::Oyster::Math::Float3 TangentialLinearMomentum( const ::Oyster::Math::Float3 &angularMomentum, const ::Oyster::Math::Float3 &worldOffset ) { - return angularMomentum.Cross( worldOffset ); + return angularMomentum.Cross( worldOffset ) /= worldOffset.Dot( worldOffset ); } /****************************************************************** @@ -135,7 +135,7 @@ namespace Oyster { namespace Physics3D ******************************************************************/ inline ::Oyster::Math::Float3 TangentialLinearMomentum( const ::Oyster::Math::Float4x4 &momentOfInertia, const ::Oyster::Math::Float3 &angularVelocity, const ::Oyster::Math::Float3 &worldOffset ) { - return TangentialLinearMomentum( AngularMomentum(momentOfInertia, angularVelocity), worldOffset ); + return TangentialLinearMomentum( AngularMomentum(momentOfInertia, angularVelocity), worldOffset ) /= worldOffset.Dot( worldOffset ); } /****************************************************************** @@ -144,7 +144,7 @@ namespace Oyster { namespace Physics3D ******************************************************************/ inline ::Oyster::Math::Float3 TangentialImpulseForce( const ::Oyster::Math::Float3 &impulseTorque, const ::Oyster::Math::Float3 &worldOffset ) { - return impulseTorque.Cross( worldOffset ); + return impulseTorque.Cross( worldOffset ) /= worldOffset.Dot( worldOffset ); } /****************************************************************** @@ -207,7 +207,7 @@ namespace Oyster { namespace Physics3D ******************************************************************/ inline ::Oyster::Math::Float3 TangentialLinearVelocity( const ::Oyster::Math::Float3 &angularVelocity, const ::Oyster::Math::Float3 &worldOffset ) { - return angularVelocity.Cross( worldOffset ); + return angularVelocity.Cross( worldOffset ) /= worldOffset.Dot( worldOffset ); } /****************************************************************** From 461d15303626cd7b87fc969f60c66d5bb34cb931 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Tue, 14 Jan 2014 11:32:02 +0100 Subject: [PATCH 08/18] Inertia tensor fix should produce radian anglevelocities --- Code/OysterPhysics3D/OysterPhysics3D.h | 28 +++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Code/OysterPhysics3D/OysterPhysics3D.h b/Code/OysterPhysics3D/OysterPhysics3D.h index 970d739e..16d0e158 100644 --- a/Code/OysterPhysics3D/OysterPhysics3D.h +++ b/Code/OysterPhysics3D/OysterPhysics3D.h @@ -278,7 +278,7 @@ namespace Oyster { namespace Physics3D /** @todo TODO: add MomentOfInertia tensor formulas */ inline ::Oyster::Math::Float CalculateSphere( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius ) { - return (2.0f/5.0f)*mass*radius*radius; + return ::Utility::Value::Radian( 2.0f / 5.0f ) * mass * radius * radius; } inline ::Oyster::Math::Float4x4 Sphere( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius ) @@ -293,7 +293,7 @@ namespace Oyster { namespace Physics3D inline ::Oyster::Math::Float CalculateHollowSphere( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius ) { - return (2.0f/3.0f)*mass*radius*radius; + return ::Utility::Value::Radian( 2.0f / 3.0f ) * mass * radius * radius; } inline ::Oyster::Math::Float4x4 HollowSphere( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius ) @@ -308,38 +308,38 @@ namespace Oyster { namespace Physics3D inline ::Oyster::Math::Float CalculateCuboidX( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float height, const ::Oyster::Math::Float depth ) { - return (1.0f/12.0f)*mass*(height*height + depth*depth); + return ::Utility::Value::Radian( 1.0f / 12.0f ) * mass * (height * height + depth * depth); } inline ::Oyster::Math::Float CalculateCuboidY( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float width, const ::Oyster::Math::Float depth ) { - return (1.0f/12.0f)*mass*(width*width + depth*depth); + return ::Utility::Value::Radian( 1.0f / 12.0f ) * mass * (width * width + depth * depth ); } inline ::Oyster::Math::Float CalculateCuboidZ( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float width, const ::Oyster::Math::Float height ) { - return (1.0f/12.0f)*mass*(height*height + width*width); + return ::Utility::Value::Radian( 1.0f / 12.0f ) * mass * (height * height + width * width ); } inline ::Oyster::Math::Float4x4 Cuboid( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float height, const ::Oyster::Math::Float width, const ::Oyster::Math::Float depth ) { ::Oyster::Math::Float4x4 inertia = ::Oyster::Math::Float4x4::identity; - inertia.m[0][0] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateCuboidX( mass , height, depth ); - inertia.m[1][1] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateCuboidY( mass , width, depth ); - inertia.m[2][2] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateCuboidZ( mass , height, width ); + inertia.m[0][0] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateCuboidX( mass, height, depth ); + inertia.m[1][1] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateCuboidY( mass, width, depth ); + inertia.m[2][2] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateCuboidZ( mass, height, width ); return inertia; } inline ::Oyster::Math::Float CalculateRodCenter( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float length ) { - return (1.0f/12.0f)*mass*(length*length); + return ::Utility::Value::Radian( 1.0f / 12.0f ) * mass * length * length; } inline ::Oyster::Math::Float4x4 RodCenter( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float length ) { ::Oyster::Math::Float4x4 inertia = ::Oyster::Math::Float4x4::identity; - inertia.m[0][0] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateRodCenter( mass , length ); + inertia.m[0][0] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateRodCenter( mass, length ); inertia.m[1][1] = inertia.m[0][0]; inertia.m[2][2] = inertia.m[0][0]; @@ -348,20 +348,20 @@ namespace Oyster { namespace Physics3D inline ::Oyster::Math::Float CalculateCylinderXY( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float height, const ::Oyster::Math::Float radius ) { - return (1.0f/12.0f)*mass*(3.0f*radius*radius + height*height); + return ::Utility::Value::Radian( 1.0f / 12.0f ) * mass * ( 3.0f * radius * radius + height * height ); } inline ::Oyster::Math::Float CalculateCylinderZ( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius ) { - return 0.5f*mass*(radius*radius); + return ::Utility::Value::Radian( 0.5f ) * mass * ( radius * radius ); } inline ::Oyster::Math::Float4x4 Cylinder( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float height, const ::Oyster::Math::Float radius ) { ::Oyster::Math::Float4x4 inertia = ::Oyster::Math::Float4x4::identity; - inertia.m[0][0] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateCylinderXY( mass , height, radius ); + inertia.m[0][0] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateCylinderXY( mass, height, radius ); inertia.m[1][1] = inertia.m[0][0]; - inertia.m[2][2] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateCylinderZ( mass , radius ); + inertia.m[2][2] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateCylinderZ( mass, radius ); return inertia; } From a41d53b911224a676e937448b31698919e2d6eb1 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Tue, 14 Jan 2014 11:40:47 +0100 Subject: [PATCH 09/18] Revert "Inertia tensor fix" This reverts commit 23dab9dea6627e11aca6e96c73f65569e92ecb83. --- Code/OysterPhysics3D/OysterPhysics3D.h | 28 +++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Code/OysterPhysics3D/OysterPhysics3D.h b/Code/OysterPhysics3D/OysterPhysics3D.h index 16d0e158..970d739e 100644 --- a/Code/OysterPhysics3D/OysterPhysics3D.h +++ b/Code/OysterPhysics3D/OysterPhysics3D.h @@ -278,7 +278,7 @@ namespace Oyster { namespace Physics3D /** @todo TODO: add MomentOfInertia tensor formulas */ inline ::Oyster::Math::Float CalculateSphere( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius ) { - return ::Utility::Value::Radian( 2.0f / 5.0f ) * mass * radius * radius; + return (2.0f/5.0f)*mass*radius*radius; } inline ::Oyster::Math::Float4x4 Sphere( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius ) @@ -293,7 +293,7 @@ namespace Oyster { namespace Physics3D inline ::Oyster::Math::Float CalculateHollowSphere( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius ) { - return ::Utility::Value::Radian( 2.0f / 3.0f ) * mass * radius * radius; + return (2.0f/3.0f)*mass*radius*radius; } inline ::Oyster::Math::Float4x4 HollowSphere( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius ) @@ -308,38 +308,38 @@ namespace Oyster { namespace Physics3D inline ::Oyster::Math::Float CalculateCuboidX( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float height, const ::Oyster::Math::Float depth ) { - return ::Utility::Value::Radian( 1.0f / 12.0f ) * mass * (height * height + depth * depth); + return (1.0f/12.0f)*mass*(height*height + depth*depth); } inline ::Oyster::Math::Float CalculateCuboidY( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float width, const ::Oyster::Math::Float depth ) { - return ::Utility::Value::Radian( 1.0f / 12.0f ) * mass * (width * width + depth * depth ); + return (1.0f/12.0f)*mass*(width*width + depth*depth); } inline ::Oyster::Math::Float CalculateCuboidZ( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float width, const ::Oyster::Math::Float height ) { - return ::Utility::Value::Radian( 1.0f / 12.0f ) * mass * (height * height + width * width ); + return (1.0f/12.0f)*mass*(height*height + width*width); } inline ::Oyster::Math::Float4x4 Cuboid( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float height, const ::Oyster::Math::Float width, const ::Oyster::Math::Float depth ) { ::Oyster::Math::Float4x4 inertia = ::Oyster::Math::Float4x4::identity; - inertia.m[0][0] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateCuboidX( mass, height, depth ); - inertia.m[1][1] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateCuboidY( mass, width, depth ); - inertia.m[2][2] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateCuboidZ( mass, height, width ); + inertia.m[0][0] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateCuboidX( mass , height, depth ); + inertia.m[1][1] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateCuboidY( mass , width, depth ); + inertia.m[2][2] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateCuboidZ( mass , height, width ); return inertia; } inline ::Oyster::Math::Float CalculateRodCenter( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float length ) { - return ::Utility::Value::Radian( 1.0f / 12.0f ) * mass * length * length; + return (1.0f/12.0f)*mass*(length*length); } inline ::Oyster::Math::Float4x4 RodCenter( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float length ) { ::Oyster::Math::Float4x4 inertia = ::Oyster::Math::Float4x4::identity; - inertia.m[0][0] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateRodCenter( mass, length ); + inertia.m[0][0] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateRodCenter( mass , length ); inertia.m[1][1] = inertia.m[0][0]; inertia.m[2][2] = inertia.m[0][0]; @@ -348,20 +348,20 @@ namespace Oyster { namespace Physics3D inline ::Oyster::Math::Float CalculateCylinderXY( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float height, const ::Oyster::Math::Float radius ) { - return ::Utility::Value::Radian( 1.0f / 12.0f ) * mass * ( 3.0f * radius * radius + height * height ); + return (1.0f/12.0f)*mass*(3.0f*radius*radius + height*height); } inline ::Oyster::Math::Float CalculateCylinderZ( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius ) { - return ::Utility::Value::Radian( 0.5f ) * mass * ( radius * radius ); + return 0.5f*mass*(radius*radius); } inline ::Oyster::Math::Float4x4 Cylinder( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float height, const ::Oyster::Math::Float radius ) { ::Oyster::Math::Float4x4 inertia = ::Oyster::Math::Float4x4::identity; - inertia.m[0][0] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateCylinderXY( mass, height, radius ); + inertia.m[0][0] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateCylinderXY( mass , height, radius ); inertia.m[1][1] = inertia.m[0][0]; - inertia.m[2][2] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateCylinderZ( mass, radius ); + inertia.m[2][2] = ::Oyster::Physics3D::Formula::MomentOfInertia::CalculateCylinderZ( mass , radius ); return inertia; } From 2dfe312e04208ed03144d155bbc6f80905a8d9ef Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Tue, 14 Jan 2014 11:42:54 +0100 Subject: [PATCH 10/18] AngularVelocity fix --- Code/OysterPhysics3D/RigidBody.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Code/OysterPhysics3D/RigidBody.cpp b/Code/OysterPhysics3D/RigidBody.cpp index 3ae48511..6fef6311 100644 --- a/Code/OysterPhysics3D/RigidBody.cpp +++ b/Code/OysterPhysics3D/RigidBody.cpp @@ -58,7 +58,7 @@ void RigidBody::Update_LeapFrog( Float updateFrameLength ) Float4x4 wMomentOfInertiaTensor = TransformMatrix( rotationMatrix, this->momentOfInertiaTensor ); // RI // dO = dt * Formula::AngularVelocity( (RI)^-1, avg_H ) = dt * (RI)^-1 * avg_H - this->axis += Formula::AngularVelocity( wMomentOfInertiaTensor.GetInverse(), AverageWithDelta(this->momentum_Angular, this->impulse_Angular) ); + this->axis += Radian( Formula::AngularVelocity(wMomentOfInertiaTensor.GetInverse(), AverageWithDelta(this->momentum_Angular, this->impulse_Angular)) ); this->rotation = Rotation( this->axis ); // update momentums and clear impulse_Linear and impulse_Angular From acb344ca44b6821be031aec484e8e88291c6de97 Mon Sep 17 00:00:00 2001 From: Robin Engman Date: Tue, 14 Jan 2014 11:58:53 +0100 Subject: [PATCH 11/18] Added missing mass and inertia --- Code/GamePhysics/Implementation/SimpleRigidBody.cpp | 2 ++ Code/GamePhysics/Implementation/SphericalRigidBody.cpp | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp index 09855984..49fa292f 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp @@ -113,6 +113,8 @@ void SimpleRigidBody::SetState( const SimpleRigidBody::State &state ) this->rigid.restitutionCoeff = state.GetRestitutionCoeff(); this->rigid.frictionCoeff_Static = state.GetFrictionCoeff_Static(); this->rigid.frictionCoeff_Kinetic = state.GetFrictionCoeff_Kinetic(); + this->rigid.SetMass_KeepMomentum( state.GetMass() ); + this->rigid.SetMomentOfInertia_KeepMomentum( state.GetMomentOfInertia() ); if( state.IsForwarded() ) { diff --git a/Code/GamePhysics/Implementation/SphericalRigidBody.cpp b/Code/GamePhysics/Implementation/SphericalRigidBody.cpp index 95901763..2d96412c 100644 --- a/Code/GamePhysics/Implementation/SphericalRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SphericalRigidBody.cpp @@ -83,6 +83,8 @@ void SphericalRigidBody::SetState( const SphericalRigidBody::State &state ) this->rigid.restitutionCoeff = state.GetRestitutionCoeff(); this->rigid.frictionCoeff_Static = state.GetFrictionCoeff_Static(); this->rigid.frictionCoeff_Kinetic = state.GetFrictionCoeff_Kinetic(); + this->rigid.SetMass_KeepMomentum( state.GetMass() ); + this->rigid.SetMomentOfInertia_KeepMomentum( state.GetMomentOfInertia() ); if( state.IsForwarded() ) { From 0f3c92cd52765f52e8632831a6bfafec0305d71e Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Wed, 15 Jan 2014 09:06:57 +0100 Subject: [PATCH 12/18] Obsolete code removed More commentated out than deleted actually --- .../GamePhysics/Implementation/PhysicsAPI_Impl.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index 1115e7e9..b3abf32c 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -86,14 +86,14 @@ namespace //sumJ += ( 1 / deuterState.GetMass() )*frictionImpulse; // FRICTION END - Float4 forwardedDeltaPos, forwardedDeltaAxis; - { // @todo TODO: is this right? - Float4 bounceAngularImpulse = ::Oyster::Math::Float4( (worldPointOfContact - protoState.GetCenterPosition()).xyz.Cross(bounce.xyz), 0.0f ), - bounceLinearImpulse = bounce - bounceAngularImpulse; - proto->Predict( forwardedDeltaPos, forwardedDeltaAxis, bounceLinearImpulse, bounceAngularImpulse, API_instance.GetFrameTimeLength() ); - } +// Float4 forwardedDeltaPos, forwardedDeltaAxis; +// { // @todo TODO: is this right? +// Float4 bounceAngularImpulse = ::Oyster::Math::Float4( (worldPointOfContact - protoState.GetCenterPosition()).xyz.Cross(bounce.xyz), 0.0f ), +// bounceLinearImpulse = bounce - bounceAngularImpulse; +// proto->Predict( forwardedDeltaPos, forwardedDeltaAxis, bounceLinearImpulse, bounceAngularImpulse, API_instance.GetFrameTimeLength() ); +// } - protoState.ApplyForwarding( forwardedDeltaPos, forwardedDeltaAxis ); +// protoState.ApplyForwarding( forwardedDeltaPos, forwardedDeltaAxis ); protoState.ApplyImpulse( bounce, worldPointOfContact, normal ); proto->SetState( protoState ); } From 337c92a5b8aa6bec29ef51855ee90ad640ddfb71 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Wed, 15 Jan 2014 10:44:20 +0100 Subject: [PATCH 13/18] Added Equal operators to Gravity struct == & != --- Code/GamePhysics/PhysicsStructs-Impl.h | 82 ++++++++++++++++++++++++++ Code/GamePhysics/PhysicsStructs.h | 16 ++++- 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/Code/GamePhysics/PhysicsStructs-Impl.h b/Code/GamePhysics/PhysicsStructs-Impl.h index c74c6537..17c18ba2 100644 --- a/Code/GamePhysics/PhysicsStructs-Impl.h +++ b/Code/GamePhysics/PhysicsStructs-Impl.h @@ -353,6 +353,26 @@ namespace Oyster return *this; } + inline bool GravityWell::operator == ( const GravityWell &gravity ) const + { + if( this->position == gravity.position ) + if( this->mass == gravity.mass ) + { + return true; + } + return false; + } + + inline bool GravityWell::operator != ( const GravityWell &gravity ) const + { + if( this->position == gravity.position ) + if( this->mass == gravity.mass ) + { + return false; + } + return true; + } + inline GravityDirected::GravityDirected( ) { this->impulse = ::Oyster::Math::Float3::null; @@ -370,6 +390,16 @@ namespace Oyster return *this; } + inline bool GravityDirected::operator == ( const GravityDirected &gravity ) const + { + return this->impulse == gravity.impulse; + } + + inline bool GravityDirected::operator != ( const GravityDirected &gravity ) const + { + return this->impulse != gravity.impulse; + } + inline GravityDirectedField::GravityDirectedField( ) { this->normalizedDirection = ::Oyster::Math::Float3::null; @@ -393,6 +423,28 @@ namespace Oyster return *this; } + inline bool GravityDirectedField::operator == ( const GravityDirectedField &gravity ) const + { + if( this->normalizedDirection == gravity.normalizedDirection ) + if( this->mass == gravity.mass ) + if( this->magnitude == gravity.magnitude ) + { + return true; + } + return false; + } + + inline bool GravityDirectedField::operator != ( const GravityDirectedField &gravity ) const + { + if( this->normalizedDirection == gravity.normalizedDirection ) + if( this->mass == gravity.mass ) + if( this->magnitude == gravity.magnitude ) + { + return false; + } + return true; + } + inline Gravity::Gravity() { this->gravityType = GravityType_Undefined; @@ -437,6 +489,36 @@ namespace Oyster return *this; } + + inline bool Gravity::operator == ( const Gravity &gravity ) const + { + if( this->gravityType == gravity.gravityType ) + { + switch( this->gravityType ) + { + case GravityType_Well: return this->well == gravity.well; + case GravityType_Directed: return this->directed == gravity.directed; + case GravityType_DirectedField: return this->directedField == gravity.directedField; + default: return true; + } + } + return false; + } + + inline bool Gravity::operator != ( const Gravity &gravity ) const + { + if( this->gravityType == gravity.gravityType ) + { + switch( this->gravityType ) + { + case GravityType_Well: return this->well != gravity.well; + case GravityType_Directed: return this->directed != gravity.directed; + case GravityType_DirectedField: return this->directedField != gravity.directedField; + default: return false; + } + } + return true; + } } } } diff --git a/Code/GamePhysics/PhysicsStructs.h b/Code/GamePhysics/PhysicsStructs.h index bf6c0618..7ed37c59 100644 --- a/Code/GamePhysics/PhysicsStructs.h +++ b/Code/GamePhysics/PhysicsStructs.h @@ -123,7 +123,10 @@ namespace Oyster { namespace Physics GravityWell( ); GravityWell( const GravityWell &gravityWell ); - GravityWell& operator=( const GravityWell &gravityWell ); + GravityWell & operator = ( const GravityWell &gravityWell ); + + bool operator == ( const GravityWell &gravity ) const; + bool operator != ( const GravityWell &gravity ) const; }; struct GravityDirected @@ -133,6 +136,9 @@ namespace Oyster { namespace Physics GravityDirected( ); GravityDirected( const GravityDirected &gravityDirected ); GravityDirected & operator = ( const GravityDirected &gravityDirected ); + + bool operator == ( const GravityDirected &gravity ) const; + bool operator != ( const GravityDirected &gravity ) const; }; struct GravityDirectedField @@ -143,7 +149,10 @@ namespace Oyster { namespace Physics GravityDirectedField( ); GravityDirectedField( const GravityDirectedField &gravityDirectedField ); - GravityDirectedField & operator=( const GravityDirectedField &gravityDirectedField ); + GravityDirectedField & operator = ( const GravityDirectedField &gravityDirectedField ); + + bool operator == ( const GravityDirectedField &gravity ) const; + bool operator != ( const GravityDirectedField &gravity ) const; }; struct Gravity @@ -177,6 +186,9 @@ namespace Oyster { namespace Physics Gravity( ); Gravity( const Gravity &gravity ); Gravity & operator = ( const Gravity &gravity ); + + bool operator == ( const Gravity &gravity ) const; + bool operator != ( const Gravity &gravity ) const; }; } } } From ff4a39cdace641b9d0ca2a6f3435f7550b5bcb03 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Wed, 15 Jan 2014 10:44:31 +0100 Subject: [PATCH 14/18] Gravity implemented --- .../Implementation/PhysicsAPI_Impl.cpp | 55 +++++++++++++++++-- .../Implementation/PhysicsAPI_Impl.h | 4 ++ Code/GamePhysics/PhysicsAPI.h | 12 ++++ Code/OysterPhysics3D/OysterPhysics3D.h | 16 ++++++ 4 files changed, 83 insertions(+), 4 deletions(-) diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index b3abf32c..ae190608 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -3,6 +3,7 @@ #include "SimpleRigidBody.h" #include "SphericalRigidBody.h" +using namespace ::Oyster; using namespace ::Oyster::Physics; using namespace ::Oyster::Math; using namespace ::Oyster::Collision3D; @@ -113,6 +114,7 @@ API_Impl::API_Impl() this->gravityConstant = Constant::gravity_constant; this->updateFrameLength = 1.0f / 120.0f; this->destructionAction = Default::EventAction_Destruction; + this->gravity = ::std::vector(); this->worldScene = Octree(); } @@ -121,6 +123,8 @@ API_Impl::~API_Impl() {} void API_Impl::Init( unsigned int numObjects, unsigned int numGravityWells , const Float3 &worldSize ) { unsigned char numLayers = 4; //!< @todo TODO: calc numLayers from worldSize + this->gravity.resize( 0 ); + this->gravity.reserve( numGravityWells ); this->worldScene = Octree( numObjects, numLayers, worldSize ); } @@ -153,14 +157,39 @@ float API_Impl::GetFrameTimeLength() const void API_Impl::Update() { /** @todo TODO: Update is a temporary solution .*/ - - - ::std::vector updateList; + ICustomBody::State state; auto proto = this->worldScene.Sample( Universe(), updateList ).begin(); for( ; proto != updateList.end(); ++proto ) { - // Step 1: @todo TODO: Apply Gravity + // Step 1: Apply Gravity + (*proto)->GetState( state ); + for( ::std::vector::size_type i = 0; i < this->gravity.size(); ++i ) + { + switch( this->gravity[i].gravityType ) + { + case Gravity::GravityType_Well: + { + Float4 d = state.GetCenterPosition() - Float4( this->gravity[i].well.position, 1.0f ); + Float rSquared = d.Dot( d ); + if( rSquared != 0.0 ) + { + Float force = Physics3D::Formula::ForceField( this->gravityConstant, state.GetMass(), this->gravity[i].well.mass, rSquared ); + state.ApplyLinearImpulse( (this->updateFrameLength * force / ::std::sqrt(rSquared)) * d ); + } + break; + } + case Gravity::GravityType_Directed: + state.ApplyLinearImpulse( Float4(this->gravity[i].directed.impulse, 0.0f) ); + break; +// case Gravity::GravityType_DirectedField: +// //this->gravity[i].directedField. +// //! TODO: @todo rethink +// break; + default: break; + } + } + (*proto)->SetState( state ); // Step 2: Apply Collision Response this->worldScene.Visit( *proto, OnPossibleCollision ); @@ -213,6 +242,24 @@ void API_Impl::DestroyObject( const ICustomBody* objRef ) } } +void API_Impl::AddGravity( const API::Gravity &g ) +{ + this->gravity.push_back( g ); +} + +void API_Impl::RemoveGravity( const API::Gravity &g ) +{ + for( ::std::vector::size_type i = this->gravity.size() - 1; i >= 0; --i ) + { + if( g == this->gravity[i] ) + { + int end = this->gravity.size() - 1; + this->gravity[i] = this->gravity[end]; + this->gravity.resize( end ); + } + } +} + //void API_Impl::ApplyForceAt( const ICustomBody* objRef, const Float3 &worldPos, const Float3 &worldF ) //{ // unsigned int tempRef = this->worldScene.GetTemporaryReferenceOf( objRef ); diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h index 0c08bf11..314e0657 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h @@ -32,6 +32,9 @@ namespace Oyster ::Utility::DynamicMemory::UniquePointer ExtractObject( const ICustomBody* objRef ); void DestroyObject( const ICustomBody* objRef ); + void AddGravity( const Gravity &g ); + void RemoveGravity( const Gravity &g ); + //void ApplyForceAt( const ICustomBody* objRef, const ::Oyster::Math::Float3 &worldPos, const ::Oyster::Math::Float3 &worldF ); //void SetMomentOfInertiaTensor_KeepVelocity( const ICustomBody* objRef, const ::Oyster::Math::Float4x4 &localI ); @@ -49,6 +52,7 @@ namespace Oyster private: ::Oyster::Math::Float gravityConstant, updateFrameLength; EventAction_Destruction destructionAction; + ::std::vector gravity; Octree worldScene; }; diff --git a/Code/GamePhysics/PhysicsAPI.h b/Code/GamePhysics/PhysicsAPI.h index 0904551d..019a4af7 100644 --- a/Code/GamePhysics/PhysicsAPI.h +++ b/Code/GamePhysics/PhysicsAPI.h @@ -22,6 +22,7 @@ namespace Oyster struct SimpleBodyDescription; struct SphericalBodyDescription; struct CustomBodyState; + struct Gravity; } enum UpdateState @@ -40,6 +41,7 @@ namespace Oyster public: typedef Struct::SimpleBodyDescription SimpleBodyDescription; typedef Struct::SphericalBodyDescription SphericalBodyDescription; + typedef Struct::Gravity Gravity; typedef void (*EventAction_Destruction)( ::Utility::DynamicMemory::UniquePointer proto ); @@ -124,6 +126,16 @@ namespace Oyster ********************************************************/ virtual void DestroyObject( const ICustomBody* objRef ) = 0; + /******************************************************** + * TODO: @todo doc + ********************************************************/ + virtual void AddGravity( const Gravity &g ) = 0; + + /******************************************************** + * TODO: @todo doc + ********************************************************/ + virtual void RemoveGravity( const Gravity &g ) = 0; + ///******************************************************** // * Apply force on an object. // * @param objRef: A pointer to the ICustomBody representing a physical object. diff --git a/Code/OysterPhysics3D/OysterPhysics3D.h b/Code/OysterPhysics3D/OysterPhysics3D.h index 970d739e..f814ff46 100644 --- a/Code/OysterPhysics3D/OysterPhysics3D.h +++ b/Code/OysterPhysics3D/OysterPhysics3D.h @@ -273,6 +273,22 @@ namespace Oyster { namespace Physics3D return momentOfInertia * angularImpulseAcceleration; } + /****************************************************************** + * @todo TODO: doc + ******************************************************************/ + inline ::Oyster::Math::Float ForceField( ::Oyster::Math::Float g, ::Oyster::Math::Float massA, ::Oyster::Math::Float massB, ::Oyster::Math::Float radiusSquared ) + { + return g * massA * massB / radiusSquared; + } + + /****************************************************************** + * @todo TODO: doc + ******************************************************************/ + inline ::Oyster::Math::Float ForceField( ::Oyster::Math::Float g, ::Oyster::Math::Float massA, ::Oyster::Math::Float massB, const ::Oyster::Math::Float4 &deltaPos ) + { + return g * massA * massB / deltaPos.Dot( deltaPos ); + } + namespace MomentOfInertia { /// Library of Formulas to calculate moment of inerta for simple shapes /** @todo TODO: add MomentOfInertia tensor formulas */ From 80c835be5860b97c2123b9d0474f4ac533b670e4 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Thu, 16 Jan 2014 11:28:07 +0100 Subject: [PATCH 15/18] BoxVsBox pointOfContact fix it was not translated --- Code/OysterPhysics3D/OysterCollision3D.cpp | 31 ++++++++++++---------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/Code/OysterPhysics3D/OysterCollision3D.cpp b/Code/OysterPhysics3D/OysterCollision3D.cpp index 3ef44974..169b5add 100644 --- a/Code/OysterPhysics3D/OysterCollision3D.cpp +++ b/Code/OysterPhysics3D/OysterCollision3D.cpp @@ -204,7 +204,7 @@ namespace Oyster { namespace Collision3D { namespace Utility return true; } - bool SeperatingAxisTest_AxisAlignedVsTransformedBox( const Float4 &boundingOffsetA, const Float4 &boundingOffsetB, const Float4x4 &rotationB, const Float4 &worldOffset, Float4 &worldPointOfContact ) + bool SeperatingAxisTest_AxisAlignedVsTransformedBox( const Float4 &boundingOffsetA, const Float4 &boundingOffsetB, const Float4x4 &rotationB, const Float4 &worldOffset, Float4 &localPointOfContact ) { // by Dan Andersson /***************************************************************** @@ -241,7 +241,7 @@ namespace Oyster { namespace Collision3D { namespace Utility { // no intersection return false; } - worldPointOfContact = s * ( centerSeperation * eA / edgeSeperation ); + localPointOfContact = s * ( centerSeperation * eA / edgeSeperation ); s = Float4::standard_unit_y; centerSeperation = t.Dot(s); @@ -251,7 +251,7 @@ namespace Oyster { namespace Collision3D { namespace Utility { // no intersection return false; } - worldPointOfContact += s * ( centerSeperation * eA / edgeSeperation ); + localPointOfContact += s * ( centerSeperation * eA / edgeSeperation ); s = Float4::standard_unit_z; centerSeperation = t.Dot(s); @@ -261,7 +261,7 @@ namespace Oyster { namespace Collision3D { namespace Utility { // no intersection return false; } - worldPointOfContact += s * ( centerSeperation * eA / edgeSeperation ); + localPointOfContact += s * ( centerSeperation * eA / edgeSeperation ); s = rotationB.v[0]; centerSeperation = t.Dot(s); @@ -271,7 +271,7 @@ namespace Oyster { namespace Collision3D { namespace Utility { // no intersection return false; } - worldPointOfContact += s * ( centerSeperation * eA / edgeSeperation ); + localPointOfContact += s * ( centerSeperation * eA / edgeSeperation ); s = rotationB.v[1]; centerSeperation = t.Dot(s); @@ -281,7 +281,7 @@ namespace Oyster { namespace Collision3D { namespace Utility { // no intersection return false; } - worldPointOfContact += s * ( centerSeperation * eA / edgeSeperation ); + localPointOfContact += s * ( centerSeperation * eA / edgeSeperation ); s = rotationB.v[2]; centerSeperation = t.Dot(s); @@ -291,7 +291,7 @@ namespace Oyster { namespace Collision3D { namespace Utility { // no intersection return false; } - worldPointOfContact += s * ( centerSeperation * eA / edgeSeperation ); // enough point of contact data gathered for approximative result. + localPointOfContact += s * ( centerSeperation * eA / edgeSeperation ); // enough point of contact data gathered for approximative result. s = Float4( Float3::standard_unit_x.Cross(rotationB.v[0].xyz), 0.0f ); centerSeperation = t.Dot(s); @@ -374,7 +374,7 @@ namespace Oyster { namespace Collision3D { namespace Utility return false; } - worldPointOfContact *= 0.5f; + localPointOfContact *= 0.5f; return true; } } @@ -821,10 +821,11 @@ namespace Oyster { namespace Collision3D { namespace Utility Float4 alignedOffsetBoundaries = (boxB.maxVertex - boxB.minVertex) * 0.5f, offset = boxA.center - Average( boxB.maxVertex, boxB.minVertex ); - Float4 pointOfContact; - if( Private::SeperatingAxisTest_AxisAlignedVsTransformedBox( alignedOffsetBoundaries, boxA.boundingOffset, boxA.rotation, offset, pointOfContact ) ) + Float4 localPointOfContact; + if( Private::SeperatingAxisTest_AxisAlignedVsTransformedBox( alignedOffsetBoundaries, boxA.boundingOffset, boxA.rotation, offset, localPointOfContact ) ) { - worldPointOfContact = pointOfContact.xyz; + worldPointOfContact = localPointOfContact + boxA.center; + worldPointOfContact.w = 1.0f; return true; } else return false; @@ -843,10 +844,12 @@ namespace Oyster { namespace Collision3D { namespace Utility Float4x4 rotationB = TransformMatrix( InverseRotationMatrix(boxA.rotation), boxB.rotation ); Float4 posB = boxB.center - boxA.center; - Float4 pointOfContact; - if( Private::SeperatingAxisTest_AxisAlignedVsTransformedBox( boxA.boundingOffset, boxB.boundingOffset, rotationB, posB, pointOfContact ) ) + Float4 localPointOfContact; + if( Private::SeperatingAxisTest_AxisAlignedVsTransformedBox( boxA.boundingOffset, boxB.boundingOffset, rotationB, posB, localPointOfContact ) ) { - worldPointOfContact = TransformVector( boxA.rotation, pointOfContact, pointOfContact ).xyz; + worldPointOfContact = TransformVector( boxA.rotation, localPointOfContact, localPointOfContact ); + worldPointOfContact += boxA.center; + worldPointOfContact.w = 1.0f; return true; } else return false; From a8c87273e20f3672bd77e0eb05a4c48035b4e527 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Fri, 17 Jan 2014 13:30:07 +0100 Subject: [PATCH 16/18] Gravity normal fix Forgot to update that value. Done --- .../Implementation/PhysicsAPI_Impl.cpp | 15 +++++++++++---- Code/GamePhysics/Implementation/PhysicsAPI_Impl.h | 6 +++--- Code/GamePhysics/PhysicsAPI.h | 4 ++-- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index ae190608..9c12f7b0 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -163,7 +163,7 @@ void API_Impl::Update() for( ; proto != updateList.end(); ++proto ) { // Step 1: Apply Gravity - (*proto)->GetState( state ); + Float4 gravityImpulse = Float4::null; for( ::std::vector::size_type i = 0; i < this->gravity.size(); ++i ) { switch( this->gravity[i].gravityType ) @@ -175,12 +175,12 @@ void API_Impl::Update() if( rSquared != 0.0 ) { Float force = Physics3D::Formula::ForceField( this->gravityConstant, state.GetMass(), this->gravity[i].well.mass, rSquared ); - state.ApplyLinearImpulse( (this->updateFrameLength * force / ::std::sqrt(rSquared)) * d ); + gravityImpulse += (this->updateFrameLength * force / ::std::sqrt(rSquared)) * d; } break; } case Gravity::GravityType_Directed: - state.ApplyLinearImpulse( Float4(this->gravity[i].directed.impulse, 0.0f) ); + gravityImpulse += Float4( this->gravity[i].directed.impulse, 0.0f ); break; // case Gravity::GravityType_DirectedField: // //this->gravity[i].directedField. @@ -189,7 +189,14 @@ void API_Impl::Update() default: break; } } - (*proto)->SetState( state ); + + if( gravityImpulse != Float4::null ) + { + (*proto)->GetState( state ); + state.ApplyLinearImpulse( gravityImpulse ); + (*proto)->SetGravityNormal( gravityImpulse.GetNormalized().xyz ); + (*proto)->SetState( state ); + } // Step 2: Apply Collision Response this->worldScene.Visit( *proto, OnPossibleCollision ); diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h index 314e0657..f62c973b 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h @@ -32,8 +32,8 @@ namespace Oyster ::Utility::DynamicMemory::UniquePointer ExtractObject( const ICustomBody* objRef ); void DestroyObject( const ICustomBody* objRef ); - void AddGravity( const Gravity &g ); - void RemoveGravity( const Gravity &g ); + void AddGravity( const API::Gravity &g ); + void RemoveGravity( const API::Gravity &g ); //void ApplyForceAt( const ICustomBody* objRef, const ::Oyster::Math::Float3 &worldPos, const ::Oyster::Math::Float3 &worldF ); @@ -52,7 +52,7 @@ namespace Oyster private: ::Oyster::Math::Float gravityConstant, updateFrameLength; EventAction_Destruction destructionAction; - ::std::vector gravity; + ::std::vector gravity; Octree worldScene; }; diff --git a/Code/GamePhysics/PhysicsAPI.h b/Code/GamePhysics/PhysicsAPI.h index 019a4af7..b42630e6 100644 --- a/Code/GamePhysics/PhysicsAPI.h +++ b/Code/GamePhysics/PhysicsAPI.h @@ -129,12 +129,12 @@ namespace Oyster /******************************************************** * TODO: @todo doc ********************************************************/ - virtual void AddGravity( const Gravity &g ) = 0; + virtual void AddGravity( const API::Gravity &g ) = 0; /******************************************************** * TODO: @todo doc ********************************************************/ - virtual void RemoveGravity( const Gravity &g ) = 0; + virtual void RemoveGravity( const API::Gravity &g ) = 0; ///******************************************************** // * Apply force on an object. From 0b8ecd740ab808e40c9df74f4d99177844121778 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Fri, 17 Jan 2014 16:07:25 +0100 Subject: [PATCH 17/18] Added ICustombody event subscription ICustomBody::EventAction_Move if an object have moved, an event can now be subscribed. External Impact: ICustomBody SimpleBodyDescription SphericalBodyDescription --- .../Implementation/PhysicsAPI_Impl.cpp | 6 ++- .../Implementation/PhysicsAPI_Impl.h | 1 + .../Implementation/SimpleRigidBody.cpp | 43 +++++++++++++++---- .../Implementation/SimpleRigidBody.h | 10 ++++- .../Implementation/SphericalRigidBody.cpp | 43 +++++++++++++++---- .../Implementation/SphericalRigidBody.h | 10 ++++- Code/GamePhysics/PhysicsAPI.h | 15 ++++++- Code/GamePhysics/PhysicsStructs-Impl.h | 6 ++- Code/GamePhysics/PhysicsStructs.h | 6 ++- 9 files changed, 114 insertions(+), 26 deletions(-) diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index 9c12f7b0..ec82c57c 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -22,7 +22,7 @@ namespace Float4 worldPointOfContact; if( proto->Intersects(*deuter, worldPointOfContact) ) { - switch( proto->CallSubscription(proto, deuter) ) + switch( proto->CallSubscription_Collision(deuter) ) { case ICustomBody::SubscriptMessage_ignore_collision_response: break; @@ -209,6 +209,7 @@ void API_Impl::Update() { case UpdateState_altered: this->worldScene.SetAsAltered( this->worldScene.GetTemporaryReferenceOf(*proto) ); + (*proto)->CallSubscription_Move(); case UpdateState_resting: default: break; } @@ -374,5 +375,8 @@ namespace Oyster { namespace Physics { /* Do nothing except returning business as usual. */ return ::Oyster::Physics::ICustomBody::SubscriptMessage_none; } + + void EventAction_Move( const ::Oyster::Physics::ICustomBody *object ) + { /* Do nothing. */ } } } } \ No newline at end of file diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h index f62c973b..8b38004c 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h @@ -60,6 +60,7 @@ namespace Oyster { void EventAction_Destruction( ::Utility::DynamicMemory::UniquePointer<::Oyster::Physics::ICustomBody> proto ); ::Oyster::Physics::ICustomBody::SubscriptMessage EventAction_Collision( const ::Oyster::Physics::ICustomBody *proto, const ::Oyster::Physics::ICustomBody *deuter ); + void EventAction_Move( const ::Oyster::Physics::ICustomBody *object ); } } } diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp index 49fa292f..b506f032 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp @@ -46,7 +46,8 @@ SimpleRigidBody::SimpleRigidBody() this->rigid = RigidBody(); this->rigid.SetMass_KeepMomentum( 16.0f ); this->gravityNormal = Float3::null; - this->collisionAction = Default::EventAction_Collision; + this->onCollision = Default::EventAction_Collision; + this->onMovement = Default::EventAction_Move; this->ignoreGravity = this->isForwarded = false; this->scene = nullptr; } @@ -63,13 +64,22 @@ SimpleRigidBody::SimpleRigidBody( const API::SimpleBodyDescription &desc ) this->gravityNormal = Float3::null; - if( desc.subscription ) + if( desc.subscription_onCollision ) { - this->collisionAction = desc.subscription; + this->onCollision = desc.subscription_onCollision; } else { - this->collisionAction = Default::EventAction_Collision; + this->onCollision = Default::EventAction_Collision; + } + + if( desc.subscription_onMovement ) + { + this->onMovement= desc.subscription_onMovement; + } + else + { + this->onMovement = Default::EventAction_Move; } this->ignoreGravity = desc.ignoreGravity; @@ -138,9 +148,14 @@ void SimpleRigidBody::SetState( const SimpleRigidBody::State &state ) } } -ICustomBody::SubscriptMessage SimpleRigidBody::CallSubscription( const ICustomBody *proto, const ICustomBody *deuter ) +ICustomBody::SubscriptMessage SimpleRigidBody::CallSubscription_Collision( const ICustomBody *deuter ) { - return this->collisionAction( proto, deuter ); + return this->onCollision( this, deuter ); +} + +void SimpleRigidBody::CallSubscription_Move() +{ + this->onMovement( this ); } bool SimpleRigidBody::IsAffectedByGravity() const @@ -282,11 +297,23 @@ void SimpleRigidBody::SetSubscription( ICustomBody::EventAction_Collision functi { if( functionPointer ) { - this->collisionAction = functionPointer; + this->onCollision = functionPointer; } else { - this->collisionAction = Default::EventAction_Collision; + this->onCollision = Default::EventAction_Collision; + } +} + +void SimpleRigidBody::SetSubscription( ICustomBody::EventAction_Move functionPointer ) +{ + if( functionPointer ) + { + this->onMovement = functionPointer; + } + else + { + this->onMovement = Default::EventAction_Move; } } diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.h b/Code/GamePhysics/Implementation/SimpleRigidBody.h index 618e0235..7f1c50e6 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.h +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.h @@ -21,7 +21,9 @@ namespace Oyster { namespace Physics void SetState( const State &state ); //::Oyster::Math::Float3 GetRigidLinearVelocity() const; - SubscriptMessage CallSubscription( const ICustomBody *proto, const ICustomBody *deuter ); + SubscriptMessage CallSubscription_Collision( const ICustomBody *deuter ); + void CallSubscription_Move(); + bool IsAffectedByGravity() const; bool Intersects( const ::Oyster::Collision3D::ICollideable &shape ) const; bool Intersects( const ::Oyster::Collision3D::ICollideable &shape, ::Oyster::Math::Float4 &worldPointOfContact ) const; @@ -39,7 +41,10 @@ namespace Oyster { namespace Physics void Predict( ::Oyster::Math::Float4 &outDeltaPos, ::Oyster::Math::Float4 &outDeltaAxis, const ::Oyster::Math::Float4 &actingLinearImpulse, const ::Oyster::Math::Float4 &actingAngularImpulse, ::Oyster::Math::Float deltaTime ); void SetScene( void *scene ); + void SetSubscription( EventAction_Collision functionPointer ); + void SetSubscription( EventAction_Move functionPointer ); + void SetGravity( bool ignore); void SetGravityNormal( const ::Oyster::Math::Float3 &normalizedVector ); //void SetMomentOfInertiaTensor_KeepVelocity( const ::Oyster::Math::Float4x4 &localI ); @@ -56,7 +61,8 @@ namespace Oyster { namespace Physics ::Oyster::Physics3D::RigidBody rigid; ::Oyster::Math::Float4 deltaPos, deltaAxis; ::Oyster::Math::Float3 gravityNormal; - EventAction_Collision collisionAction; + EventAction_Collision onCollision; + EventAction_Move onMovement; Octree *scene; bool ignoreGravity, isForwarded; }; diff --git a/Code/GamePhysics/Implementation/SphericalRigidBody.cpp b/Code/GamePhysics/Implementation/SphericalRigidBody.cpp index 2d96412c..3ce1a6ce 100644 --- a/Code/GamePhysics/Implementation/SphericalRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SphericalRigidBody.cpp @@ -13,7 +13,8 @@ SphericalRigidBody::SphericalRigidBody() this->rigid = RigidBody(); this->rigid.SetMass_KeepMomentum( 10.0f ); this->gravityNormal = Float3::null; - this->collisionAction = Default::EventAction_Collision; + this->onCollision = Default::EventAction_Collision; + this->onMovement = Default::EventAction_Move; this->ignoreGravity = this->isForwarded = false; this->scene = nullptr; this->body = Sphere( Float3::null, 0.5f ); @@ -32,13 +33,22 @@ SphericalRigidBody::SphericalRigidBody( const API::SphericalBodyDescription &des this->gravityNormal = Float3::null; - if( desc.subscription ) + if( desc.subscription_onCollision ) { - this->collisionAction = desc.subscription; + this->onCollision = desc.subscription_onCollision; } else { - this->collisionAction = Default::EventAction_Collision; + this->onCollision = Default::EventAction_Collision; + } + + if( desc.subscription_onMovement ) + { + this->onMovement= desc.subscription_onMovement; + } + else + { + this->onMovement = Default::EventAction_Move; } this->ignoreGravity = desc.ignoreGravity; @@ -108,9 +118,14 @@ void SphericalRigidBody::SetState( const SphericalRigidBody::State &state ) } } -ICustomBody::SubscriptMessage SphericalRigidBody::CallSubscription( const ICustomBody *proto, const ICustomBody *deuter ) +ICustomBody::SubscriptMessage SphericalRigidBody::CallSubscription_Collision( const ICustomBody *deuter ) { - return this->collisionAction( proto, deuter ); + return this->onCollision( this, deuter ); +} + +void SphericalRigidBody::CallSubscription_Move() +{ + this->onMovement( this ); } bool SphericalRigidBody::IsAffectedByGravity() const @@ -207,11 +222,23 @@ void SphericalRigidBody::SetSubscription( ICustomBody::EventAction_Collision fun { if( functionPointer ) { - this->collisionAction = functionPointer; + this->onCollision = functionPointer; } else { - this->collisionAction = Default::EventAction_Collision; + this->onCollision = Default::EventAction_Collision; + } +} + +void SphericalRigidBody::SetSubscription( ICustomBody::EventAction_Move functionPointer ) +{ + if( functionPointer ) + { + this->onMovement = functionPointer; + } + else + { + this->onMovement = Default::EventAction_Move; } } diff --git a/Code/GamePhysics/Implementation/SphericalRigidBody.h b/Code/GamePhysics/Implementation/SphericalRigidBody.h index 51d1b998..74b74535 100644 --- a/Code/GamePhysics/Implementation/SphericalRigidBody.h +++ b/Code/GamePhysics/Implementation/SphericalRigidBody.h @@ -22,7 +22,9 @@ namespace Oyster { namespace Physics void SetState( const State &state ); //::Oyster::Math::Float3 GetRigidLinearVelocity() const; - SubscriptMessage CallSubscription( const ICustomBody *proto, const ICustomBody *deuter ); + SubscriptMessage CallSubscription_Collision( const ICustomBody *deuter ); + void CallSubscription_Move(); + bool IsAffectedByGravity() const; bool Intersects( const ::Oyster::Collision3D::ICollideable &shape ) const; bool Intersects( const ::Oyster::Collision3D::ICollideable &shape, ::Oyster::Math::Float4 &worldPointOfContact ) const; @@ -40,7 +42,10 @@ namespace Oyster { namespace Physics void Predict( ::Oyster::Math::Float4 &outDeltaPos, ::Oyster::Math::Float4 &outDeltaAxis, const ::Oyster::Math::Float4 &actingLinearImpulse, const ::Oyster::Math::Float4 &actingAngularImpulse, ::Oyster::Math::Float deltaTime ); void SetScene( void *scene ); + void SetSubscription( EventAction_Collision functionPointer ); + void SetSubscription( EventAction_Move functionPointer ); + void SetGravity( bool ignore); void SetGravityNormal( const ::Oyster::Math::Float3 &normalizedVector ); //void SetMomentOfInertiaTensor_KeepVelocity( const ::Oyster::Math::Float4x4 &localI ); @@ -57,7 +62,8 @@ namespace Oyster { namespace Physics ::Oyster::Physics3D::RigidBody rigid; ::Oyster::Math::Float4 deltaPos, deltaAxis; ::Oyster::Math::Float3 gravityNormal; - EventAction_Collision collisionAction; + EventAction_Collision onCollision; + EventAction_Move onMovement; bool ignoreGravity, isForwarded; Octree *scene; ::Oyster::Collision3D::Sphere body; diff --git a/Code/GamePhysics/PhysicsAPI.h b/Code/GamePhysics/PhysicsAPI.h index b42630e6..82a139c3 100644 --- a/Code/GamePhysics/PhysicsAPI.h +++ b/Code/GamePhysics/PhysicsAPI.h @@ -235,6 +235,7 @@ namespace Oyster }; typedef SubscriptMessage (*EventAction_Collision)( const ICustomBody *proto, const ICustomBody *deuter ); + typedef void (*EventAction_Move)( const ICustomBody *object ); typedef Struct::SimpleBodyDescription SimpleBodyDescription; typedef Struct::SphericalBodyDescription SphericalBodyDescription; typedef Struct::CustomBodyState State; @@ -250,7 +251,12 @@ namespace Oyster /******************************************************** * @todo TODO: need doc ********************************************************/ - virtual SubscriptMessage CallSubscription( const ICustomBody *proto, const ICustomBody *deuter ) = 0; + virtual SubscriptMessage CallSubscription_Collision( const ICustomBody *deuter ) = 0; + + /******************************************************** + * @todo TODO: need doc + ********************************************************/ + virtual void CallSubscription_Move() = 0; /******************************************************** * @todo TODO: need doc @@ -371,6 +377,13 @@ namespace Oyster ********************************************************/ virtual void SetSubscription( EventAction_Collision functionPointer ) = 0; + /******************************************************** + * Sets the function that will be called by the engine + * whenever an object have moved. + * @param functionPointer: If NULL, an empty default function will be set. + ********************************************************/ + virtual void SetSubscription( EventAction_Move functionPointer ) = 0; + /******************************************************** * @param ignore: True if Engine should not apply Gravity. ********************************************************/ diff --git a/Code/GamePhysics/PhysicsStructs-Impl.h b/Code/GamePhysics/PhysicsStructs-Impl.h index 17c18ba2..2f218095 100644 --- a/Code/GamePhysics/PhysicsStructs-Impl.h +++ b/Code/GamePhysics/PhysicsStructs-Impl.h @@ -17,7 +17,8 @@ namespace Oyster this->size = ::Oyster::Math::Float4( 1.0f ); this->mass = 12.0f; this->inertiaTensor = ::Oyster::Math::Float4x4::identity; - this->subscription = NULL; + this->subscription_onCollision = NULL; + this->subscription_onMovement = NULL; this->ignoreGravity = false; } @@ -27,7 +28,8 @@ namespace Oyster this->centerPosition = ::Oyster::Math::Float4::standard_unit_w; this->radius = 0.5f; this->mass = 10.0f; - this->subscription = NULL; + this->subscription_onCollision = NULL; + this->subscription_onMovement = NULL; this->ignoreGravity = false; } diff --git a/Code/GamePhysics/PhysicsStructs.h b/Code/GamePhysics/PhysicsStructs.h index 7ed37c59..1d3d58dd 100644 --- a/Code/GamePhysics/PhysicsStructs.h +++ b/Code/GamePhysics/PhysicsStructs.h @@ -15,7 +15,8 @@ namespace Oyster { namespace Physics ::Oyster::Math::Float4 size; ::Oyster::Math::Float mass; ::Oyster::Math::Float4x4 inertiaTensor; - ::Oyster::Physics::ICustomBody::EventAction_Collision subscription; + ::Oyster::Physics::ICustomBody::EventAction_Collision subscription_onCollision; + ::Oyster::Physics::ICustomBody::EventAction_Move subscription_onMovement; bool ignoreGravity; SimpleBodyDescription(); @@ -27,7 +28,8 @@ namespace Oyster { namespace Physics ::Oyster::Math::Float4 centerPosition; ::Oyster::Math::Float radius; ::Oyster::Math::Float mass; - ::Oyster::Physics::ICustomBody::EventAction_Collision subscription; + ::Oyster::Physics::ICustomBody::EventAction_Collision subscription_onCollision; + ::Oyster::Physics::ICustomBody::EventAction_Move subscription_onMovement; bool ignoreGravity; SphericalBodyDescription(); From 82d1a0345073e3c461fbd9577e36035f6ff999bf Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Mon, 20 Jan 2014 13:44:12 +0100 Subject: [PATCH 18/18] ICustomBody :: Custom Tag added --- .../Implementation/SimpleRigidBody.cpp | 16 ++++++++++++-- .../Implementation/SimpleRigidBody.h | 3 +++ .../Implementation/SphericalRigidBody.cpp | 21 +++++++++++++------ .../Implementation/SphericalRigidBody.h | 6 ++++-- Code/GamePhysics/PhysicsAPI.h | 13 ++++++++++++ 5 files changed, 49 insertions(+), 10 deletions(-) diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp index b506f032..ad84bdbd 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp @@ -48,8 +48,9 @@ SimpleRigidBody::SimpleRigidBody() this->gravityNormal = Float3::null; this->onCollision = Default::EventAction_Collision; this->onMovement = Default::EventAction_Move; - this->ignoreGravity = this->isForwarded = false; this->scene = nullptr; + this->customTag = nullptr; + this->ignoreGravity = this->isForwarded = false; } SimpleRigidBody::SimpleRigidBody( const API::SimpleBodyDescription &desc ) @@ -82,8 +83,9 @@ SimpleRigidBody::SimpleRigidBody( const API::SimpleBodyDescription &desc ) this->onMovement = Default::EventAction_Move; } - this->ignoreGravity = desc.ignoreGravity; this->scene = nullptr; + this->customTag = nullptr; + this->ignoreGravity = desc.ignoreGravity; } SimpleRigidBody::~SimpleRigidBody() {} @@ -240,6 +242,11 @@ Float3 & SimpleRigidBody::GetGravityNormal( Float3 &targetMem ) const return targetMem = this->gravityNormal; } +void * SimpleRigidBody::GetCustomTag() const +{ + return this->customTag; +} + //Float3 & SimpleRigidBody::GetCenter( Float3 &targetMem ) const //{ // return targetMem = this->rigid.centerPos; @@ -328,6 +335,11 @@ void SimpleRigidBody::SetGravityNormal( const Float3 &normalizedVector ) this->gravityNormal = normalizedVector; } +void SimpleRigidBody::SetCustomTag( void *ref ) +{ + this->customTag = ref; +} + //void SimpleRigidBody::SetMomentOfInertiaTensor_KeepVelocity( const Float4x4 &localI ) //{ // this->rigid.SetMomentOfInertia_KeepVelocity( localI ); diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.h b/Code/GamePhysics/Implementation/SimpleRigidBody.h index 7f1c50e6..cce657da 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.h +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.h @@ -32,6 +32,7 @@ namespace Oyster { namespace Physics ::Oyster::Collision3D::Sphere & GetBoundingSphere( ::Oyster::Collision3D::Sphere &targetMem = ::Oyster::Collision3D::Sphere() ) const; ::Oyster::Math::Float4 & GetNormalAt( const ::Oyster::Math::Float4 &worldPos, ::Oyster::Math::Float4 &targetMem = ::Oyster::Math::Float4() ) const; ::Oyster::Math::Float3 & GetGravityNormal( ::Oyster::Math::Float3 &targetMem = ::Oyster::Math::Float3() ) const; + void * GetCustomTag() const; //::Oyster::Math::Float3 & GetCenter( ::Oyster::Math::Float3 &targetMem = ::Oyster::Math::Float3() ) const; //::Oyster::Math::Float4x4 & GetRotation( ::Oyster::Math::Float4x4 &targetMem = ::Oyster::Math::Float4x4() ) const; //::Oyster::Math::Float4x4 & GetOrientation( ::Oyster::Math::Float4x4 &targetMem = ::Oyster::Math::Float4x4() ) const; @@ -47,6 +48,7 @@ namespace Oyster { namespace Physics void SetGravity( bool ignore); void SetGravityNormal( const ::Oyster::Math::Float3 &normalizedVector ); + void SetCustomTag( void *ref ); //void SetMomentOfInertiaTensor_KeepVelocity( const ::Oyster::Math::Float4x4 &localI ); //void SetMomentOfInertiaTensor_KeepMomentum( const ::Oyster::Math::Float4x4 &localI ); //void SetMass_KeepVelocity( ::Oyster::Math::Float m ); @@ -64,6 +66,7 @@ namespace Oyster { namespace Physics EventAction_Collision onCollision; EventAction_Move onMovement; Octree *scene; + void *customTag; bool ignoreGravity, isForwarded; }; } } diff --git a/Code/GamePhysics/Implementation/SphericalRigidBody.cpp b/Code/GamePhysics/Implementation/SphericalRigidBody.cpp index 3ce1a6ce..7a8f12a9 100644 --- a/Code/GamePhysics/Implementation/SphericalRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SphericalRigidBody.cpp @@ -15,9 +15,9 @@ SphericalRigidBody::SphericalRigidBody() this->gravityNormal = Float3::null; this->onCollision = Default::EventAction_Collision; this->onMovement = Default::EventAction_Move; - this->ignoreGravity = this->isForwarded = false; this->scene = nullptr; - this->body = Sphere( Float3::null, 0.5f ); + this->customTag = nullptr; + this->ignoreGravity = this->isForwarded = false; } SphericalRigidBody::SphericalRigidBody( const API::SphericalBodyDescription &desc ) @@ -51,9 +51,9 @@ SphericalRigidBody::SphericalRigidBody( const API::SphericalBodyDescription &des this->onMovement = Default::EventAction_Move; } - this->ignoreGravity = desc.ignoreGravity; this->scene = nullptr; - this->body = Sphere( desc.centerPosition, desc.radius ); + this->customTag = nullptr; + this->ignoreGravity = desc.ignoreGravity; } SphericalRigidBody::~SphericalRigidBody() {} @@ -150,7 +150,7 @@ bool SphericalRigidBody::Intersects( const ICustomBody &object, Float4 &worldPoi Sphere & SphericalRigidBody::GetBoundingSphere( Sphere &targetMem ) const { - return targetMem = this->body; + return targetMem = Sphere( this->rigid.centerPos, this->rigid.boundingReach.x ); } Float4 & SphericalRigidBody::GetNormalAt( const Float4 &worldPos, Float4 &targetMem ) const @@ -170,6 +170,11 @@ Float3 & SphericalRigidBody::GetGravityNormal( Float3 &targetMem ) const return targetMem = this->gravityNormal; } +void * SphericalRigidBody::GetCustomTag() const +{ + return this->customTag; +} + //Float3 & SphericalRigidBody::GetCenter( Float3 &targetMem ) const //{ // return targetMem = this->rigid.centerPos; @@ -206,7 +211,6 @@ UpdateState SphericalRigidBody::Update( Float timeStepLength ) } this->rigid.Update_LeapFrog( timeStepLength ); - this->body.center = this->rigid.centerPos; // compare previous and new state and return result //return this->current == this->previous ? UpdateState_resting : UpdateState_altered; @@ -258,6 +262,11 @@ void SphericalRigidBody::SetGravityNormal( const Float3 &normalizedVector ) this->gravityNormal = normalizedVector; } +void SphericalRigidBody::SetCustomTag( void *ref ) +{ + this->customTag = ref; +} + //void SphericalRigidBody::SetMomentOfInertiaTensor_KeepVelocity( const Float4x4 &localI ) //{ // this->rigid.SetMomentOfInertia_KeepVelocity( localI ); diff --git a/Code/GamePhysics/Implementation/SphericalRigidBody.h b/Code/GamePhysics/Implementation/SphericalRigidBody.h index 74b74535..49c8fb8c 100644 --- a/Code/GamePhysics/Implementation/SphericalRigidBody.h +++ b/Code/GamePhysics/Implementation/SphericalRigidBody.h @@ -33,6 +33,7 @@ namespace Oyster { namespace Physics ::Oyster::Collision3D::Sphere & GetBoundingSphere( ::Oyster::Collision3D::Sphere &targetMem = ::Oyster::Collision3D::Sphere() ) const; ::Oyster::Math::Float4 & GetNormalAt( const ::Oyster::Math::Float4 &worldPos, ::Oyster::Math::Float4 &targetMem = ::Oyster::Math::Float4() ) const; ::Oyster::Math::Float3 & GetGravityNormal( ::Oyster::Math::Float3 &targetMem = ::Oyster::Math::Float3() ) const; + void * GetCustomTag() const; //::Oyster::Math::Float3 & GetCenter( ::Oyster::Math::Float3 &targetMem = ::Oyster::Math::Float3() ) const; //::Oyster::Math::Float4x4 & GetRotation( ::Oyster::Math::Float4x4 &targetMem = ::Oyster::Math::Float4x4() ) const; //::Oyster::Math::Float4x4 & GetOrientation( ::Oyster::Math::Float4x4 &targetMem = ::Oyster::Math::Float4x4() ) const; @@ -48,6 +49,7 @@ namespace Oyster { namespace Physics void SetGravity( bool ignore); void SetGravityNormal( const ::Oyster::Math::Float3 &normalizedVector ); + void SetCustomTag( void *ref ); //void SetMomentOfInertiaTensor_KeepVelocity( const ::Oyster::Math::Float4x4 &localI ); //void SetMomentOfInertiaTensor_KeepMomentum( const ::Oyster::Math::Float4x4 &localI ); //void SetMass_KeepVelocity( ::Oyster::Math::Float m ); @@ -64,9 +66,9 @@ namespace Oyster { namespace Physics ::Oyster::Math::Float3 gravityNormal; EventAction_Collision onCollision; EventAction_Move onMovement; - bool ignoreGravity, isForwarded; Octree *scene; - ::Oyster::Collision3D::Sphere body; + void *customTag; + bool ignoreGravity, isForwarded; }; } } diff --git a/Code/GamePhysics/PhysicsAPI.h b/Code/GamePhysics/PhysicsAPI.h index 82a139c3..4fcd2940 100644 --- a/Code/GamePhysics/PhysicsAPI.h +++ b/Code/GamePhysics/PhysicsAPI.h @@ -327,6 +327,12 @@ namespace Oyster ********************************************************/ virtual ::Oyster::Math::Float3 & GetGravityNormal( ::Oyster::Math::Float3 &targetMem = ::Oyster::Math::Float3() ) const = 0; + /******************************************************** + * @return the void pointer set by SetCustomTag. + * nullptr if none is set. + ********************************************************/ + virtual void * GetCustomTag() const = 0; + ///******************************************************** // * The world position of this center of gravity. // * @param targetMem: Provided memory that written into and then returned. @@ -395,6 +401,13 @@ namespace Oyster ********************************************************/ virtual void SetGravityNormal( const ::Oyster::Math::Float3 &normalizedVector ) = 0; + /******************************************************** + * Not used by the engine itself. Just a quality of life feature + * for developers who want to tag something to the objects. + * @param ref: Anything castable to a void pointer, the engine won't care. + ********************************************************/ + virtual void SetCustomTag( void *ref ) = 0; + ///******************************************************** // * To not be called if is in Engine // * Use API::SetMomentOfInertiaTensor_KeepVelocity(...) instead