From 0cae6261e9d2fa0a7f9bad46f6dc024b6076c79d Mon Sep 17 00:00:00 2001 From: Robin Engman Date: Wed, 22 Jan 2014 12:36:12 +0100 Subject: [PATCH 1/3] ApplyEffect is now easier to handle for the user Only needs a function with a pointer to an ICustomBody now. --- Code/GamePhysics/Implementation/Octree.cpp | 2 +- Code/GamePhysics/Implementation/Octree.h | 2 +- Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp | 2 +- Code/GamePhysics/Implementation/PhysicsAPI_Impl.h | 2 +- Code/GamePhysics/PhysicsAPI.h | 8 ++++++-- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Code/GamePhysics/Implementation/Octree.cpp b/Code/GamePhysics/Implementation/Octree.cpp index 60f05298..3faff29e 100644 --- a/Code/GamePhysics/Implementation/Octree.cpp +++ b/Code/GamePhysics/Implementation/Octree.cpp @@ -134,7 +134,7 @@ void Octree::Visit(const Oyster::Collision3D::ICollideable& collideable, Visitor { if(collideable.Intersects(this->leafData[i].container)) { - hitAction(*this, i); + hitAction( this->GetCustomBody(i) ); } } } diff --git a/Code/GamePhysics/Implementation/Octree.h b/Code/GamePhysics/Implementation/Octree.h index 6c6606a9..7b350795 100644 --- a/Code/GamePhysics/Implementation/Octree.h +++ b/Code/GamePhysics/Implementation/Octree.h @@ -18,7 +18,7 @@ namespace Oyster static const unsigned int invalid_ref; typedef void(*VisitorAction)(Octree&, unsigned int, unsigned int); - typedef void(*VisitorActionCollideable)(Octree&, unsigned int); + typedef void(*VisitorActionCollideable)(ICustomBody*); struct Data { diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index 2899eae6..5b88baf5 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -268,7 +268,7 @@ void API_Impl::RemoveGravity( const API::Gravity &g ) } } -void API_Impl::ApplyEffect( const Oyster::Collision3D::ICollideable& collideable, void(hitAction)(Octree&, unsigned int) ) +void API_Impl::ApplyEffect( const Oyster::Collision3D::ICollideable& collideable, void(hitAction)(ICustomBody*) ) { this->worldScene.Visit(collideable, hitAction); } diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h index ad2c91a4..c50668de 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h @@ -35,7 +35,7 @@ namespace Oyster void AddGravity( const API::Gravity &g ); void RemoveGravity( const API::Gravity &g ); - void ApplyEffect( const Oyster::Collision3D::ICollideable& collideable, void(hitAction)(Octree&, unsigned int) ); + void ApplyEffect( const Oyster::Collision3D::ICollideable& collideable, void(hitAction)(ICustomBody*) ); //void ApplyForceAt( const ICustomBody* objRef, const ::Oyster::Math::Float3 &worldPos, const ::Oyster::Math::Float3 &worldF ); diff --git a/Code/GamePhysics/PhysicsAPI.h b/Code/GamePhysics/PhysicsAPI.h index 4e1844f5..98daac0d 100644 --- a/Code/GamePhysics/PhysicsAPI.h +++ b/Code/GamePhysics/PhysicsAPI.h @@ -16,7 +16,6 @@ namespace Oyster { class API; class ICustomBody; - class Octree; namespace Struct { @@ -137,7 +136,12 @@ namespace Oyster ********************************************************/ virtual void RemoveGravity( const API::Gravity &g ) = 0; - virtual void ApplyEffect( const Oyster::Collision3D::ICollideable& collideable, void(hitAction)(Octree&, unsigned int) ) = 0; + /******************************************************** + * Applies an effect to objects that collide with the set volume. + * @param collideable: An ICollideable that defines the volume of the effect. + * @param hitAction: A function that contains the effect. + ********************************************************/ + virtual void ApplyEffect( const Oyster::Collision3D::ICollideable& collideable, void(hitAction)(ICustomBody*) ) = 0; ///******************************************************** // * Apply force on an object. From 53961f0e88ada554347e215673bb47e46f964d69 Mon Sep 17 00:00:00 2001 From: Robin Engman Date: Wed, 22 Jan 2014 13:50:54 +0100 Subject: [PATCH 2/3] Added new onCollisionResponse handle --- .../Implementation/PhysicsAPI_Impl.cpp | 10 +++++++ .../Implementation/PhysicsAPI_Impl.h | 1 + .../Implementation/SimpleRigidBody.cpp | 27 ++++++++++++++++++ .../Implementation/SimpleRigidBody.h | 3 ++ .../Implementation/SphericalRigidBody.cpp | 28 +++++++++++++++++++ .../Implementation/SphericalRigidBody.h | 3 ++ Code/GamePhysics/PhysicsAPI.h | 13 +++++++++ Code/GamePhysics/PhysicsStructs.h | 2 ++ 8 files changed, 87 insertions(+) diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index 5b88baf5..f70841a7 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -94,9 +94,14 @@ namespace // proto->Predict( forwardedDeltaPos, forwardedDeltaAxis, bounceLinearImpulse, bounceAngularImpulse, API_instance.GetFrameTimeLength() ); // } + + // protoState.ApplyForwarding( forwardedDeltaPos, forwardedDeltaAxis ); protoState.ApplyImpulse( bounce, worldPointOfContact, normal ); proto->SetState( protoState ); + + proto->CallSubscription_CollisionResponse( deuter, protoState.GetLinearMomentum().GetMagnitude()/(protoState.GetMass() + protoState.GetLinearMomentum().GetMagnitude())); + } break; } @@ -381,6 +386,11 @@ namespace Oyster { namespace Physics return ::Oyster::Physics::ICustomBody::SubscriptMessage_none; } + void EventAction_CollisionResponse( const ::Oyster::Physics::ICustomBody *proto, const ::Oyster::Physics::ICustomBody *deuter, ::Oyster::Math::Float kineticEnergyLoss ) + { /* 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 c50668de..b9343ae6 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h @@ -62,6 +62,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_CollisionResponse( const ::Oyster::Physics::ICustomBody *proto, const ::Oyster::Physics::ICustomBody *deuter, ::Oyster::Math::Float kineticEnergyLoss ); void EventAction_Move( const ::Oyster::Physics::ICustomBody *object ); } } diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp index 1f2837b2..5a9bf53f 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp @@ -47,6 +47,7 @@ SimpleRigidBody::SimpleRigidBody() this->rigid.SetMass_KeepMomentum( 16.0f ); this->gravityNormal = Float3::null; this->onCollision = Default::EventAction_Collision; + this->onCollisionResponse = Default::EventAction_CollisionResponse; this->onMovement = Default::EventAction_Move; this->scene = nullptr; this->customTag = nullptr; @@ -74,6 +75,15 @@ SimpleRigidBody::SimpleRigidBody( const API::SimpleBodyDescription &desc ) this->onCollision = Default::EventAction_Collision; } + if( desc.subscription_onCollisionResponse ) + { + this->onCollisionResponse = desc.subscription_onCollisionResponse; + } + else + { + this->onCollisionResponse = Default::EventAction_CollisionResponse; + } + if( desc.subscription_onMovement ) { this->onMovement= desc.subscription_onMovement; @@ -158,6 +168,11 @@ ICustomBody::SubscriptMessage SimpleRigidBody::CallSubscription_Collision( const return this->onCollision( this, deuter ); } +void SimpleRigidBody::CallSubscription_CollisionResponse( const ICustomBody *deuter, Float kineticEnergyLoss ) +{ + return this->onCollisionResponse( this, deuter, kineticEnergyLoss ); +} + void SimpleRigidBody::CallSubscription_Move() { this->onMovement( this ); @@ -315,6 +330,18 @@ void SimpleRigidBody::SetSubscription( ICustomBody::EventAction_Collision functi } } +void SimpleRigidBody::SetSubscription( ICustomBody::EventAction_CollisionResponse functionPointer ) +{ + if( functionPointer ) + { + this->onCollisionResponse = functionPointer; + } + else + { + this->onCollisionResponse = Default::EventAction_CollisionResponse; + } +} + void SimpleRigidBody::SetSubscription( ICustomBody::EventAction_Move functionPointer ) { if( functionPointer ) diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.h b/Code/GamePhysics/Implementation/SimpleRigidBody.h index cce657da..271c4362 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.h +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.h @@ -22,6 +22,7 @@ namespace Oyster { namespace Physics //::Oyster::Math::Float3 GetRigidLinearVelocity() const; SubscriptMessage CallSubscription_Collision( const ICustomBody *deuter ); + void CallSubscription_CollisionResponse( const ICustomBody *deuter, ::Oyster::Math::Float kineticEnergyLoss ); void CallSubscription_Move(); bool IsAffectedByGravity() const; @@ -44,6 +45,7 @@ namespace Oyster { namespace Physics void SetScene( void *scene ); void SetSubscription( EventAction_Collision functionPointer ); + void SetSubscription( EventAction_CollisionResponse functionPointer ); void SetSubscription( EventAction_Move functionPointer ); void SetGravity( bool ignore); @@ -64,6 +66,7 @@ namespace Oyster { namespace Physics ::Oyster::Math::Float4 deltaPos, deltaAxis; ::Oyster::Math::Float3 gravityNormal; EventAction_Collision onCollision; + EventAction_CollisionResponse onCollisionResponse; EventAction_Move onMovement; Octree *scene; void *customTag; diff --git a/Code/GamePhysics/Implementation/SphericalRigidBody.cpp b/Code/GamePhysics/Implementation/SphericalRigidBody.cpp index 7b15510a..9fd8d219 100644 --- a/Code/GamePhysics/Implementation/SphericalRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SphericalRigidBody.cpp @@ -14,6 +14,7 @@ SphericalRigidBody::SphericalRigidBody() this->rigid.SetMass_KeepMomentum( 10.0f ); this->gravityNormal = Float3::null; this->onCollision = Default::EventAction_Collision; + this->onCollisionResponse = Default::EventAction_CollisionResponse; this->onMovement = Default::EventAction_Move; this->scene = nullptr; this->customTag = nullptr; @@ -42,6 +43,15 @@ SphericalRigidBody::SphericalRigidBody( const API::SphericalBodyDescription &des this->onCollision = Default::EventAction_Collision; } + if( desc.subscription_onCollisionResponse ) + { + this->onCollisionResponse = desc.subscription_onCollisionResponse; + } + else + { + this->onCollisionResponse = Default::EventAction_CollisionResponse; + } + if( desc.subscription_onMovement ) { this->onMovement= desc.subscription_onMovement; @@ -123,6 +133,12 @@ ICustomBody::SubscriptMessage SphericalRigidBody::CallSubscription_Collision( co return this->onCollision( this, deuter ); } +void SphericalRigidBody::CallSubscription_CollisionResponse( const ICustomBody *deuter, Float kineticEnergyLoss ) +{ + this->onCollisionResponse( this, deuter, kineticEnergyLoss); +} + + void SphericalRigidBody::CallSubscription_Move() { this->onMovement( this ); @@ -234,6 +250,18 @@ void SphericalRigidBody::SetSubscription( ICustomBody::EventAction_Collision fun } } +void SphericalRigidBody::SetSubscription( ICustomBody::EventAction_CollisionResponse functionPointer ) +{ + if( functionPointer ) + { + this->onCollisionResponse = functionPointer; + } + else + { + this->onCollisionResponse = Default::EventAction_CollisionResponse; + } +} + void SphericalRigidBody::SetSubscription( ICustomBody::EventAction_Move functionPointer ) { if( functionPointer ) diff --git a/Code/GamePhysics/Implementation/SphericalRigidBody.h b/Code/GamePhysics/Implementation/SphericalRigidBody.h index 49c8fb8c..213e225b 100644 --- a/Code/GamePhysics/Implementation/SphericalRigidBody.h +++ b/Code/GamePhysics/Implementation/SphericalRigidBody.h @@ -23,6 +23,7 @@ namespace Oyster { namespace Physics //::Oyster::Math::Float3 GetRigidLinearVelocity() const; SubscriptMessage CallSubscription_Collision( const ICustomBody *deuter ); + void CallSubscription_CollisionResponse( const ICustomBody *deuter, ::Oyster::Math::Float kineticEnergyLoss ); void CallSubscription_Move(); bool IsAffectedByGravity() const; @@ -45,6 +46,7 @@ namespace Oyster { namespace Physics void SetScene( void *scene ); void SetSubscription( EventAction_Collision functionPointer ); + void SetSubscription( EventAction_CollisionResponse functionPointer ); void SetSubscription( EventAction_Move functionPointer ); void SetGravity( bool ignore); @@ -65,6 +67,7 @@ namespace Oyster { namespace Physics ::Oyster::Math::Float4 deltaPos, deltaAxis; ::Oyster::Math::Float3 gravityNormal; EventAction_Collision onCollision; + EventAction_CollisionResponse onCollisionResponse; EventAction_Move onMovement; Octree *scene; void *customTag; diff --git a/Code/GamePhysics/PhysicsAPI.h b/Code/GamePhysics/PhysicsAPI.h index 98daac0d..7f20fcf7 100644 --- a/Code/GamePhysics/PhysicsAPI.h +++ b/Code/GamePhysics/PhysicsAPI.h @@ -238,6 +238,7 @@ namespace Oyster enum SubscriptMessage { SubscriptMessage_none, + SubscriptMessage_kineticLoss, SubscriptMessage_ignore_collision_response }; @@ -261,6 +262,11 @@ namespace Oyster ********************************************************/ virtual SubscriptMessage CallSubscription_Collision( const ICustomBody *deuter ) = 0; + /******************************************************** + * @todo TODO: need doc + ********************************************************/ + virtual void CallSubscription_CollisionResponse( const ICustomBody *deuter, ::Oyster::Math::Float kineticEnergyLoss ) = 0; + /******************************************************** * @todo TODO: need doc ********************************************************/ @@ -391,6 +397,13 @@ namespace Oyster ********************************************************/ virtual void SetSubscription( EventAction_Collision functionPointer ) = 0; + /******************************************************** + * Sets the function that will be called by the engine + * whenever a collision has finished. + * @param functionPointer: If NULL, an empty default function will be set. + ********************************************************/ + virtual void SetSubscription( EventAction_CollisionResponse functionPointer ) = 0; + /******************************************************** * Sets the function that will be called by the engine * whenever an object have moved. diff --git a/Code/GamePhysics/PhysicsStructs.h b/Code/GamePhysics/PhysicsStructs.h index 059e9f98..1bc1736f 100644 --- a/Code/GamePhysics/PhysicsStructs.h +++ b/Code/GamePhysics/PhysicsStructs.h @@ -19,6 +19,7 @@ namespace Oyster { namespace Physics ::Oyster::Math::Float frictionCoeff_Dynamic; ::Oyster::Math::Float4x4 inertiaTensor; ::Oyster::Physics::ICustomBody::EventAction_Collision subscription_onCollision; + ::Oyster::Physics::ICustomBody::EventAction_CollisionResponse subscription_onCollisionResponse; ::Oyster::Physics::ICustomBody::EventAction_Move subscription_onMovement; bool ignoreGravity; @@ -35,6 +36,7 @@ namespace Oyster { namespace Physics ::Oyster::Math::Float frictionCoeff_Static; ::Oyster::Math::Float frictionCoeff_Dynamic; ::Oyster::Physics::ICustomBody::EventAction_Collision subscription_onCollision; + ::Oyster::Physics::ICustomBody::EventAction_CollisionResponse subscription_onCollisionResponse; ::Oyster::Physics::ICustomBody::EventAction_Move subscription_onMovement; bool ignoreGravity; From bcbe67b62090047baa5419463fde30c803b0f59a Mon Sep 17 00:00:00 2001 From: Robin Engman Date: Wed, 22 Jan 2014 14:09:53 +0100 Subject: [PATCH 3/3] Made default value of onCollisionResponse null --- Code/GamePhysics/PhysicsStructs-Impl.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Code/GamePhysics/PhysicsStructs-Impl.h b/Code/GamePhysics/PhysicsStructs-Impl.h index eac9bef3..46de90cc 100644 --- a/Code/GamePhysics/PhysicsStructs-Impl.h +++ b/Code/GamePhysics/PhysicsStructs-Impl.h @@ -21,6 +21,7 @@ namespace Oyster this->frictionCoeff_Static = 0.5f; this->inertiaTensor = ::Oyster::Math::Float4x4::identity; this->subscription_onCollision = NULL; + this->subscription_onCollisionResponse = NULL; this->subscription_onMovement = NULL; this->ignoreGravity = false; } @@ -35,6 +36,7 @@ namespace Oyster this->frictionCoeff_Dynamic = 0.5f; this->frictionCoeff_Static = 0.5f; this->subscription_onCollision = NULL; + this->subscription_onCollisionResponse = NULL; this->subscription_onMovement = NULL; this->ignoreGravity = false; }