From 29362c8c60ab06fb375673ce19703bd71fc640ae Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Tue, 26 Nov 2013 13:27:34 +0100 Subject: [PATCH 1/6] Added more API & ICustomBody methods API::Init ICustomBody::SetSubscription Gravity related calls moved DLLMain.cpp --- Code/GamePhysics/GamePhysics.vcxproj | 4 +- Code/GamePhysics/GamePhysics.vcxproj.filters | 8 ++- .../{ => Implementation}/DLLMain.cpp | 0 .../Implementation/PhysicsAPI_Impl.cpp | 10 +++ .../Implementation/PhysicsAPI_Impl.h | 3 + .../Implementation/SimpleRigidBody.cpp | 39 ++++++++++- .../Implementation/SimpleRigidBody.h | 8 +++ Code/GamePhysics/PhysicsAPI.h | 67 ++++++++++++++++--- 8 files changed, 126 insertions(+), 13 deletions(-) rename Code/GamePhysics/{ => Implementation}/DLLMain.cpp (100%) diff --git a/Code/GamePhysics/GamePhysics.vcxproj b/Code/GamePhysics/GamePhysics.vcxproj index ddc37cf4..232bc3ed 100644 --- a/Code/GamePhysics/GamePhysics.vcxproj +++ b/Code/GamePhysics/GamePhysics.vcxproj @@ -152,12 +152,14 @@ + + - + diff --git a/Code/GamePhysics/GamePhysics.vcxproj.filters b/Code/GamePhysics/GamePhysics.vcxproj.filters index 256cd6b1..4c0fd5f3 100644 --- a/Code/GamePhysics/GamePhysics.vcxproj.filters +++ b/Code/GamePhysics/GamePhysics.vcxproj.filters @@ -30,6 +30,9 @@ Header Files\Implementation + + Header Files\Implementation + @@ -38,7 +41,10 @@ Source Files - + + Source Files + + Source Files diff --git a/Code/GamePhysics/DLLMain.cpp b/Code/GamePhysics/Implementation/DLLMain.cpp similarity index 100% rename from Code/GamePhysics/DLLMain.cpp rename to Code/GamePhysics/Implementation/DLLMain.cpp diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index 54af5084..6c3e4a27 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -57,6 +57,11 @@ API_Impl::API_Impl() API_Impl::~API_Impl() {} +void API_Impl::Init( unsigned int numObjects, unsigned int numGravityWells , const Float3 &worldSize ) +{ + //! @todo TODO: implement stub +} + void API_Impl::SetDeltaTime( float deltaTime ) { updateFrameLength = deltaTime; @@ -155,6 +160,11 @@ void API_Impl::SetOrientation( const ICustomBody* objRef, const Float4x4 &orient //! @todo TODO: implement stub } +void API_Impl::SetSize( const ICustomBody* objRef, const Float3 &size ) +{ + //! @todo TODO: implement stub +} + UniquePointer API_Impl::CreateSimpleRigidBody() const { return new SimpleRigidBody(); diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h index c02ebdb2..5a2e3ef6 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h @@ -13,6 +13,8 @@ namespace Oyster API_Impl(); virtual ~API_Impl(); + void Init( unsigned int numObjects, unsigned int numGravityWells , const ::Oyster::Math::Float3 &worldSize ); + void SetDeltaTime( float deltaTime ); void SetGravityConstant( float g ); void SetAction( EventAction_Collision functionPointer ); @@ -38,6 +40,7 @@ namespace Oyster void SetCenter( const ICustomBody* objRef, const ::Oyster::Math::Float3 &worldPos ); void SetRotation( const ICustomBody* objRef, const ::Oyster::Math::Float4x4 &rotation ); void SetOrientation( const ICustomBody* objRef, const ::Oyster::Math::Float4x4 &orientation ); + void SetSize( const ICustomBody* objRef, const ::Oyster::Math::Float3 &size ); ::Utility::DynamicMemory::UniquePointer CreateSimpleRigidBody() const; private: diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp index 7fd15e40..cc1f847b 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp @@ -7,7 +7,11 @@ using namespace ::Oyster::Collision3D; using namespace ::Utility::DynamicMemory; using namespace ::Utility::Value; -SimpleRigidBody::SimpleRigidBody() : previous(), current() {} +SimpleRigidBody::SimpleRigidBody() + : previous(), current(), + gravityNormal(0.0f), + subscribeCollision(true), + ignoreGravity(false) {} SimpleRigidBody::~SimpleRigidBody() {} @@ -18,7 +22,12 @@ UniquePointer SimpleRigidBody::Clone() const bool SimpleRigidBody::IsSubscribingCollisions() const { // Assumption - return true; + return this->subscribeCollision; +} + +bool SimpleRigidBody::IsAffectedByGravity() const +{ + return !this->ignoreGravity; } bool SimpleRigidBody::Intersects( const ICustomBody &object, Float timeStepLength, Float &deltaWhen, Float3 &worldPointOfContact ) const @@ -51,6 +60,11 @@ Float3 & SimpleRigidBody::GetNormalAt( const Float3 &worldPos, Float3 &targetMem return targetMem = (worldPos - this->current.box.center).GetNormalized(); } +Float3 & SimpleRigidBody::GetGravityNormal( Float3 &targetMem ) const +{ + return targetMem = this->gravityNormal; +} + Float3 & SimpleRigidBody::GetCenter( Float3 &targetMem ) const { return targetMem = this->current.box.center; @@ -81,6 +95,22 @@ UpdateState SimpleRigidBody::Update( Float timeStepLength ) return this->current == this->previous ? resting : altered; } +void SimpleRigidBody::SetGravity( bool ignore) +{ + this->ignoreGravity = ignore; + this->gravityNormal = Float3::null; +} + +void SimpleRigidBody::SetGravityNormal( const Float3 &normalizedVector ) +{ + this->gravityNormal = normalizedVector; +} + +void SimpleRigidBody::SetSubscription( bool subscribeCollision ) +{ + this->subscribeCollision = subscribeCollision; +} + void SimpleRigidBody::SetMomentOfInertiaTensor_KeepVelocity( const Float4x4 &localI ) { this->current.SetMomentOfInertia_KeepVelocity( localI ); @@ -114,4 +144,9 @@ void SimpleRigidBody::SetRotation( const Float4x4 &rotation ) void SimpleRigidBody::SetOrientation( const Float4x4 &orientation ) { this->current.SetOrientation( orientation ); +} + +void SimpleRigidBody::SetSize( const Float3 &size ) +{ + this->current.SetSize( size ); } \ No newline at end of file diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.h b/Code/GamePhysics/Implementation/SimpleRigidBody.h index 99295e44..41be919b 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.h +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.h @@ -15,11 +15,13 @@ namespace Oyster { namespace Physics ::Utility::DynamicMemory::UniquePointer Clone() const; bool IsSubscribingCollisions() const; + bool IsAffectedByGravity() const; bool Intersects( const ICustomBody &object, ::Oyster::Math::Float timeStepLength, ::Oyster::Math::Float &deltaWhen, ::Oyster::Math::Float3 &worldPointOfContact ) const; bool Intersects( const ::Oyster::Collision3D::ICollideable &shape ) const; ::Oyster::Collision3D::Sphere & GetBoundingSphere( ::Oyster::Collision3D::Sphere &targetMem = ::Oyster::Collision3D::Sphere() ) const; ::Oyster::Math::Float3 & GetNormalAt( const ::Oyster::Math::Float3 &worldPos, ::Oyster::Math::Float3 &targetMem = ::Oyster::Math::Float3() ) const; + ::Oyster::Math::Float3 & GetGravityNormal( ::Oyster::Math::Float3 &targetMem = ::Oyster::Math::Float3() ) 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; @@ -27,6 +29,9 @@ namespace Oyster { namespace Physics UpdateState Update( ::Oyster::Math::Float timeStepLength ); + void SetGravity( bool ignore); + void SetGravityNormal( const ::Oyster::Math::Float3 &normalizedVector ); + void SetSubscription( bool subscribeCollision ); void SetMomentOfInertiaTensor_KeepVelocity( const ::Oyster::Math::Float4x4 &localI ); void SetMomentOfInertiaTensor_KeepMomentum( const ::Oyster::Math::Float4x4 &localI ); void SetMass_KeepVelocity( ::Oyster::Math::Float m ); @@ -34,9 +39,12 @@ namespace Oyster { namespace Physics void SetCenter( const ::Oyster::Math::Float3 &worldPos ); void SetRotation( const ::Oyster::Math::Float4x4 &rotation ); void SetOrientation( const ::Oyster::Math::Float4x4 &orientation ); + void SetSize( const ::Oyster::Math::Float3 &size ); private: ::Oyster::Physics3D::RigidBody previous, current; + ::Oyster::Math::Float3 gravityNormal; + bool subscribeCollision, ignoreGravity; }; } } diff --git a/Code/GamePhysics/PhysicsAPI.h b/Code/GamePhysics/PhysicsAPI.h index 0f4a80ee..bf80e6fa 100644 --- a/Code/GamePhysics/PhysicsAPI.h +++ b/Code/GamePhysics/PhysicsAPI.h @@ -51,6 +51,14 @@ namespace Oyster /** Gets the Physics instance. */ static API & Instance(); + /******************************************************** + * Clears all content and reset Engine assetts such as buffers. + * @param numObjects: The predicted max number of active objects. + * @param numGravityWells: The predicted max number of active gravity wells. + * @param worldSize: The size of acceptable physics space. + ********************************************************/ + virtual void Init( unsigned int numObjects, unsigned int numGravityWells , const ::Oyster::Math::Float3 &worldSize ) = 0; + /******************************************************** * Sets the time length of each physics update frame. ********************************************************/ @@ -133,7 +141,7 @@ namespace Oyster * @param worldF: Vector with the direction and magnitude of the force. [N] ********************************************************/ virtual void ApplyForceAt( const ICustomBody* objRef, const ::Oyster::Math::Float3 &worldPos, const ::Oyster::Math::Float3 &worldF ) = 0; - + /******************************************************** * Apply force on an object. * @param objRefA: A pointer to the ICustomBody representing a physical object. @@ -196,6 +204,13 @@ namespace Oyster ********************************************************/ virtual void SetOrientation( const ICustomBody* objRef, const ::Oyster::Math::Float4x4 &orientation ) = 0; + /******************************************************** + * Resizes the boundingBox. + * @param objRef: A pointer to the ICustomBody representing a physical object. + * @param size: New size of this [m] + ********************************************************/ + virtual void SetSize( const ICustomBody* objRef, const ::Oyster::Math::Float3 &size ) = 0; + /******************************************************** * Creates a new dynamically allocated object that can be used as a component for more complex ICustomBodies. * @return A pointer along with the responsibility to delete. @@ -223,6 +238,11 @@ namespace Oyster ********************************************************/ virtual bool IsSubscribingCollisions() const = 0; + /******************************************************** + * @return true if Engine should apply gravity on this object. + ********************************************************/ + virtual bool IsAffectedByGravity() const = 0; + /******************************************************** * Performs a detailed Intersect test and returns if, when and where. * @param object: What this is intersect testing against. @@ -254,6 +274,13 @@ namespace Oyster ********************************************************/ virtual ::Oyster::Math::Float3 & GetNormalAt( const ::Oyster::Math::Float3 &worldPos, ::Oyster::Math::Float3 &targetMem = ::Oyster::Math::Float3() ) const = 0; + /******************************************************** + * The gravity normal will have same direction as the total gravity force pulling on this and have the magnitude of 1.0f. + * @param targetMem: Provided memory that written into and then returned. + * @return a normalized vector in worldSpace. Exception: Null vector if no gravity been applied. + ********************************************************/ + virtual ::Oyster::Math::Float3 & GetGravityNormal( ::Oyster::Math::Float3 &targetMem = ::Oyster::Math::Float3() ) const = 0; + /******************************************************** * The world position of this center of gravity. * @param targetMem: Provided memory that written into and then returned. @@ -280,52 +307,74 @@ namespace Oyster virtual ::Oyster::Math::Float4x4 & GetView( ::Oyster::Math::Float4x4 &targetMem = ::Oyster::Math::Float4x4() ) const = 0; /******************************************************** - * To be only called by Engine + * To not be called if is in Engine * Is called during API::Update ********************************************************/ virtual UpdateState Update( ::Oyster::Math::Float timeStepLength ) = 0; + + /******************************************************** + * @param ignore: True if Engine should not apply Gravity. + ********************************************************/ + virtual void SetGravity( bool ignore) = 0; /******************************************************** - * To be only called by Engine + * Used by Engine + * @param normalizedVector: Should have same direction as the pullinggravity. + ********************************************************/ + virtual void SetGravityNormal( const ::Oyster::Math::Float3 &normalizedVector ) = 0; + + /******************************************************** + * @param subscribeCollision: If is true, engine will call EventAction_Collision when this collides. + ********************************************************/ + virtual void SetSubscription( bool subscribeCollision ) = 0; + + /******************************************************** + * To not be called if is in Engine * Use API::SetMomentOfInertiaTensor_KeepVelocity(...) instead ********************************************************/ virtual void SetMomentOfInertiaTensor_KeepVelocity( const ::Oyster::Math::Float4x4 &localI ) = 0; /******************************************************** - * To be only called by Engine + * To not be called if is in Engine * Use API::SetMomentOfInertiaTensor_KeepMomentum(...) ********************************************************/ virtual void SetMomentOfInertiaTensor_KeepMomentum( const ::Oyster::Math::Float4x4 &localI ) = 0; /******************************************************** - * To be only called by Engine + * To not be called if is in Engine * Use API::SetMass_KeepVelocity(...) ********************************************************/ virtual void SetMass_KeepVelocity( ::Oyster::Math::Float m ) = 0; /******************************************************** - * To be only called by Engine + * To not be called if is in Engine * Use API::SetMass_KeepMomentum(...) ********************************************************/ virtual void SetMass_KeepMomentum( ::Oyster::Math::Float m ) = 0; /******************************************************** - * To be only called by Engine + * To not be called if is in Engine * Use API::SetCenter(...) ********************************************************/ virtual void SetCenter( const ::Oyster::Math::Float3 &worldPos ) = 0; /******************************************************** - * To be only called by Engine + * To not be called if is in Engine * Use API::SetRotation(...) ********************************************************/ virtual void SetRotation( const ::Oyster::Math::Float4x4 &rotation ) = 0; /******************************************************** - * To be only called by Engine + * To not be called if is in Engine * Use API::SetOrientation(...) ********************************************************/ virtual void SetOrientation( const ::Oyster::Math::Float4x4 &orientation ) = 0; + + /******************************************************** + * To not be called if is in Engine + * Use API::SetSize(...) + ********************************************************/ + virtual void SetSize( const ::Oyster::Math::Float3 &size ) = 0; }; } } From 62b1c684790ad3335913fd5c197b97c5b4e1cbe7 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Tue, 26 Nov 2013 13:28:33 +0100 Subject: [PATCH 2/6] Created SphericalRigidBody A rotateable rigidbody that represents something spherical. --- .../Implementation/SphericalRigidBody.cpp | 157 ++++++++++++++++++ .../Implementation/SphericalRigidBody.h | 53 ++++++ 2 files changed, 210 insertions(+) create mode 100644 Code/GamePhysics/Implementation/SphericalRigidBody.cpp create mode 100644 Code/GamePhysics/Implementation/SphericalRigidBody.h diff --git a/Code/GamePhysics/Implementation/SphericalRigidBody.cpp b/Code/GamePhysics/Implementation/SphericalRigidBody.cpp new file mode 100644 index 00000000..20d806c9 --- /dev/null +++ b/Code/GamePhysics/Implementation/SphericalRigidBody.cpp @@ -0,0 +1,157 @@ +#include "SphericalRigidBody.h" +#include "PhysicsAPI_Impl.h" + +using namespace ::Oyster::Physics; +using namespace ::Oyster::Math3D; +using namespace ::Oyster::Collision3D; +using namespace ::Utility::DynamicMemory; +using namespace ::Utility::Value; + +SphericalRigidBody::SphericalRigidBody() + : previous(), current( Box(Float4x4::identity, Float3::null, Float3(1.0f)) ), + gravityNormal( 0.0f ), + subscribeCollision( true ), + ignoreGravity( false ), + body( Float3::null, 0.5f ) {} + +SphericalRigidBody::~SphericalRigidBody() {} + +UniquePointer SphericalRigidBody::Clone() const +{ + return new SphericalRigidBody( *this ); +} + +bool SphericalRigidBody::IsSubscribingCollisions() const +{ // Assumption + return this->subscribeCollision; +} + +bool SphericalRigidBody::IsAffectedByGravity() const +{ + return !this->ignoreGravity; +} + +bool SphericalRigidBody::Intersects( const ICustomBody &object, Float timeStepLength, Float &deltaWhen, Float3 &worldPointOfContact ) const +{ + if( object.Intersects(this->body) ) + { //! @todo TODO: better implementation needed + deltaWhen = timeStepLength; + worldPointOfContact = Average( this->body.center, object.GetCenter() ); + return true; + } + else + { + return false; + } +} + +bool SphericalRigidBody::Intersects( const ICollideable &shape ) const +{ + return this->current.box.Intersects( shape ); +} + +Sphere & SphericalRigidBody::GetBoundingSphere( Sphere &targetMem ) const +{ + return targetMem = this->body; +} + +Float3 & SphericalRigidBody::GetNormalAt( const Float3 &worldPos, Float3 &targetMem ) const +{ + //! @todo TODO: better implementation needed + return targetMem = (worldPos - this->current.box.center).GetNormalized(); +} + +Float3 & SphericalRigidBody::GetGravityNormal( Float3 &targetMem ) const +{ + return targetMem = this->gravityNormal; +} + +Float3 & SphericalRigidBody::GetCenter( Float3 &targetMem ) const +{ + return targetMem = this->current.box.center; +} + +Float4x4 & SphericalRigidBody::GetRotation( Float4x4 &targetMem ) const +{ + return targetMem = this->current.box.rotation; +} + +Float4x4 & SphericalRigidBody::GetOrientation( Float4x4 &targetMem ) const +{ + return targetMem = this->current.GetOrientation(); +} + +Float4x4 & SphericalRigidBody::GetView( Float4x4 &targetMem ) const +{ + return targetMem = this->current.GetView(); +} + +UpdateState SphericalRigidBody::Update( Float timeStepLength ) +{ + this->previous = this->current; // memorizing the old state + + this->current.Update_LeapFrog( timeStepLength ); + this->body.center = this->current.GetCenter(); + + // compare previous and new state and return result + return this->current == this->previous ? resting : altered; +} + +void SphericalRigidBody::SetGravity( bool ignore) +{ + this->ignoreGravity = ignore; + this->gravityNormal = Float3::null; +} + +void SphericalRigidBody::SetGravityNormal( const Float3 &normalizedVector ) +{ + this->gravityNormal = normalizedVector; +} + +void SphericalRigidBody::SetSubscription( bool subscribeCollision ) +{ + this->subscribeCollision = subscribeCollision; +} + +void SphericalRigidBody::SetMomentOfInertiaTensor_KeepVelocity( const Float4x4 &localI ) +{ + this->current.SetMomentOfInertia_KeepVelocity( localI ); +} + +void SphericalRigidBody::SetMomentOfInertiaTensor_KeepMomentum( const Float4x4 &localI ) +{ + this->current.SetMomentOfInertia_KeepMomentum( localI ); +} + +void SphericalRigidBody::SetMass_KeepVelocity( Float m ) +{ + this->current.SetMass_KeepVelocity( m ); +} + +void SphericalRigidBody::SetMass_KeepMomentum( Float m ) +{ + this->current.SetMass_KeepMomentum( m ); +} + +void SphericalRigidBody::SetCenter( const Float3 &worldPos ) +{ + this->current.SetCenter( worldPos ); + this->body.center = worldPos; +} + +void SphericalRigidBody::SetRotation( const Float4x4 &rotation ) +{ + this->current.SetRotation( rotation ); +} + +void SphericalRigidBody::SetOrientation( const Float4x4 &orientation ) +{ + this->current.SetOrientation( orientation ); + this->body.center = orientation.v[3].xyz; +} + +void SphericalRigidBody::SetSize( const Float3 &size ) +{ + this->current.SetSize( size ); + this->body.radius = 0.5f * Min( Min( size.x, size.y ), size.z ); // inline Min( FloatN )? +} \ No newline at end of file diff --git a/Code/GamePhysics/Implementation/SphericalRigidBody.h b/Code/GamePhysics/Implementation/SphericalRigidBody.h new file mode 100644 index 00000000..7a727ee8 --- /dev/null +++ b/Code/GamePhysics/Implementation/SphericalRigidBody.h @@ -0,0 +1,53 @@ +#ifndef OYSTER_PHYSICS_SPHERICAL_RIGIDBODY_H +#define OYSTER_PHYSICS_SPHERICAL_RIGIDBODY_H + +#include "..\PhysicsAPI.h" +#include "RigidBody.h" +#include "Sphere.h" + +namespace Oyster { namespace Physics +{ + class SphericalRigidBody : public ICustomBody + { + public: + SphericalRigidBody(); + virtual ~SphericalRigidBody(); + + ::Utility::DynamicMemory::UniquePointer Clone() const; + + bool IsSubscribingCollisions() const; + bool IsAffectedByGravity() const; + bool Intersects( const ICustomBody &object, ::Oyster::Math::Float timeStepLength, ::Oyster::Math::Float &deltaWhen, ::Oyster::Math::Float3 &worldPointOfContact ) const; + bool Intersects( const ::Oyster::Collision3D::ICollideable &shape ) const; + + ::Oyster::Collision3D::Sphere & GetBoundingSphere( ::Oyster::Collision3D::Sphere &targetMem = ::Oyster::Collision3D::Sphere() ) const; + ::Oyster::Math::Float3 & GetNormalAt( const ::Oyster::Math::Float3 &worldPos, ::Oyster::Math::Float3 &targetMem = ::Oyster::Math::Float3() ) const; + ::Oyster::Math::Float3 & GetGravityNormal( ::Oyster::Math::Float3 &targetMem = ::Oyster::Math::Float3() ) 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; + ::Oyster::Math::Float4x4 & GetView( ::Oyster::Math::Float4x4 &targetMem = ::Oyster::Math::Float4x4() ) const; + + UpdateState Update( ::Oyster::Math::Float timeStepLength ); + + void SetGravity( bool ignore); + void SetGravityNormal( const ::Oyster::Math::Float3 &normalizedVector ); + void SetSubscription( bool subscribeCollision ); + void SetMomentOfInertiaTensor_KeepVelocity( const ::Oyster::Math::Float4x4 &localI ); + void SetMomentOfInertiaTensor_KeepMomentum( const ::Oyster::Math::Float4x4 &localI ); + void SetMass_KeepVelocity( ::Oyster::Math::Float m ); + void SetMass_KeepMomentum( ::Oyster::Math::Float m ); + void SetCenter( const ::Oyster::Math::Float3 &worldPos ); + void SetRotation( const ::Oyster::Math::Float4x4 &rotation ); + void SetOrientation( const ::Oyster::Math::Float4x4 &orientation ); + void SetSize( const ::Oyster::Math::Float3 &size ); + + private: + ::Oyster::Physics3D::RigidBody previous, current; + ::Oyster::Math::Float3 gravityNormal; + bool subscribeCollision, ignoreGravity; + ::Oyster::Collision3D::Sphere body; + }; +} } + +#endif \ No newline at end of file From 36741de19b3310a8e8411a4179f2ccb7c49e9142 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Wed, 27 Nov 2013 15:20:29 +0100 Subject: [PATCH 3/6] Evil Incompotent Compiler fix Sometimes the compiler decides to not compile constructors properly. :( Edited all constructors in Collision3D to reduce chance of this. --- Code/OysterPhysics3D/Box.cpp | 18 +++++++++++------ Code/OysterPhysics3D/BoxAxisAligned.cpp | 26 ++++++++++++++++++++----- Code/OysterPhysics3D/Frustrum.cpp | 19 +++++++++++++----- Code/OysterPhysics3D/ICollideable.cpp | 4 +--- Code/OysterPhysics3D/Line.cpp | 21 +++++++++++++++++--- Code/OysterPhysics3D/Plane.cpp | 14 +++++++++++-- Code/OysterPhysics3D/Point.cpp | 16 ++++++++++++--- Code/OysterPhysics3D/Ray.cpp | 20 ++++++++++++++++--- Code/OysterPhysics3D/Sphere.cpp | 14 +++++++++++-- 9 files changed, 120 insertions(+), 32 deletions(-) diff --git a/Code/OysterPhysics3D/Box.cpp b/Code/OysterPhysics3D/Box.cpp index e9f04b04..382c6732 100644 --- a/Code/OysterPhysics3D/Box.cpp +++ b/Code/OysterPhysics3D/Box.cpp @@ -8,13 +8,19 @@ using namespace ::Oyster::Collision3D; using namespace ::Oyster::Math3D; -Box::Box( ) - : ICollideable(Type_box), rotation(Float4x4::identity), center(0.0f), boundingOffset(0.5f) -{} +Box::Box( ) : ICollideable(Type_box) +{ + this->rotation = Float4x4::identity; + this->center =0.0f; + this->boundingOffset = Float3(0.5f); +} -Box::Box( const Float4x4 &r, const Float3 &p, const Float3 &s ) - : ICollideable(Type_box), rotation(r), center(p), boundingOffset(s*0.5) -{} +Box::Box( const Float4x4 &r, const Float3 &p, const Float3 &s ) : ICollideable(Type_box) +{ + this->rotation = r; + this->center = p; + this->boundingOffset = Float3(s*0.5); +} Box::~Box( ) {} diff --git a/Code/OysterPhysics3D/BoxAxisAligned.cpp b/Code/OysterPhysics3D/BoxAxisAligned.cpp index bdd9d0a9..f50f4b6d 100644 --- a/Code/OysterPhysics3D/BoxAxisAligned.cpp +++ b/Code/OysterPhysics3D/BoxAxisAligned.cpp @@ -8,10 +8,24 @@ using namespace ::Oyster::Collision3D; using namespace ::Oyster::Math3D; -BoxAxisAligned::BoxAxisAligned( ) : ICollideable(Type_box_axis_aligned), minVertex(-0.5f,-0.5f,-0.5f), maxVertex(0.5f,0.5f,0.5f) {} -BoxAxisAligned::BoxAxisAligned( const Float3 &_minVertex, const Float3 &_maxVertex ) : ICollideable(Type_box_axis_aligned), minVertex(_minVertex), maxVertex(_maxVertex) {} -BoxAxisAligned::BoxAxisAligned( const Float &leftClip, const Float &rightClip, const Float &topClip, const Float &bottomClip, const Float &nearClip, const Float &farClip ) - : ICollideable(Type_box_axis_aligned), minVertex(leftClip, bottomClip, nearClip), maxVertex(rightClip, topClip, farClip) {} +BoxAxisAligned::BoxAxisAligned( ) : ICollideable(Type_box_axis_aligned) +{ + this->minVertex = Float3(-0.5f,-0.5f,-0.5f ); + this->maxVertex = Float3( 0.5f, 0.5f, 0.5f ); +} + +BoxAxisAligned::BoxAxisAligned( const Float3 &_minVertex, const Float3 &_maxVertex ) : ICollideable(Type_box_axis_aligned) +{ + this->minVertex = _minVertex; + this->maxVertex = _maxVertex; +} + +BoxAxisAligned::BoxAxisAligned( const Float &leftClip, const Float &rightClip, const Float &topClip, const Float &bottomClip, const Float &nearClip, const Float &farClip ) : ICollideable(Type_box_axis_aligned) +{ + this->minVertex = Float3( leftClip, bottomClip, nearClip ); + this->maxVertex = Float3( rightClip, topClip, farClip ); +} + BoxAxisAligned::~BoxAxisAligned( ) {} BoxAxisAligned & BoxAxisAligned::operator = ( const BoxAxisAligned &box ) @@ -22,7 +36,9 @@ BoxAxisAligned & BoxAxisAligned::operator = ( const BoxAxisAligned &box ) } ::Utility::DynamicMemory::UniquePointer BoxAxisAligned::Clone( ) const -{ return ::Utility::DynamicMemory::UniquePointer( new BoxAxisAligned(*this) ); } +{ + return ::Utility::DynamicMemory::UniquePointer( new BoxAxisAligned(*this) ); +} bool BoxAxisAligned::Intersects( const ICollideable &target ) const { diff --git a/Code/OysterPhysics3D/Frustrum.cpp b/Code/OysterPhysics3D/Frustrum.cpp index a826a4e0..7099c2ce 100644 --- a/Code/OysterPhysics3D/Frustrum.cpp +++ b/Code/OysterPhysics3D/Frustrum.cpp @@ -74,13 +74,22 @@ namespace PrivateStatic } } -Frustrum::Frustrum() : ICollideable(Type_frustrum), - leftPlane(Float3::standard_unit_x, -0.5f), rightPlane(-Float3::standard_unit_x, 0.5f), - bottomPlane(Float3::standard_unit_y, -0.5f), topPlane(-Float3::standard_unit_y, 0.5f), - nearPlane(Float3::standard_unit_z, -0.5f), farPlane(-Float3::standard_unit_z, 0.5f) {} +Frustrum::Frustrum() : ICollideable(Type_frustrum) +{ + this->leftPlane = Plane( Float3::standard_unit_x, -0.5f ); + this->rightPlane = Plane(-Float3::standard_unit_x, 0.5f ), + this->bottomPlane = Plane( Float3::standard_unit_y, -0.5f ); + this->topPlane = Plane(-Float3::standard_unit_y, 0.5f ); + this->nearPlane = Plane( Float3::standard_unit_z, -0.5f ); + this->farPlane = Plane(-Float3::standard_unit_z, 0.5f ); +} Frustrum::Frustrum( const Float4x4 &vp ) : ICollideable(Type_frustrum) -{ PrivateStatic::VP_ToPlanes( this->leftPlane, this->rightPlane, this->bottomPlane, this->topPlane, this->nearPlane, this->farPlane, vp ); } +{ + PrivateStatic::VP_ToPlanes( this->leftPlane, this->rightPlane, this->bottomPlane, + this->topPlane, this->nearPlane, this->farPlane, + vp ); +} Frustrum::~Frustrum() {} diff --git a/Code/OysterPhysics3D/ICollideable.cpp b/Code/OysterPhysics3D/ICollideable.cpp index 89effa36..b0af99e4 100644 --- a/Code/OysterPhysics3D/ICollideable.cpp +++ b/Code/OysterPhysics3D/ICollideable.cpp @@ -6,7 +6,5 @@ using namespace ::Oyster::Collision3D; -ICollideable::ICollideable( Type _type ) - : type(_type) {} - +ICollideable::ICollideable( Type _type ) : type(_type) {} ICollideable::~ICollideable() {} \ No newline at end of file diff --git a/Code/OysterPhysics3D/Line.cpp b/Code/OysterPhysics3D/Line.cpp index 6258c9d9..17fe55f7 100644 --- a/Code/OysterPhysics3D/Line.cpp +++ b/Code/OysterPhysics3D/Line.cpp @@ -8,9 +8,24 @@ using namespace ::Oyster::Collision3D; using namespace ::Oyster::Math3D; -Line::Line( ) : ICollideable(Type_line), ray(), length(0.0f) {} -Line::Line( const class Ray &_ray, const Float &_length ) : ICollideable(Type_line), ray(_ray), length(_length) {} -Line::Line( const Float3 &origin, const Float3 &normalizedDirection, const Float &_length ) : ICollideable(Type_line), ray(origin, normalizedDirection), length(_length) {} +Line::Line( ) : ICollideable(Type_line) +{ + this->ray = Ray(); + this->length = 0.0f; +} + +Line::Line( const class Ray &_ray, const Float &_length ) : ICollideable(Type_line) +{ + this->ray = _ray; + this->length = _length; +} + +Line::Line( const Float3 &origin, const Float3 &normalizedDirection, const Float &_length ) : ICollideable(Type_line) +{ + this->ray = Ray( origin, normalizedDirection ); + this->length = _length; +} + Line::~Line( ) {} Line & Line::operator = ( const Line &line ) diff --git a/Code/OysterPhysics3D/Plane.cpp b/Code/OysterPhysics3D/Plane.cpp index 48a76883..a1a16b14 100644 --- a/Code/OysterPhysics3D/Plane.cpp +++ b/Code/OysterPhysics3D/Plane.cpp @@ -8,8 +8,18 @@ using namespace ::Oyster::Collision3D; using namespace ::Oyster::Math; -Plane::Plane( ) : ICollideable(Type_plane), normal(), phasing(0.0f) {} -Plane::Plane( const Float3 &n, const Float &p ) : ICollideable(Type_plane), normal(n), phasing(p) {} +Plane::Plane( ) : ICollideable(Type_plane) +{ + this->normal = Float3::standard_unit_z; + this->phasing = 0.0f; +} + +Plane::Plane( const Float3 &n, const Float &p ) : ICollideable(Type_plane) +{ + this->normal = n; + this->phasing = p; +} + Plane::~Plane( ) {} Plane & Plane::operator = ( const Plane &plane ) diff --git a/Code/OysterPhysics3D/Point.cpp b/Code/OysterPhysics3D/Point.cpp index a100359b..cf1186c9 100644 --- a/Code/OysterPhysics3D/Point.cpp +++ b/Code/OysterPhysics3D/Point.cpp @@ -8,8 +8,16 @@ using namespace ::Oyster::Collision3D; using namespace ::Oyster::Math3D; -Point::Point( ) : ICollideable(Type_point), center() {} -Point::Point( const Float3 &pos ) : ICollideable(Type_point), center(pos) {} +Point::Point( ) : ICollideable(Type_point) +{ + this->center = Float3::null; +} + +Point::Point( const Float3 &pos ) : ICollideable(Type_point) +{ + this->center = pos; +} + Point::~Point( ) {} Point & Point::operator = ( const Point &point ) @@ -19,7 +27,9 @@ Point & Point::operator = ( const Point &point ) } ::Utility::DynamicMemory::UniquePointer Point::Clone( ) const -{ return ::Utility::DynamicMemory::UniquePointer( new Point(*this) ); } +{ + return ::Utility::DynamicMemory::UniquePointer( new Point(*this) ); +} bool Point::Intersects( const ICollideable &target ) const { diff --git a/Code/OysterPhysics3D/Ray.cpp b/Code/OysterPhysics3D/Ray.cpp index 6d501335..09fb13ec 100644 --- a/Code/OysterPhysics3D/Ray.cpp +++ b/Code/OysterPhysics3D/Ray.cpp @@ -8,8 +8,20 @@ using namespace ::Oyster::Collision3D; using namespace ::Oyster::Math3D; -Ray::Ray( ) : ICollideable(Type_ray), origin(), direction(), collisionDistance(0.0f) {} -Ray::Ray( const Float3 &o, const ::Oyster::Math::Float3 &d ) : ICollideable(Type_ray), origin(o), direction(d), collisionDistance(0.0f) {} +Ray::Ray( ) : ICollideable(Type_ray) +{ + this->origin = Float3::null; + this->direction = Float3::standard_unit_z; + this->collisionDistance = 0.0f; +} + +Ray::Ray( const Float3 &o, const ::Oyster::Math::Float3 &d ) : ICollideable(Type_ray) +{ + this->origin = o; + this->direction = d; + this->collisionDistance = 0.0f; +} + Ray::~Ray( ) {} Ray & Ray::operator = ( const Ray &ray ) @@ -20,7 +32,9 @@ Ray & Ray::operator = ( const Ray &ray ) } ::Utility::DynamicMemory::UniquePointer Ray::Clone( ) const -{ return ::Utility::DynamicMemory::UniquePointer( new Ray(*this) ); } +{ + return ::Utility::DynamicMemory::UniquePointer( new Ray(*this) ); +} bool Ray::Intersects( const ICollideable &target ) const { diff --git a/Code/OysterPhysics3D/Sphere.cpp b/Code/OysterPhysics3D/Sphere.cpp index e30ad384..0cc8bc3b 100644 --- a/Code/OysterPhysics3D/Sphere.cpp +++ b/Code/OysterPhysics3D/Sphere.cpp @@ -4,8 +4,18 @@ using namespace ::Oyster::Collision3D; using namespace ::Oyster::Math; -Sphere::Sphere( ) : ICollideable(Type_sphere), center(), radius(0.0f) { } -Sphere::Sphere( const Float3 &_position, const Float &_radius ) : ICollideable(Type_sphere), center(_position), radius(_radius) {} +Sphere::Sphere( ) : ICollideable(Type_sphere) +{ + this->center = Float3::null; + this->radius = 0.0f; +} + +Sphere::Sphere( const Float3 &_position, const Float &_radius ) : ICollideable(Type_sphere) +{ + this->center = _position; + this->radius = _radius; +} + Sphere::~Sphere( ) {} Sphere & Sphere::operator = ( const Sphere &sphere ) From 6ba10d48c66fbcfead581c206790b89abd678173 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Wed, 27 Nov 2013 15:24:09 +0100 Subject: [PATCH 4/6] Some matrix constructors made explicit --- Code/OysterMath/Matrix.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Code/OysterMath/Matrix.h b/Code/OysterMath/Matrix.h index c5c8735e..c7b6b9e0 100644 --- a/Code/OysterMath/Matrix.h +++ b/Code/OysterMath/Matrix.h @@ -29,7 +29,7 @@ namespace LinearAlgebra Matrix2x2( ); Matrix2x2( const ScalarType &m11, const ScalarType &m12, const ScalarType &m21, const ScalarType &m22 ); - Matrix2x2( const Vector2 vec[2] ); + explicit Matrix2x2( const Vector2 vec[2] ); Matrix2x2( const Vector2 &vec1, const Vector2 &vec2 ); explicit Matrix2x2( const ScalarType element[4] ); Matrix2x2( const Matrix2x2 &matrix ); @@ -80,7 +80,7 @@ namespace LinearAlgebra Matrix3x3( const ScalarType &m11, const ScalarType &m12, const ScalarType &m13, const ScalarType &m21, const ScalarType &m22, const ScalarType &m23, const ScalarType &m31, const ScalarType &m32, const ScalarType &m33 ); - Matrix3x3( const Vector3 vec[3] ); + explicit Matrix3x3( const Vector3 vec[3] ); Matrix3x3( const Vector3 &vec1, const Vector3 &vec2, const Vector3 &vec3 ); explicit Matrix3x3( const ScalarType element[9] ); Matrix3x3( const Matrix3x3 &matrix ); @@ -132,7 +132,7 @@ namespace LinearAlgebra const ScalarType &m21, const ScalarType &m22, const ScalarType &m23, const ScalarType &m24, const ScalarType &m31, const ScalarType &m32, const ScalarType &m33, const ScalarType &m34, const ScalarType &m41, const ScalarType &m42, const ScalarType &m43, const ScalarType &m44 ); - Matrix4x4( const Vector4 vec[4] ); + explicit Matrix4x4( const Vector4 vec[4] ); Matrix4x4( const Vector4 &vec1, const Vector4 &vec2, const Vector4 &vec3, const Vector4 &vec4 ); explicit Matrix4x4( const ScalarType element[16] ); Matrix4x4( const Matrix4x4 &matrix ); From 3a444aaae847b0f4ed6e7d30151cad0a22ff106e Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Thu, 28 Nov 2013 10:24:32 +0100 Subject: [PATCH 5/6] Moved Octree.h into the Implementation Filter --- Code/GamePhysics/GamePhysics.vcxproj.filters | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Code/GamePhysics/GamePhysics.vcxproj.filters b/Code/GamePhysics/GamePhysics.vcxproj.filters index 4c0fd5f3..cc9a84c8 100644 --- a/Code/GamePhysics/GamePhysics.vcxproj.filters +++ b/Code/GamePhysics/GamePhysics.vcxproj.filters @@ -33,6 +33,9 @@ Header Files\Implementation + + Header Files\Implementation + From 07e3aa169741a3e50ad79bfa7df2b95e8fe8a090 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Thu, 28 Nov 2013 10:26:29 +0100 Subject: [PATCH 6/6] Improved Collision Event Subscription From now on, will each ICustomBody instance have it's own collisionAction function. --- .../Implementation/PhysicsAPI_Impl.cpp | 43 +++++++++++-------- .../Implementation/PhysicsAPI_Impl.h | 11 +++-- .../Implementation/SimpleRigidBody.cpp | 26 +++++------ .../Implementation/SimpleRigidBody.h | 6 +-- .../Implementation/SphericalRigidBody.cpp | 26 +++++------ .../Implementation/SphericalRigidBody.h | 5 ++- Code/GamePhysics/PhysicsAPI.h | 39 ++++++++--------- 7 files changed, 86 insertions(+), 70 deletions(-) diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index 6c3e4a27..13184ce5 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -10,14 +10,6 @@ using namespace ::Utility::DynamicMemory; API_Impl API_instance; -// default API::EventAction_Collision -void defaultCollisionAction( const ICustomBody *proto, const ICustomBody *deuter ) -{ /* do nothing */ } - -// default API::EventAction_Destruction -void defaultDestructionAction( UniquePointer proto ) -{ /* do nothing besides proto auto deleting itself. */ } - Float4x4 & MomentOfInertia::CreateSphereMatrix( const Float mass, const Float radius) { return Formula::MomentOfInertia::Sphere(mass, radius); @@ -51,8 +43,7 @@ API & API::Instance() API_Impl::API_Impl() : gravityConstant( Constant::gravity_constant ), updateFrameLength( 1.0f / 120.0f ), - collisionAction( defaultCollisionAction ), - destructionAction( defaultDestructionAction ) + destructionAction( Default::EventAction_Destruction ) {} API_Impl::~API_Impl() {} @@ -66,17 +57,22 @@ void API_Impl::SetDeltaTime( float deltaTime ) { updateFrameLength = deltaTime; } + void API_Impl::SetGravityConstant( float g ) { this->gravityConstant = g; } -void API_Impl::SetAction( API::EventAction_Collision functionPointer ) + +void API_Impl::SetSubscription( API::EventAction_Destruction functionPointer ) { - this->collisionAction = functionPointer; -} -void API_Impl::SetAction( API::EventAction_Destruction functionPointer ) -{ - this->destructionAction = functionPointer; + if( functionPointer ) + { + this->destructionAction = functionPointer; + } + else + { + this->destructionAction = Default::EventAction_Destruction; + } } void API_Impl::Update() @@ -168,4 +164,17 @@ void API_Impl::SetSize( const ICustomBody* objRef, const Float3 &size ) UniquePointer API_Impl::CreateSimpleRigidBody() const { return new SimpleRigidBody(); -} \ No newline at end of file +} + +namespace Oyster { namespace Physics { namespace Default +{ + + void EventAction_Destruction( ::Utility::DynamicMemory::UniquePointer<::Oyster::Physics::ICustomBody> proto ) + { /* Do nothing except allowing the proto uniquePointer destroy itself. */ } + + ::Oyster::Physics::ICustomBody::SubscriptMessage EventAction_Collision( const ::Oyster::Physics::ICustomBody *proto, const ::Oyster::Physics::ICustomBody *deuter ) + { /* Do nothing except returning business as usual. */ + return ::Oyster::Physics::ICustomBody::SubscriptMessage_none; + } + +} } } \ No newline at end of file diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h index 5a2e3ef6..28858da0 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h @@ -17,8 +17,7 @@ namespace Oyster void SetDeltaTime( float deltaTime ); void SetGravityConstant( float g ); - void SetAction( EventAction_Collision functionPointer ); - void SetAction( EventAction_Destruction functionPointer ); + void SetSubscription( EventAction_Destruction functionPointer ); void Update(); @@ -45,11 +44,15 @@ namespace Oyster ::Utility::DynamicMemory::UniquePointer CreateSimpleRigidBody() const; private: ::Oyster::Math::Float gravityConstant, updateFrameLength; - EventAction_Collision collisionAction; EventAction_Destruction destructionAction; }; - } + namespace Default + { + 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 ); + } + } } #endif \ No newline at end of file diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp index cc1f847b..95256915 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp @@ -10,7 +10,7 @@ using namespace ::Utility::Value; SimpleRigidBody::SimpleRigidBody() : previous(), current(), gravityNormal(0.0f), - subscribeCollision(true), + collisionAction(Default::EventAction_Collision), ignoreGravity(false) {} SimpleRigidBody::~SimpleRigidBody() {} @@ -20,11 +20,6 @@ UniquePointer SimpleRigidBody::Clone() const return new SimpleRigidBody( *this ); } -bool SimpleRigidBody::IsSubscribingCollisions() const -{ // Assumption - return this->subscribeCollision; -} - bool SimpleRigidBody::IsAffectedByGravity() const { return !this->ignoreGravity; @@ -92,7 +87,19 @@ UpdateState SimpleRigidBody::Update( Float timeStepLength ) this->current.Update_LeapFrog( timeStepLength ); // compare previous and new state and return result - return this->current == this->previous ? resting : altered; + return this->current == this->previous ? UpdateState_resting : UpdateState_altered; +} + +void SimpleRigidBody::SetSubscription( ICustomBody::EventAction_Collision functionPointer ) +{ + if( functionPointer ) + { + this->collisionAction = functionPointer; + } + else + { + this->collisionAction = Default::EventAction_Collision; + } } void SimpleRigidBody::SetGravity( bool ignore) @@ -106,11 +113,6 @@ void SimpleRigidBody::SetGravityNormal( const Float3 &normalizedVector ) this->gravityNormal = normalizedVector; } -void SimpleRigidBody::SetSubscription( bool subscribeCollision ) -{ - this->subscribeCollision = subscribeCollision; -} - void SimpleRigidBody::SetMomentOfInertiaTensor_KeepVelocity( const Float4x4 &localI ) { this->current.SetMomentOfInertia_KeepVelocity( localI ); diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.h b/Code/GamePhysics/Implementation/SimpleRigidBody.h index 41be919b..c19a1ddb 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.h +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.h @@ -14,7 +14,6 @@ namespace Oyster { namespace Physics ::Utility::DynamicMemory::UniquePointer Clone() const; - bool IsSubscribingCollisions() const; bool IsAffectedByGravity() const; bool Intersects( const ICustomBody &object, ::Oyster::Math::Float timeStepLength, ::Oyster::Math::Float &deltaWhen, ::Oyster::Math::Float3 &worldPointOfContact ) const; bool Intersects( const ::Oyster::Collision3D::ICollideable &shape ) const; @@ -29,9 +28,9 @@ namespace Oyster { namespace Physics UpdateState Update( ::Oyster::Math::Float timeStepLength ); + void SetSubscription( EventAction_Collision functionPointer ); void SetGravity( bool ignore); void SetGravityNormal( const ::Oyster::Math::Float3 &normalizedVector ); - void SetSubscription( bool subscribeCollision ); void SetMomentOfInertiaTensor_KeepVelocity( const ::Oyster::Math::Float4x4 &localI ); void SetMomentOfInertiaTensor_KeepMomentum( const ::Oyster::Math::Float4x4 &localI ); void SetMass_KeepVelocity( ::Oyster::Math::Float m ); @@ -44,7 +43,8 @@ namespace Oyster { namespace Physics private: ::Oyster::Physics3D::RigidBody previous, current; ::Oyster::Math::Float3 gravityNormal; - bool subscribeCollision, ignoreGravity; + EventAction_Collision collisionAction; + bool ignoreGravity; }; } } diff --git a/Code/GamePhysics/Implementation/SphericalRigidBody.cpp b/Code/GamePhysics/Implementation/SphericalRigidBody.cpp index 20d806c9..6d50b386 100644 --- a/Code/GamePhysics/Implementation/SphericalRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SphericalRigidBody.cpp @@ -10,7 +10,7 @@ using namespace ::Utility::Value; SphericalRigidBody::SphericalRigidBody() : previous(), current( Box(Float4x4::identity, Float3::null, Float3(1.0f)) ), gravityNormal( 0.0f ), - subscribeCollision( true ), + collisionAction(Default::EventAction_Collision), ignoreGravity( false ), body( Float3::null, 0.5f ) {} @@ -21,11 +21,6 @@ UniquePointer SphericalRigidBody::Clone() const return new SphericalRigidBody( *this ); } -bool SphericalRigidBody::IsSubscribingCollisions() const -{ // Assumption - return this->subscribeCollision; -} - bool SphericalRigidBody::IsAffectedByGravity() const { return !this->ignoreGravity; @@ -94,7 +89,19 @@ UpdateState SphericalRigidBody::Update( Float timeStepLength ) this->body.center = this->current.GetCenter(); // compare previous and new state and return result - return this->current == this->previous ? resting : altered; + return this->current == this->previous ? UpdateState_resting : UpdateState_altered; +} + +void SphericalRigidBody::SetSubscription( ICustomBody::EventAction_Collision functionPointer ) +{ + if( functionPointer ) + { + this->collisionAction = functionPointer; + } + else + { + this->collisionAction = Default::EventAction_Collision; + } } void SphericalRigidBody::SetGravity( bool ignore) @@ -108,11 +115,6 @@ void SphericalRigidBody::SetGravityNormal( const Float3 &normalizedVector ) this->gravityNormal = normalizedVector; } -void SphericalRigidBody::SetSubscription( bool subscribeCollision ) -{ - this->subscribeCollision = subscribeCollision; -} - void SphericalRigidBody::SetMomentOfInertiaTensor_KeepVelocity( const Float4x4 &localI ) { this->current.SetMomentOfInertia_KeepVelocity( localI ); diff --git a/Code/GamePhysics/Implementation/SphericalRigidBody.h b/Code/GamePhysics/Implementation/SphericalRigidBody.h index 7a727ee8..1540f7a1 100644 --- a/Code/GamePhysics/Implementation/SphericalRigidBody.h +++ b/Code/GamePhysics/Implementation/SphericalRigidBody.h @@ -30,9 +30,9 @@ namespace Oyster { namespace Physics UpdateState Update( ::Oyster::Math::Float timeStepLength ); + void SetSubscription( EventAction_Collision functionPointer ); void SetGravity( bool ignore); void SetGravityNormal( const ::Oyster::Math::Float3 &normalizedVector ); - void SetSubscription( bool subscribeCollision ); void SetMomentOfInertiaTensor_KeepVelocity( const ::Oyster::Math::Float4x4 &localI ); void SetMomentOfInertiaTensor_KeepMomentum( const ::Oyster::Math::Float4x4 &localI ); void SetMass_KeepVelocity( ::Oyster::Math::Float m ); @@ -45,7 +45,8 @@ namespace Oyster { namespace Physics private: ::Oyster::Physics3D::RigidBody previous, current; ::Oyster::Math::Float3 gravityNormal; - bool subscribeCollision, ignoreGravity; + EventAction_Collision collisionAction; + bool ignoreGravity; ::Oyster::Collision3D::Sphere body; }; } } diff --git a/Code/GamePhysics/PhysicsAPI.h b/Code/GamePhysics/PhysicsAPI.h index bf80e6fa..71bebc1f 100644 --- a/Code/GamePhysics/PhysicsAPI.h +++ b/Code/GamePhysics/PhysicsAPI.h @@ -19,8 +19,8 @@ namespace Oyster enum UpdateState { - resting, - altered + UpdateState_resting, + UpdateState_altered }; namespace Constant @@ -45,7 +45,6 @@ namespace Oyster class PHYSICS_DLL_USAGE API { public: - typedef void (*EventAction_Collision)( const ICustomBody *proto, const ICustomBody *deuter ); typedef void (*EventAction_Destruction)( ::Utility::DynamicMemory::UniquePointer proto ); /** Gets the Physics instance. */ @@ -70,20 +69,15 @@ namespace Oyster * @param g: Default is the real world Constant::gravity_constant [N(m/kg)^2] ********************************************************/ virtual void SetGravityConstant( float g ) = 0; - - /******************************************************** - * Sets the function that will be called by the engine - * whenever a subscribed collision occurs. - ********************************************************/ - virtual void SetAction( EventAction_Collision functionPointer ) = 0; /******************************************************** * Sets the function that will be called by the engine * whenever an object is being destroyed for some reason. * - Because DestroyObject(...) were called. * - Out of memory forced engine to destroy an object. + * @param functionPointer: If NULL, an empty default function will be set. ********************************************************/ - virtual void SetAction( EventAction_Destruction functionPointer ) = 0; + virtual void SetSubscription( EventAction_Destruction functionPointer ) = 0; /******************************************************** * Triggers the engine to run next update frame. @@ -225,6 +219,14 @@ namespace Oyster class PHYSICS_DLL_USAGE ICustomBody { public: + enum SubscriptMessage + { + SubscriptMessage_none, + SubscriptMessage_ignore_collision_response + }; + + typedef SubscriptMessage (*EventAction_Collision)( const ICustomBody *proto, const ICustomBody *deuter ); + virtual ~ICustomBody() {}; /******************************************************** @@ -232,11 +234,6 @@ namespace Oyster * @return An ICustomBody pointer along with the responsibility to delete. ********************************************************/ virtual ::Utility::DynamicMemory::UniquePointer Clone() const = 0; - - /******************************************************** - * @return true if Engine should call the EventAction_Collision function. - ********************************************************/ - virtual bool IsSubscribingCollisions() const = 0; /******************************************************** * @return true if Engine should apply gravity on this object. @@ -312,6 +309,13 @@ namespace Oyster ********************************************************/ virtual UpdateState Update( ::Oyster::Math::Float timeStepLength ) = 0; + /******************************************************** + * Sets the function that will be called by the engine + * whenever a collision occurs. + * @param functionPointer: If NULL, an empty default function will be set. + ********************************************************/ + virtual void SetSubscription( EventAction_Collision functionPointer ) = 0; + /******************************************************** * @param ignore: True if Engine should not apply Gravity. ********************************************************/ @@ -323,11 +327,6 @@ namespace Oyster ********************************************************/ virtual void SetGravityNormal( const ::Oyster::Math::Float3 &normalizedVector ) = 0; - /******************************************************** - * @param subscribeCollision: If is true, engine will call EventAction_Collision when this collides. - ********************************************************/ - virtual void SetSubscription( bool subscribeCollision ) = 0; - /******************************************************** * To not be called if is in Engine * Use API::SetMomentOfInertiaTensor_KeepVelocity(...) instead