From ac1fb8bab92c75199c5abc248aaedf4e6fcddb3e Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Wed, 13 Nov 2013 12:07:21 +0100 Subject: [PATCH 1/7] UniquePointer and UniqueArray UniquePointer - removed hazardous members. UniqueArray - added All Documented --- Misc/Misc.vcxproj | 1 + Misc/Misc.vcxproj.filters | 3 + Misc/Utilities-InlineImpl.h | 168 ++++++++++++++++++++++++++++++++++++ Misc/Utilities.h | 140 +++++++++++++++--------------- 4 files changed, 240 insertions(+), 72 deletions(-) create mode 100644 Misc/Utilities-InlineImpl.h diff --git a/Misc/Misc.vcxproj b/Misc/Misc.vcxproj index 76714140..58c71f07 100644 --- a/Misc/Misc.vcxproj +++ b/Misc/Misc.vcxproj @@ -142,6 +142,7 @@ + diff --git a/Misc/Misc.vcxproj.filters b/Misc/Misc.vcxproj.filters index 9f5237ac..6f633e1d 100644 --- a/Misc/Misc.vcxproj.filters +++ b/Misc/Misc.vcxproj.filters @@ -29,5 +29,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/Misc/Utilities-InlineImpl.h b/Misc/Utilities-InlineImpl.h new file mode 100644 index 00000000..93b780ec --- /dev/null +++ b/Misc/Utilities-InlineImpl.h @@ -0,0 +1,168 @@ +///////////////////////////////////////////////////////////////////// +// Inline and template implementations for +// the Utility Collection of Miscellanious Handy Functions +// © Dan Andersson 2013 +///////////////////////////////////////////////////////////////////// + +#ifndef UTILITIES_INLINE_IMPL_H +#define UTILITIES_INLINE_IMPL_H + +#include "Utilities.h" + +namespace Utility +{ + namespace DynamicMemory + { + template + inline void SafeDeleteInstance( Type *dynamicInstance ) + { + if( dynamicInstance ) + { + delete dynamicInstance; + } + } + + template + void SafeDeleteArray( Type dynamicArray[] ) + { + if( dynamicArray ) + { + delete [] dynamicArray; + } + } + + template + UniquePointer::UniquePointer( Type *assignedInstance ) + { + this->ownedInstance = assignedInstance; + } + + template + UniquePointer::~UniquePointer() + { + SafeDeleteInstance( this->ownedInstance ); + } + + template + UniquePointer & UniquePointer::operator = ( Type *assignedInstance ) + { + SafeDeleteInstance( this->ownedInstance ); + this->ownedInstance = assignedInstance; + return *this; + } + + template + UniquePointer & UniquePointer::operator = ( const UniquePointer &donor ) + { + SafeDeleteInstance( this->ownedInstance ); + this->ownedInstance = donor.ownedInstance; + donor.ownedInstance = NULL; + return *this; + } + + template + UniquePointer::operator Type* () + { + return this->ownedInstance; + } + + template + UniquePointer::operator const Type* () const + { + return this->ownedInstance; + } + + template + Type * UniquePointer::operator -> () + { + return this->ownedInstance; + } + + template + const Type * UniquePointer::operator -> () const + { + return this->ownedInstance; + } + + template + UniquePointer::operator bool() const + { + return this->ownedInstance != NULL; + } + + template + Type* UniquePointer::Release() + { + Type *copy = this->ownedInstance; + this->ownedInstance = NULL; + return copy; + } + + template + inline bool UniquePointer::HaveOwnership() const + { + return this->operator bool(); + } + + template + UniqueArray::UniqueArray( Type assignedArray[] ) + { + this->ownedArray = assignedArray; + } + + template + UniqueArray::~UniqueArray() + { + SafeDeleteArray( this->ownedArray ); + } + + template + UniqueArray & UniqueArray::operator = ( Type assignedArray[] ) + { + SafeDeleteArray( this->ownedArray ); + this->ownedArray = assignedArray; + } + + template + UniqueArray & UniqueArray::operator = ( const UniquePointer &donor ) + { + SafeDeleteArray( this->ownedArray ); + this->ownedArray = donor.ownedInstance; + donor.owned = NULL; + } + + template template + template Type & UniqueArray::operator [] ( Index i ) + { + return this->ownedArray[i]; + } + + template template + template const Type & UniqueArray::operator [] ( Index i ) const + { + return this->ownedArray[i]; + } + + template + UniqueArray::operator bool () const + { + return this->ownedArray != NULL; + } + + template + Type* UniqueArray::Release() + { + Type *copy = this->ownedArray; + this->ownedArray = NULL; + return copy; + } + + template + inline bool UniqueArray::HaveOwnership() const + { + return this->operator bool(); + } + } +} + +#endif \ No newline at end of file diff --git a/Misc/Utilities.h b/Misc/Utilities.h index ba6e0c3a..91285cde 100644 --- a/Misc/Utilities.h +++ b/Misc/Utilities.h @@ -3,7 +3,6 @@ // © Dan Andersson 2013 ///////////////////////////////////////////////////////////////////// -#pragma once #ifndef UTILITIES_H #define UTILITIES_H @@ -15,100 +14,97 @@ namespace Utility { - namespace Memory + namespace DynamicMemory { + /// If dynamicInstance is not NULL, then delete + template void SafeDeleteInstance( Type *dynamicInstance ); + + /// If dynamicArray is not NULL, then delete [] + template void SafeDeleteArray( Type dynamicArray[] ); + template struct UniquePointer - { + { /// Wrapper to safely transfer dynamic ownership/responsibility public: - UniquePointer( Type *assignedMemory = NULL ); + /// Assigns assignedInstance ownership to this UniquePonter, old owned instance will be deleted. + /// If NULL is assigned is equavalent with clearing all responsibilities from this UniquePointer. + UniquePointer( Type *assignedInstance = NULL ); + + /// Will auto delete assigned dynamic instance. ~UniquePointer(); - UniquePointer & operator = ( Type *assignedMemory ); + /// Assigns assignedInstance ownership to this UniquePonter, old owned instance will be deleted. + /// If NULL is assigned is equavalent with clearing all responsibilities from this UniquePointer. + UniquePointer & operator = ( Type *assignedInstance ); + + /// Transfers assignedInstance ownership from donor to this UniquePonter, old owned instance will be deleted. + /// If donor had nothing, is equavalent with clearing all responsibilities from this UniquePointer. UniquePointer & operator = ( const UniquePointer &donor ); + /// Access the assigned dynamic instance. Will crash if nothing there operator Type* (); - operator const Type* () const; - Type * operator -> (); - const Type * operator -> () const; - template Type & operator [] ( Index i ); - template const Type & operator [] ( Index i ) const; + /// Access the assigned dynamic instance. Will crash if nothing there + operator const Type* () const; + + /// Access members of the assigned dynamic instance. Will crash if nothing there + Type * operator -> (); + + /// Access members of the assigned dynamic instance. Will crash if nothing there + const Type * operator -> () const; + + /// If true, this UniquePointer have a current ownership/responsibility of a dynamic instance. operator bool () const; + /// This UniquePointer drops all claims of ownership/responsibility and returns the dynamic instance. Now it is your responsibility to delete. Type* Release(); - bool haveOwnership() const; - + + /// (inline) If true, this UniquePointer have a current ownership/responsibility of a dynamic instance. + bool HaveOwnership() const; + private: - mutable Type *ownedMemory; + mutable Type *ownedInstance; }; - // IMPLEMENTATIONS //////////////////////////////////////////////// - template - UniquePointer::UniquePointer( Type *assignedMemory ) - { this->ownedMemory = assignedMemory; } + struct UniqueArray + { /// Wrapper to safely transfer dynamic ownership/responsibility + public: + /// Assigns assignedInstance ownership to this UniquePonter, old owned array will be deleted. + /// If NULL is assigned is equavalent with clearing all responsibilities from this UniqueArray. + UniqueArray( Type assignedArray[] = NULL ); + + /// Will auto delete assigned dynamic array. + ~UniqueArray(); - template - UniquePointer::~UniquePointer() - { if( this->ownedMemory ) delete this->ownedMemory; } + /// Assigns assignedInstance ownership to this UniquePonter, old owned array will be deleted. + /// If NULL is assigned is equavalent with clearing all responsibilities from this UniqueArray. + UniqueArray & operator = ( Type assignedArray[] ); + + /// Transfers assignedInstance ownership from donor to this UniquePonter, old owned array will be deleted. + /// If donor had nothing, is equavalent with clearing all responsibilities from this UniqueArray. + UniqueArray & operator = ( const UniqueArray &donor ); - template - UniquePointer & UniquePointer::operator = ( Type *assignedMemory ) - { - if( this->ownedPointer ) delete this->ownedMemory; - this->ownedMemory = assignedMemory; - return *this; - } + /// Accesses the instance at index i of this UniqeArray's owned dynamic array. + /// Will crash if out-of-bound or there is no assigned array. + template Type & operator [] ( Index i ); + + /// Accesses the instance at index i of this UniqeArray's owned dynamic array. + /// Will crash if out-of-bound or there is no assigned array. + template const Type & operator [] ( Index i ) const; - template - UniquePointer & UniquePointer::operator = ( const UniquePointer &donor ) - { - if( this->ownedMemory ) delete this->ownedMemory; - this->ownedMemory = donor.ownedMemory; - donor.ownedMemory = NULL; - return *this; - } + /// If true, this UniqueArray have a current ownership/responsibility of a dynamic instance. + operator bool () const; - template - UniquePointer::operator Type* () - { return this->ownedMemory; } + /// This UniqueArray drops all claims of ownership/responsibility and returns the dynamic array. Now it is your responsibility to delete. + Type* Release(); - template - UniquePointer::operator const Type* () const - { return this->ownedMemory; } + /// (inline) If true, this UniqueArray have a current ownership/responsibility of a dynamic array. + bool HaveOwnership() const; - template - Type * UniquePointer::operator -> () - { return this->ownedMemory; } - - template - const Type * UniquePointer::operator -> () const - { return this->ownedMemory; } - - template template - Type & UniquePointer::operator [] ( Index i ) - { return this->ownedMemory[i]; } - - template template - const Type & UniquePointer::operator [] ( Index i ) const - { return this->ownedMemory[i]; } - - template - UniquePointer::operator bool() const - { return this->ownedMemory != NULL; } - - template - Type* UniquePointer::Release() - { - Type *copy = this->ownedMemory; - this->ownedMemory = NULL; - return copy; - } - - template - inline bool UniquePointer::haveOwnership() const - { return this->operator bool(); } + private: + mutable Type *ownedArray; + }; } namespace String From fa43650439df0aace4383df5d4433d55e3e6ac99 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Wed, 13 Nov 2013 13:56:25 +0100 Subject: [PATCH 2/7] Forgot to #include the inline implementation file For Utilities --- Misc/Utilities.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Misc/Utilities.h b/Misc/Utilities.h index 91285cde..950c3933 100644 --- a/Misc/Utilities.h +++ b/Misc/Utilities.h @@ -245,4 +245,6 @@ namespace Utility } } +#include "Utilities-InlineImpl.h" + #endif \ No newline at end of file From 893e6427a54b1145fa0d58651aecad2d8eba42fe Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Wed, 13 Nov 2013 14:48:24 +0100 Subject: [PATCH 3/7] UniquePointer and UniqeArray ready to use All compile errors fixed. Tested against Visual Leak detector. No leak detected! --- Misc/Utilities-InlineImpl.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Misc/Utilities-InlineImpl.h b/Misc/Utilities-InlineImpl.h index 93b780ec..dc047153 100644 --- a/Misc/Utilities-InlineImpl.h +++ b/Misc/Utilities-InlineImpl.h @@ -124,7 +124,7 @@ namespace Utility } template - UniqueArray & UniqueArray::operator = ( const UniquePointer &donor ) + UniqueArray & UniqueArray::operator = ( const UniqueArray &donor ) { SafeDeleteArray( this->ownedArray ); this->ownedArray = donor.ownedInstance; @@ -132,13 +132,13 @@ namespace Utility } template template - template Type & UniqueArray::operator [] ( Index i ) + Type & UniqueArray::operator [] ( Index i ) { return this->ownedArray[i]; } template template - template const Type & UniqueArray::operator [] ( Index i ) const + const Type & UniqueArray::operator [] ( Index i ) const { return this->ownedArray[i]; } From 19522d8ff65668fa953b1e17dad5caf885a9e1e1 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Wed, 13 Nov 2013 14:50:08 +0100 Subject: [PATCH 4/7] References in solution adjusted All references to ::Utility::Memory changed to ::Utility::DynamicMemory --- OysterPhysics3D/Box.cpp | 4 ++-- OysterPhysics3D/Box.h | 2 +- OysterPhysics3D/BoxAxisAligned.cpp | 4 ++-- OysterPhysics3D/BoxAxisAligned.h | 2 +- OysterPhysics3D/Frustrum.cpp | 4 ++-- OysterPhysics3D/Frustrum.h | 2 +- OysterPhysics3D/ICollideable.h | 2 +- OysterPhysics3D/Line.cpp | 4 ++-- OysterPhysics3D/Line.h | 2 +- OysterPhysics3D/OysterPhysics3D.h | 11 ++++++++++- OysterPhysics3D/Plane.cpp | 4 ++-- OysterPhysics3D/Plane.h | 2 +- OysterPhysics3D/Point.cpp | 4 ++-- OysterPhysics3D/Point.h | 2 +- OysterPhysics3D/Ray.cpp | 4 ++-- OysterPhysics3D/Ray.h | 2 +- OysterPhysics3D/RigidBody.h | 2 +- OysterPhysics3D/Sphere.cpp | 4 ++-- OysterPhysics3D/Sphere.h | 2 +- OysterPhysics3D/Universe.cpp | 2 +- OysterPhysics3D/Universe.h | 2 +- 21 files changed, 38 insertions(+), 29 deletions(-) diff --git a/OysterPhysics3D/Box.cpp b/OysterPhysics3D/Box.cpp index 9ba1bb28..a20b8195 100644 --- a/OysterPhysics3D/Box.cpp +++ b/OysterPhysics3D/Box.cpp @@ -19,8 +19,8 @@ Box & Box::operator = ( const Box &box ) return *this; } -::Utility::Memory::UniquePointer Box::Clone( ) const -{ return ::Utility::Memory::UniquePointer( new Box(*this) ); } +::Utility::DynamicMemory::UniquePointer Box::Clone( ) const +{ return ::Utility::DynamicMemory::UniquePointer( new Box(*this) ); } bool Box::Intersects( const ICollideable *target ) const { diff --git a/OysterPhysics3D/Box.h b/OysterPhysics3D/Box.h index be7b29ad..034639d0 100644 --- a/OysterPhysics3D/Box.h +++ b/OysterPhysics3D/Box.h @@ -33,7 +33,7 @@ namespace Oyster { namespace Collision3D Box & operator = ( const Box &box ); - virtual ::Utility::Memory::UniquePointer Clone( ) const; + virtual ::Utility::DynamicMemory::UniquePointer Clone( ) const; bool Intersects( const ICollideable *target ) const; bool Contains( const ICollideable *target ) const; }; diff --git a/OysterPhysics3D/BoxAxisAligned.cpp b/OysterPhysics3D/BoxAxisAligned.cpp index b745646f..c782ed25 100644 --- a/OysterPhysics3D/BoxAxisAligned.cpp +++ b/OysterPhysics3D/BoxAxisAligned.cpp @@ -21,8 +21,8 @@ BoxAxisAligned & BoxAxisAligned::operator = ( const BoxAxisAligned &box ) return *this; } -::Utility::Memory::UniquePointer BoxAxisAligned::Clone( ) const -{ return ::Utility::Memory::UniquePointer( new BoxAxisAligned(*this) ); } +::Utility::DynamicMemory::UniquePointer BoxAxisAligned::Clone( ) const +{ return ::Utility::DynamicMemory::UniquePointer( new BoxAxisAligned(*this) ); } bool BoxAxisAligned::Intersects( const ICollideable *target ) const { diff --git a/OysterPhysics3D/BoxAxisAligned.h b/OysterPhysics3D/BoxAxisAligned.h index e3c91722..9810c39a 100644 --- a/OysterPhysics3D/BoxAxisAligned.h +++ b/OysterPhysics3D/BoxAxisAligned.h @@ -27,7 +27,7 @@ namespace Oyster { namespace Collision3D BoxAxisAligned & operator = ( const BoxAxisAligned &box ); - virtual ::Utility::Memory::UniquePointer Clone( ) const; + virtual ::Utility::DynamicMemory::UniquePointer Clone( ) const; bool Intersects( const ICollideable *target ) const; bool Contains( const ICollideable *target ) const; }; diff --git a/OysterPhysics3D/Frustrum.cpp b/OysterPhysics3D/Frustrum.cpp index 837ba579..e4da0ce9 100644 --- a/OysterPhysics3D/Frustrum.cpp +++ b/OysterPhysics3D/Frustrum.cpp @@ -188,8 +188,8 @@ void Frustrum::WriteToByte( unsigned int &nextIndex, unsigned char targetMem[] ) nextIndex += 6 * ::Utility::StaticArray::NumElementsOf( this->plane[0].byte ); } -::Utility::Memory::UniquePointer Frustrum::Clone( ) const -{ return ::Utility::Memory::UniquePointer( new Frustrum(*this) ); } +::Utility::DynamicMemory::UniquePointer Frustrum::Clone( ) const +{ return ::Utility::DynamicMemory::UniquePointer( new Frustrum(*this) ); } bool Frustrum::Intersects( const ICollideable *target ) const { diff --git a/OysterPhysics3D/Frustrum.h b/OysterPhysics3D/Frustrum.h index a27a8f4a..c711c9af 100644 --- a/OysterPhysics3D/Frustrum.h +++ b/OysterPhysics3D/Frustrum.h @@ -37,7 +37,7 @@ namespace Oyster { namespace Collision3D void Split( Frustrum targetList[], unsigned int numX, unsigned int numY = 1U, unsigned int numZ = 1u ) const; /// DEPRECATED void WriteToByte( unsigned char targetMem[], unsigned int &nextIndex ) const; /// DEPRECATED - virtual ::Utility::Memory::UniquePointer Clone( ) const; + virtual ::Utility::DynamicMemory::UniquePointer Clone( ) const; bool Intersects( const ICollideable *target ) const; bool Contains( const ICollideable *target ) const; }; diff --git a/OysterPhysics3D/ICollideable.h b/OysterPhysics3D/ICollideable.h index b2a74aed..aa6c7891 100644 --- a/OysterPhysics3D/ICollideable.h +++ b/OysterPhysics3D/ICollideable.h @@ -33,7 +33,7 @@ namespace Oyster { namespace Collision3D /// Contains a collection of 3D shapes ICollideable( Type type = Type_undefined ); virtual ~ICollideable(); - virtual ::Utility::Memory::UniquePointer Clone( ) const = 0; + virtual ::Utility::DynamicMemory::UniquePointer Clone( ) const = 0; virtual bool Intersects( const ICollideable *target ) const = 0; virtual bool Contains( const ICollideable *target ) const = 0; }; diff --git a/OysterPhysics3D/Line.cpp b/OysterPhysics3D/Line.cpp index 2afa69d8..7093f6b2 100644 --- a/OysterPhysics3D/Line.cpp +++ b/OysterPhysics3D/Line.cpp @@ -20,8 +20,8 @@ Line & Line::operator = ( const Line &line ) return *this; } -::Utility::Memory::UniquePointer Line::Clone( ) const -{ return ::Utility::Memory::UniquePointer( new Line(*this) ); } +::Utility::DynamicMemory::UniquePointer Line::Clone( ) const +{ return ::Utility::DynamicMemory::UniquePointer( new Line(*this) ); } bool Line::Intersects( const ICollideable *target ) const { diff --git a/OysterPhysics3D/Line.h b/OysterPhysics3D/Line.h index b23ff58f..fecc0bd2 100644 --- a/OysterPhysics3D/Line.h +++ b/OysterPhysics3D/Line.h @@ -28,7 +28,7 @@ namespace Oyster { namespace Collision3D Line & operator = ( const Line &line ); - virtual ::Utility::Memory::UniquePointer Clone( ) const; + virtual ::Utility::DynamicMemory::UniquePointer Clone( ) const; bool Intersects( const ICollideable *target ) const; bool Contains( const ICollideable *target ) const; }; diff --git a/OysterPhysics3D/OysterPhysics3D.h b/OysterPhysics3D/OysterPhysics3D.h index e31aa17b..a5d58c0b 100644 --- a/OysterPhysics3D/OysterPhysics3D.h +++ b/OysterPhysics3D/OysterPhysics3D.h @@ -57,6 +57,15 @@ namespace Oyster { namespace Physics3D return ( momentOfInertia * ::Oyster::Math::Float4(angularVelocity, 0.0f) ).xyz; } + /****************************************************************** + * Returns the local angular momentum of a mass in rotation. + * @todo TODO: improve doc + ******************************************************************/ + inline ::Oyster::Math::Float3 AngularMomentum( const ::Oyster::Math::Float3 linearMomentum, const ::Oyster::Math::Float3 &offset ) + { + return offset.Cross( linearMomentum ); + } + /****************************************************************** * Returns the local tangential momentum at localPos, of a mass in rotation. * @todo TODO: improve doc @@ -160,7 +169,7 @@ namespace Oyster { namespace Physics3D * * @todo TODO: improve doc ******************************************************************/ - inline ::Oyster::Math::Float3 ImpulseTorque( const ::Oyster::Math::Float3 & offset, const ::Oyster::Math::Float3 &impulseForce ) + inline ::Oyster::Math::Float3 ImpulseTorque( const ::Oyster::Math::Float3 & impulseForce, const ::Oyster::Math::Float3 &offset ) { return offset.Cross( impulseForce ); } diff --git a/OysterPhysics3D/Plane.cpp b/OysterPhysics3D/Plane.cpp index 3c89975b..c999324b 100644 --- a/OysterPhysics3D/Plane.cpp +++ b/OysterPhysics3D/Plane.cpp @@ -19,8 +19,8 @@ Plane & Plane::operator = ( const Plane &plane ) return *this; } -::Utility::Memory::UniquePointer Plane::Clone( ) const -{ return ::Utility::Memory::UniquePointer( new Plane(*this) ); } +::Utility::DynamicMemory::UniquePointer Plane::Clone( ) const +{ return ::Utility::DynamicMemory::UniquePointer( new Plane(*this) ); } bool Plane::Intersects( const ICollideable *target ) const { diff --git a/OysterPhysics3D/Plane.h b/OysterPhysics3D/Plane.h index 570161fb..d44c92bd 100644 --- a/OysterPhysics3D/Plane.h +++ b/OysterPhysics3D/Plane.h @@ -27,7 +27,7 @@ namespace Oyster { namespace Collision3D Plane & operator = ( const Plane &plane ); - virtual ::Utility::Memory::UniquePointer Clone( ) const; + virtual ::Utility::DynamicMemory::UniquePointer Clone( ) const; bool Intersects( const ICollideable *target ) const; bool Contains( const ICollideable *target ) const; }; diff --git a/OysterPhysics3D/Point.cpp b/OysterPhysics3D/Point.cpp index 562e7e98..39cc93af 100644 --- a/OysterPhysics3D/Point.cpp +++ b/OysterPhysics3D/Point.cpp @@ -18,8 +18,8 @@ Point & Point::operator = ( const Point &point ) return *this; } -::Utility::Memory::UniquePointer Point::Clone( ) const -{ return ::Utility::Memory::UniquePointer( new Point(*this) ); } +::Utility::DynamicMemory::UniquePointer Point::Clone( ) const +{ return ::Utility::DynamicMemory::UniquePointer( new Point(*this) ); } bool Point::Intersects( const ICollideable *target ) const { diff --git a/OysterPhysics3D/Point.h b/OysterPhysics3D/Point.h index d4ff655b..2529ae0c 100644 --- a/OysterPhysics3D/Point.h +++ b/OysterPhysics3D/Point.h @@ -26,7 +26,7 @@ namespace Oyster { namespace Collision3D Point & operator = ( const Point &point ); - virtual ::Utility::Memory::UniquePointer Clone( ) const; + virtual ::Utility::DynamicMemory::UniquePointer Clone( ) const; bool Intersects( const ICollideable *target ) const; bool Contains( const ICollideable *target ) const; }; diff --git a/OysterPhysics3D/Ray.cpp b/OysterPhysics3D/Ray.cpp index 7675fad7..f7873437 100644 --- a/OysterPhysics3D/Ray.cpp +++ b/OysterPhysics3D/Ray.cpp @@ -19,8 +19,8 @@ Ray & Ray::operator = ( const Ray &ray ) return *this; } -::Utility::Memory::UniquePointer Ray::Clone( ) const -{ return ::Utility::Memory::UniquePointer( new Ray(*this) ); } +::Utility::DynamicMemory::UniquePointer Ray::Clone( ) const +{ return ::Utility::DynamicMemory::UniquePointer( new Ray(*this) ); } bool Ray::Intersects( const ICollideable *target ) const { diff --git a/OysterPhysics3D/Ray.h b/OysterPhysics3D/Ray.h index a5dced1c..4132c0b8 100644 --- a/OysterPhysics3D/Ray.h +++ b/OysterPhysics3D/Ray.h @@ -35,7 +35,7 @@ namespace Oyster { namespace Collision3D Ray & operator = ( const Ray &ray ); - virtual ::Utility::Memory::UniquePointer Clone( ) const; + virtual ::Utility::DynamicMemory::UniquePointer Clone( ) const; bool Intersects( const ICollideable *target ) const; bool Contains( const ICollideable *target ) const; }; diff --git a/OysterPhysics3D/RigidBody.h b/OysterPhysics3D/RigidBody.h index bb0dcc7d..a2d32f95 100644 --- a/OysterPhysics3D/RigidBody.h +++ b/OysterPhysics3D/RigidBody.h @@ -96,7 +96,7 @@ namespace Oyster { namespace Physics3D void SetSize( const ::Oyster::Math::Float3 &widthHeight ); void SetCenter( const ::Oyster::Math::Float3 &p ); - void SetImpulsTorque( const ::Oyster::Math::Float3 &t ); + void SetImpulseTorque( const ::Oyster::Math::Float3 &t ); void SetAngularMomentum( const ::Oyster::Math::Float3 &h ); void SetAngularImpulseAcceleration( const ::Oyster::Math::Float3 &a ); void SetAngularVelocity( const ::Oyster::Math::Float3 &w ); diff --git a/OysterPhysics3D/Sphere.cpp b/OysterPhysics3D/Sphere.cpp index b0f14cf3..f138208c 100644 --- a/OysterPhysics3D/Sphere.cpp +++ b/OysterPhysics3D/Sphere.cpp @@ -15,8 +15,8 @@ Sphere & Sphere::operator = ( const Sphere &sphere ) return *this; } -::Utility::Memory::UniquePointer Sphere::Clone( ) const -{ return ::Utility::Memory::UniquePointer( new Sphere(*this) ); } +::Utility::DynamicMemory::UniquePointer Sphere::Clone( ) const +{ return ::Utility::DynamicMemory::UniquePointer( new Sphere(*this) ); } bool Sphere::Intersects( const ICollideable *target ) const { diff --git a/OysterPhysics3D/Sphere.h b/OysterPhysics3D/Sphere.h index 9d8c4c8f..881c8928 100644 --- a/OysterPhysics3D/Sphere.h +++ b/OysterPhysics3D/Sphere.h @@ -26,7 +26,7 @@ namespace Oyster { namespace Collision3D Sphere & operator = ( const Sphere &sphere ); - virtual ::Utility::Memory::UniquePointer Clone( ) const; + virtual ::Utility::DynamicMemory::UniquePointer Clone( ) const; bool Intersects( const ICollideable *target ) const; bool Contains( const ICollideable *target ) const; }; diff --git a/OysterPhysics3D/Universe.cpp b/OysterPhysics3D/Universe.cpp index ee02144b..f473ff2c 100644 --- a/OysterPhysics3D/Universe.cpp +++ b/OysterPhysics3D/Universe.cpp @@ -2,7 +2,7 @@ #include "OysterCollision3D.h" using namespace ::Oyster::Collision3D; -using namespace ::Utility::Memory; +using namespace ::Utility::DynamicMemory; Universe::Universe() : ICollideable(Type_universe) {} Universe::~Universe() {} diff --git a/OysterPhysics3D/Universe.h b/OysterPhysics3D/Universe.h index 74b93108..fd06a454 100644 --- a/OysterPhysics3D/Universe.h +++ b/OysterPhysics3D/Universe.h @@ -18,7 +18,7 @@ namespace Oyster { namespace Collision3D Universe & operator = ( const Universe &universe ); - virtual ::Utility::Memory::UniquePointer Clone( ) const; + virtual ::Utility::DynamicMemory::UniquePointer Clone( ) const; bool Intersects( const ICollideable *target ) const; bool Contains( const ICollideable *target ) const; }; From 74aee284f9994cb3767888f98131aa49296b236d Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Tue, 12 Nov 2013 15:57:53 +0100 Subject: [PATCH 5/7] PhysicsAPI created --- GamePhysics/Include/GamePhysics.h | 0 GamePhysics/Include/PhysicsAPI.h | 36 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) delete mode 100644 GamePhysics/Include/GamePhysics.h create mode 100644 GamePhysics/Include/PhysicsAPI.h diff --git a/GamePhysics/Include/GamePhysics.h b/GamePhysics/Include/GamePhysics.h deleted file mode 100644 index e69de29b..00000000 diff --git a/GamePhysics/Include/PhysicsAPI.h b/GamePhysics/Include/PhysicsAPI.h new file mode 100644 index 00000000..3b40e705 --- /dev/null +++ b/GamePhysics/Include/PhysicsAPI.h @@ -0,0 +1,36 @@ +#ifndef PHYSICS_API_H +#define PHYSICS_API_H + +#include "OysterMath.h" + +namespace Oyster +{ + namespace Physics + { + class API; + class IRigidBody; + class IParticle; + + class API + { + public: + static API & Instance(); + }; + + class IRigidBody + { + public: + + }; + + class IParticle + { + public: + + }; + } + + namespace Collision + {} +} +#endif \ No newline at end of file From d4b4973d3f505f222b7b089da8207bf80497602a Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Wed, 13 Nov 2013 17:12:55 +0100 Subject: [PATCH 6/7] GameLogic Properties edited Now they will be properly linked to Physics, and everything else Physics depends on --- GameLogic/GameLogic.vcxproj | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/GameLogic/GameLogic.vcxproj b/GameLogic/GameLogic.vcxproj index d65db31d..900f0834 100644 --- a/GameLogic/GameLogic.vcxproj +++ b/GameLogic/GameLogic.vcxproj @@ -90,7 +90,7 @@ Level3 Disabled true - %(AdditionalIncludeDirectories) + $(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterPhysics3D;$(SolutionDir)GamePhysics;%(AdditionalIncludeDirectories) true @@ -101,7 +101,7 @@ Level3 Disabled true - %(AdditionalIncludeDirectories) + $(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterPhysics3D;$(SolutionDir)GamePhysics;%(AdditionalIncludeDirectories) true @@ -114,7 +114,7 @@ true true true - %(AdditionalIncludeDirectories) + $(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterPhysics3D;$(SolutionDir)GamePhysics;%(AdditionalIncludeDirectories) true @@ -129,7 +129,7 @@ true true true - %(AdditionalIncludeDirectories) + $(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterPhysics3D;$(SolutionDir)GamePhysics;%(AdditionalIncludeDirectories) true @@ -141,6 +141,15 @@ {104fa3e9-94d9-4e1d-a941-28a03bc8a095} + + {2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee} + + + {f10cbc03-9809-4cba-95d8-327c287b18ee} + + + {4285bd3f-3c6c-4670-b7af-a29afef5f6a8} + From 7275b52609b9fd406ceaa9f2f058d0037776d5e7 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Mon, 18 Nov 2013 14:01:23 +0100 Subject: [PATCH 7/7] Preliminary Rigidbody Absolutly not done, but it compiles --- OysterMath/LinearMath.h | 13 +++ OysterMath/OysterMath.cpp | 3 + OysterMath/OysterMath.h | 4 + OysterPhysics3D/RigidBody.cpp | 171 +++++++++++++++++++++------------- 4 files changed, 125 insertions(+), 66 deletions(-) diff --git a/OysterMath/LinearMath.h b/OysterMath/LinearMath.h index a0dba20e..4a036e99 100644 --- a/OysterMath/LinearMath.h +++ b/OysterMath/LinearMath.h @@ -336,6 +336,19 @@ namespace LinearAlgebra3D 0, 0, 0, 1 ); } + // O0 = T0 * R0 + // O1 = T1 * T0 * R1 * R0 + template + ::LinearAlgebra::Matrix4x4 & UpdateOrientationMatrix( const ::LinearAlgebra::Vector3 &deltaPosition, const ::LinearAlgebra::Matrix4x4 &deltaRotationMatrix, ::LinearAlgebra::Matrix4x4 &orientationMatrix ) + { + ::LinearAlgebra::Vector3 position = deltaPosition + orientationMatrix.v[3].xyz; + orientationMatrix.v[3].xyz = ::LinearAlgebra::Vector3::null; + + orientationMatrix = deltaRotationMatrix * orientationMatrix; + orientationMatrix.v[3].xyz = position; + return orientationMatrix; + } + /* Creates an orthographic projection matrix designed for DirectX enviroment. width; of the projection sample volume. height; of the projection sample volume. diff --git a/OysterMath/OysterMath.cpp b/OysterMath/OysterMath.cpp index 24266a37..84b46b7b 100644 --- a/OysterMath/OysterMath.cpp +++ b/OysterMath/OysterMath.cpp @@ -75,6 +75,9 @@ namespace Oyster { namespace Math3D Float4x4 & InverseOrientationMatrix( const Float4x4 &orientationMatrix, Float4x4 &targetMem ) { return ::LinearAlgebra3D::InverseOrientationMatrix( orientationMatrix, targetMem ); } + Float4x4 & UpdateOrientationMatrix( const Float3 &deltaPosition, const Float4x4 &deltaRotationMatrix, Float4x4 &orientationMatrix ) + { return ::LinearAlgebra3D::UpdateOrientationMatrix( deltaPosition, deltaRotationMatrix, orientationMatrix ); } + Float4x4 & ProjectionMatrix_Orthographic( const Float &width, const Float &height, const Float &nearClip, const Float &farClip, Float4x4 &targetMem ) { return ::LinearAlgebra3D::ProjectionMatrix_Orthographic( width, height, nearClip, farClip, targetMem ); } diff --git a/OysterMath/OysterMath.h b/OysterMath/OysterMath.h index c66f44cc..07ae50d3 100644 --- a/OysterMath/OysterMath.h +++ b/OysterMath/OysterMath.h @@ -183,6 +183,10 @@ namespace Oyster { namespace Math3D /// Oyster's native math library specialized /// If orientationMatrix is assumed to be by all definitions a rigid orientation matrix aka rigid body matrix. Then this is a much faster inverse method. Float4x4 & InverseOrientationMatrix( const Float4x4 &orientationMatrix, Float4x4 &targetMem = Float4x4() ); + // O0 = T0 * R0 + // O1 = T1 * T0 * R1 * R0 + Float4x4 & UpdateOrientationMatrix( const Float3 &deltaPosition, const Float4x4 &deltaRotationMatrix, Float4x4 &orientationMatrix ); + /******************************************************************* * Creates an orthographic projection matrix designed for DirectX enviroment. * @param width; of the projection sample volume. diff --git a/OysterPhysics3D/RigidBody.cpp b/OysterPhysics3D/RigidBody.cpp index 306a0bf6..fff6b994 100644 --- a/OysterPhysics3D/RigidBody.cpp +++ b/OysterPhysics3D/RigidBody.cpp @@ -60,8 +60,10 @@ void RigidBody::Update_LeapFrog( Float deltaTime ) deltaRadian = ::std::sqrt( deltaRadian ); rotationAxis /= deltaRadian; - // using rotationAxis, deltaRadian and deltaPos to create a matrix to transform the orientation matrix - this->box.orientation = OrientationMatrix( rotationAxis, deltaRadian, deltaPos ) * this->box.orientation; + // using rotationAxis, deltaRadian and deltaPos to create a matrix to update the orientation matrix + UpdateOrientationMatrix( deltaPos, RotationMatrix(deltaRadian, rotationAxis), this->box.orientation ); + + /** @todo TODO: ISSUE! how is momentOfInertiaTensor related to the orientation of the RigidBody? */ } else { // no rotation, only use deltaPos to translate the RigidBody @@ -85,7 +87,7 @@ void RigidBody::ApplyImpulseForceAt_Local( const Float3 &localForce, const Float if( localOffset != Float3::null ) { this->impulseForceSum += VectorProjection( localForce, localOffset ); - this->impulseTorqueSum += Formula::ImpulseTorque( localOffset, localForce ); + this->impulseTorqueSum += Formula::ImpulseTorque( localForce, localOffset ); } else { @@ -284,138 +286,175 @@ Float3 RigidBody::GetTangentialLinearVelocityAt_World( const Float3 &worldPos ) return this->GetTangentialLinearVelocityAt_Local( (this->GetView() * Float4(worldPos, 1.0f)).xyz ); // should not be any disform thus result.w = 1.0f } -Float3 RigidBody::GetImpulseForceAt_Local( const Float3 &pos ) const -{ // by - return Float3::null; +Float3 RigidBody::GetImpulseForceAt_Local( const Float3 &localPos ) const +{ // by Dan Andersson + return this->impulseForceSum + Formula::TangentialImpulseForce( this->impulseForceSum, localPos ); } -Float3 RigidBody::GetImpulseForceAt_World( const Float3 &pos ) const -{ // by - return Float3::null; +Float3 RigidBody::GetImpulseForceAt_World( const Float3 &worldPos ) const +{ // by Dan Andersson + Float4 localForce = Float4( this->GetImpulseForceAt_Local((this->GetView() * Float4(worldPos, 1.0f)).xyz), 0.0f ); // should not be any disform thus result.w = 1.0f + return (this->box.orientation * localForce).xyz; // should not be any disform thus result.w = 0.0f } -Float3 RigidBody::GetLinearMomentumAt_Local( const Float3 &pos ) const -{ // by - return Float3::null; +Float3 RigidBody::GetLinearMomentumAt_Local( const Float3 &localPos ) const +{ // by Dan Andersson + // Reminder! Momentum is a world value. + return Float3::null; // TODO: } -Float3 RigidBody::GetLinearMomentumAt_World( const Float3 &pos ) const -{ // by - return Float3::null; +Float3 RigidBody::GetLinearMomentumAt_World( const Float3 &worldPos ) const +{ // by Dan Andersson + // Reminder! Momentum is a world value. + Float4 localMomentum = Float4( this->GetLinearMomentumAt_Local((this->GetView() * Float4(worldPos, 1.0f)).xyz), 0.0f ); // should not be any disform thus result.w = 1.0f + return (this->box.orientation * localMomentum).xyz; // should not be any disform thus result.w = 0.0f + + // TODO: angularMomentum is a local value!! + return this->linearMomentum + Formula::TangentialLinearMomentum( this->angularMomentum, worldPos ); } -Float3 RigidBody::GetImpulseAccelerationAt_Local( const Float3 &pos ) const -{ // by - return Float3::null; +Float3 RigidBody::GetImpulseAccelerationAt_Local( const Float3 &localPos ) const +{ // by Dan Andersson + // Reminder! Acceleration is a world value. + Float4 worldAccel = Float4( this->GetImpulseAccelerationAt_Local((this->box.orientation * Float4(localPos, 1.0f)).xyz), 0.0f ); // should not be any disform thus result.w = 1.0f + return (this->GetView() * worldAccel).xyz; // should not be any disform thus result.w = 0.0f } -Float3 RigidBody::GetImpulseAccelerationAt_World( const Float3 &pos ) const -{ // by - return Float3::null; +Float3 RigidBody::GetImpulseAccelerationAt_World( const Float3 &worldPos ) const +{ // by Dan Andersson + // Reminder! Acceleration is a world value. + return Formula::LinearImpulseAcceleration( this->mass, this->impulseForceSum ) + + Formula::TangentialImpulseAcceleration( this->momentOfInertiaTensor.GetInverse(), this->impulseTorqueSum, worldPos ); } -Float3 RigidBody::GetLinearVelocityAt_Local( const Float3 &pos ) const -{ // by - return Float3::null; +Float3 RigidBody::GetLinearVelocityAt_Local( const Float3 &localPos ) const +{ // by Dan Andersson + // Reminder! Velocity is a world value. + Float4 worldV = Float4( this->GetLinearVelocityAt_Local((this->box.orientation * Float4(localPos, 1.0f)).xyz), 0.0f ); // should not be any disform thus result.w = 1.0f + return (this->GetView() * worldV).xyz; // should not be any disform thus result.w = 0.0f } -Float3 RigidBody::GetLinearVelocityAt_World( const Float3 &pos ) const -{ // by - return Float3::null; +Float3 RigidBody::GetLinearVelocityAt_World( const Float3 &worldPos ) const +{ // by Dan Andersson + // Reminder! Velocity is a world value. + return Formula::LinearVelocity( this->mass, this->linearMomentum ) + + Formula::TangentialLinearVelocity( this->momentOfInertiaTensor.GetInverse(), this->angularMomentum, worldPos ); } void RigidBody::SetMomentOfInertia( const Float4x4 &i ) -{ // by - +{ // by Dan Andersson + if( i.GetDeterminant() != 0.0f ) // insanitycheck! momentOfInertiaTensor must be invertable + { + this->momentOfInertiaTensor = i; + } } void RigidBody::SetMass_KeepVelocity( const Float &m ) -{ // by - +{ // by Dan Andersson + if( m != 0.0f ) // insanitycheck! mass must be invertable + { + Float3 velocity = Formula::LinearVelocity( this->mass, this->linearMomentum ); + this->mass = m; + this->linearMomentum = Formula::LinearMomentum( this->mass, velocity ); + } } void RigidBody::SetMass_KeepMomentum( const Float &m ) -{ // by - +{ // by Dan Anderson + if( m != 0.0f ) // insanitycheck! mass must be invertable + { + this->mass = m; + } } void RigidBody::SetOrientation( const Float4x4 &o ) -{ // by - +{ // by Dan Andersson + this->box.orientation = o; } void RigidBody::SetSize( const Float3 &widthHeight ) -{ // by - +{ // by Dan Andersson + this->box.boundingOffset = 0.5f * widthHeight; } void RigidBody::SetCenter( const Float3 &p ) -{ // by - +{ // by Dan Andersson + this->box.center = p; } -void RigidBody::SetImpulsTorque( const Float3 &t ) -{ // by - +void RigidBody::SetImpulseTorque( const Float3 &t ) +{ // by Dan Andersson + this->impulseTorqueSum = t; } void RigidBody::SetAngularMomentum( const Float3 &h ) -{ // by - +{ // by Dan Andersson + this->angularMomentum = h; } void RigidBody::SetAngularImpulseAcceleration( const Float3 &a ) -{ // by - +{ // by Dan Andersson + this->impulseTorqueSum = Formula::ImpulseTorque( this->momentOfInertiaTensor, a ); } void RigidBody::SetAngularVelocity( const Float3 &w ) -{ // by - +{ // by Dan Andersson + this->angularMomentum = Formula::AngularMomentum( this->momentOfInertiaTensor, w ); } void RigidBody::SetImpulseForce( const Float3 &f ) -{ // by - +{ // by Dan Andersson + this->impulseForceSum = f; } void RigidBody::SetLinearMomentum( const Float3 &g ) -{ // by - +{ // by Dan Andersson + this->linearMomentum = g; } void RigidBody::SetLinearImpulseAcceleration( const Float3 &a ) -{ // by - +{ // by Dan Andersson + this->impulseForceSum = Formula::ImpulseForce( this->mass, a ); } void RigidBody::SetLinearVelocity( const Float3 &v ) -{ // by - +{ // by Dan Andersson + this->linearMomentum = Formula::LinearMomentum( this->mass, v ); } void RigidBody::SetImpulseForceAt_Local( const Float3 &localForce, const Float3 &localPos ) -{ // by +{ // by Dan Andersson + // Reminder! Impulse force and torque is world values. + Float3 worldForce = ( this->box.orientation * Float4(localForce, 0.0f) ).xyz, + worldPos = ( this->box.orientation * Float4(localPos, 1.0f) ).xyz; + this->SetImpulseForceAt_World( worldForce, worldPos ); } void RigidBody::SetImpulseForceAt_World( const Float3 &worldForce, const Float3 &worldPos ) -{ // by - +{ // by Dan Andersson + // Reminder! Impulse force and torque is world values. + this->impulseForceSum = VectorProjection( worldForce, worldPos ); + this->impulseTorqueSum = Formula::ImpulseTorque( worldForce, worldPos ); } -void RigidBody::SetLinearMomentumAt_Local( const Float3 &g, const Float3 &pos ) -{ // by - +void RigidBody::SetLinearMomentumAt_Local( const Float3 &localG, const Float3 &localPos ) +{ // by Dan Andersson + // Reminder! Linear and angular momentum is world values. + Float3 worldG = ( this->box.orientation * Float4(localG, 0.0f) ).xyz, + worldPos = ( this->box.orientation * Float4(localPos, 1.0f) ).xyz; + this->SetLinearMomentumAt_World( worldG, worldPos ); } -void RigidBody::SetLinearMomentumAt_World( const Float3 &g, const Float3 &pos ) -{ // by - +void RigidBody::SetLinearMomentumAt_World( const Float3 &worldG, const Float3 &worldPos ) +{ // by Dan Andersson + // Reminder! Linear and angular momentum is world values. + this->linearMomentum = VectorProjection( worldG, worldPos ); + this->angularMomentum = Formula::AngularMomentum( worldG, worldPos ); } void RigidBody::SetImpulseAccelerationAt_Local( const Float3 &a, const Float3 &pos ) -{ // by +{ // by Dan Andersson }