diff --git a/Code/Debug/Tester.ilk b/Code/Debug/Tester.ilk deleted file mode 100644 index 49c33576..00000000 Binary files a/Code/Debug/Tester.ilk and /dev/null differ diff --git a/Code/Debug/Tester.pdb b/Code/Debug/Tester.pdb deleted file mode 100644 index 6fcf8970..00000000 Binary files a/Code/Debug/Tester.pdb and /dev/null differ diff --git a/Code/GamePhysics/GamePhysics.vcxproj b/Code/GamePhysics/GamePhysics.vcxproj index c9f6513c..f5493960 100644 --- a/Code/GamePhysics/GamePhysics.vcxproj +++ b/Code/GamePhysics/GamePhysics.vcxproj @@ -24,7 +24,7 @@ - StaticLibrary + DynamicLibrary true v110 MultiByte @@ -66,22 +66,22 @@ - $(SolutionDir)..\External\Bin\DLL\ + $(SolutionDir)..\Bin\DLL\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D - $(SolutionDir)..\External\Bin\DLL\ + $(SolutionDir)..\Bin\DLL\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName) - $(SolutionDir)..\External\Bin\DLL\ + $(SolutionDir)..\Bin\DLL\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D - $(SolutionDir)..\External\Bin\DLL\ + $(SolutionDir)..\Bin\DLL\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName) diff --git a/Code/GamePhysics/Implementation/NullBody.cpp b/Code/GamePhysics/Implementation/NullBody.cpp index 76d2471d..82c75a78 100644 --- a/Code/GamePhysics/Implementation/NullBody.cpp +++ b/Code/GamePhysics/Implementation/NullBody.cpp @@ -28,11 +28,6 @@ bool NullBody::Intersects( const ICollideable &shape ) const return false; } -unsigned int NullBody::GetReference() const -{ - return not_a_reference; -} - Sphere & NullBody::GetBoundingSphere( Sphere &targetMem ) const { return targetMem = Sphere( Float3::null, 0.0f ); @@ -80,4 +75,4 @@ void NullBody::SetCenter( const Float3 &worldPos ) {} void NullBody::SetRotation( const Float4x4 &rotation ) {} -void NullBody::SetOrientation( const Float4x4 &orientation ) {} \ No newline at end of file +void NullBody::SetOrientation( const Float4x4 &orientation ) {} diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index c1816a0b..9bb764fc 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -1,5 +1,6 @@ #include "PhysicsAPI_Impl.h" #include "SimpleRigidBody.h" +#include "OysterPhysics3D.h" using namespace ::Oyster::Physics; using namespace ::Oyster::Math; @@ -8,6 +9,31 @@ using namespace ::Utility::DynamicMemory; API_Impl instance; +::Oyster::Math::Float4x4 & MomentOfInertia::CreateSphereMatrix( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius) +{ + return ::Oyster::Physics3D::Formula::MomentOfInertia::Sphere(mass, radius); +} + +::Oyster::Math::Float4x4 & MomentOfInertia::CreateHollowSphereMatrix( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius) +{ + return ::Oyster::Physics3D::Formula::MomentOfInertia::HollowSphere(mass, radius); +} + +::Oyster::Math::Float4x4 & MomentOfInertia::CreateCuboidMatrix( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float height, const ::Oyster::Math::Float width, const ::Oyster::Math::Float depth ) +{ + return ::Oyster::Physics3D::Formula::MomentOfInertia::Cuboid(mass, height, width, depth); +} + +::Oyster::Math::Float4x4 & MomentOfInertia::CreateCylinderMatrix( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float height, const ::Oyster::Math::Float radius ) +{ + return ::Oyster::Physics3D::Formula::MomentOfInertia::Cylinder(mass, height, radius); +} + +::Oyster::Math::Float4x4 & MomentOfInertia::CreateRodMatrix( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float length ) +{ + return ::Oyster::Physics3D::Formula::MomentOfInertia::RodCenter(mass, length); +} + API & Instance() { return instance; @@ -45,80 +71,78 @@ void API_Impl::Update() /** @todo TODO: Fix this function.*/ } -bool API_Impl::IsInLimbo( unsigned int objRef ) +bool API_Impl::IsInLimbo( const ICustomBody* objRef ) { //! @todo TODO: implement stub return true; } -void API_Impl::MoveToLimbo( unsigned int objRef ) +void API_Impl::MoveToLimbo( const ICustomBody* objRef ) { /** @todo TODO: Fix this function.*/ } -void API_Impl::ReleaseFromLimbo( unsigned int objRef ) +void API_Impl::ReleaseFromLimbo( const ICustomBody* objRef ) { /** @todo TODO: Fix this function.*/ } -unsigned int API_Impl::AddObject( ::Utility::DynamicMemory::UniquePointer handle ) +void API_Impl::AddObject( ::Utility::DynamicMemory::UniquePointer handle ) { /** @todo TODO: Fix this function.*/ - - return 0; } -::Utility::DynamicMemory::UniquePointer ExtractObject( unsigned int objRef ) +::Utility::DynamicMemory::UniquePointer API_Impl::ExtractObject( const ICustomBody* objRef ) { //! @todo TODO: implement stub return NULL; } -void API_Impl::DestroyObject( unsigned int objRef ) +void API_Impl::DestroyObject( const ICustomBody* objRef ) { /** @todo TODO: Fix this function.*/ } -void API_Impl::ApplyForceAt( unsigned int objRef, const Float3 &worldPos, const Float3 &worldF ) +void API_Impl::ApplyForceAt( const ICustomBody* objRef, const Float3 &worldPos, const Float3 &worldF ) { //! @todo TODO: implement stub } -void API_Impl::ApplyCollisionResponse( unsigned int objRefA, unsigned int objRefB, Float &deltaWhen, Float3 &worldPointOfContact ) +void API_Impl::ApplyCollisionResponse( const ICustomBody* objRefA, const ICustomBody* objRefB, Float &deltaWhen, Float3 &worldPointOfContact ) { //! @todo TODO: implement stub } -void API_Impl::SetMomentOfInertiaTensor_KeepVelocity( unsigned int objRef, const Float4x4 &localI ) +void API_Impl::SetMomentOfInertiaTensor_KeepVelocity( const ICustomBody* objRef, const Float4x4 &localI ) { //! @todo TODO: implement stub } -void API_Impl::SetMomentOfInertiaTensor_KeepMomentum( unsigned int objRef, const Float4x4 &localI ) +void API_Impl::SetMomentOfInertiaTensor_KeepMomentum( const ICustomBody* objRef, const Float4x4 &localI ) { //! @todo TODO: implement stub } -void API_Impl::SetMass_KeepVelocity( unsigned int objRef, Float m ) +void API_Impl::SetMass_KeepVelocity( const ICustomBody* objRef, Float m ) { //! @todo TODO: implement stub } -void API_Impl::SetMass_KeepMomentum( unsigned int objRef, Float m ) +void API_Impl::SetMass_KeepMomentum( const ICustomBody* objRef, Float m ) { //! @todo TODO: implement stub } -void API_Impl::SetCenter( unsigned int objRef, const Float3 &worldPos ) +void API_Impl::SetCenter( const ICustomBody* objRef, const Float3 &worldPos ) { //! @todo TODO: implement stub } -void API_Impl::SetRotation( unsigned int objRef, const Float4x4 &rotation ) +void API_Impl::SetRotation( const ICustomBody* objRef, const Float4x4 &rotation ) { //! @todo TODO: implement stub } -void API_Impl::SetOrientation( unsigned int objRef, const Float4x4 &orientation ) +void API_Impl::SetOrientation( const ICustomBody* objRef, const Float4x4 &orientation ) { //! @todo TODO: implement stub } diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h index 3b1f217c..2cec91a9 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.h @@ -20,26 +20,24 @@ namespace Oyster void Update(); - bool IsInLimbo( unsigned int objRef ); - void MoveToLimbo( unsigned int objRef ); - void ReleaseFromLimbo( unsigned int objRef ); + bool IsInLimbo( const ICustomBody* objRef ); + void MoveToLimbo( const ICustomBody* objRef ); + void ReleaseFromLimbo( const ICustomBody* objRef ); - unsigned int AddObject( ::Utility::DynamicMemory::UniquePointer handle ); - ::Utility::DynamicMemory::UniquePointer ExtractObject( unsigned int objRef ); - void DestroyObject( unsigned int objRef ); + void AddObject( ::Utility::DynamicMemory::UniquePointer handle ); + ::Utility::DynamicMemory::UniquePointer ExtractObject( const ICustomBody* objRef ); + void DestroyObject( const ICustomBody* objRef ); - const ICustomBody & Peek( unsigned int objRef ) const; + void ApplyForceAt( const ICustomBody* objRef, const ::Oyster::Math::Float3 &worldPos, const ::Oyster::Math::Float3 &worldF ); + void ApplyCollisionResponse( const ICustomBody* objRefA, const ICustomBody* objRefB, ::Oyster::Math::Float &deltaWhen, ::Oyster::Math::Float3 &worldPointOfContact ); - void ApplyForceAt( unsigned int objRef, const ::Oyster::Math::Float3 &worldPos, const ::Oyster::Math::Float3 &worldF ); - void ApplyCollisionResponse( unsigned int objRefA, unsigned int objRefB, ::Oyster::Math::Float &deltaWhen, ::Oyster::Math::Float3 &worldPointOfContact ); - - void SetMomentOfInertiaTensor_KeepVelocity( unsigned int objRef, const ::Oyster::Math::Float4x4 &localI ); - void SetMomentOfInertiaTensor_KeepMomentum( unsigned int objRef, const ::Oyster::Math::Float4x4 &localI ); - void SetMass_KeepVelocity( unsigned int objRef, ::Oyster::Math::Float m ); - void SetMass_KeepMomentum( unsigned int objRef, ::Oyster::Math::Float m ); - void SetCenter( unsigned int objRef, const ::Oyster::Math::Float3 &worldPos ); - void SetRotation( unsigned int objRef, const ::Oyster::Math::Float4x4 &rotation ); - void SetOrientation( unsigned int objRef, const ::Oyster::Math::Float4x4 &orientation ); + void SetMomentOfInertiaTensor_KeepVelocity( const ICustomBody* objRef, const ::Oyster::Math::Float4x4 &localI ); + void SetMomentOfInertiaTensor_KeepMomentum( const ICustomBody* objRef, const ::Oyster::Math::Float4x4 &localI ); + void SetMass_KeepVelocity( const ICustomBody* objRef, ::Oyster::Math::Float m ); + void SetMass_KeepMomentum( const ICustomBody* objRef, ::Oyster::Math::Float m ); + 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 ); ::Utility::DynamicMemory::UniquePointer CreateSimpleRigidBody() const; }; diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp index 59d501b4..00f456aa 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp @@ -38,12 +38,6 @@ bool SimpleRigidBody::Intersects( const ICollideable &shape ) const return false; } -unsigned int SimpleRigidBody::GetReference() const -{ - //! @todo TODO: implement stub - return Error::not_a_reference; -} - Sphere & SimpleRigidBody::GetBoundingSphere( Sphere &targetMem ) const { //! @todo TODO: implement stub diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.h b/Code/GamePhysics/Implementation/SimpleRigidBody.h index dd6531e6..ebedcb6a 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.h +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.h @@ -17,7 +17,6 @@ namespace Oyster { namespace Physics bool Intersects( const ICustomBody &object, ::Oyster::Math::Float &deltaWhen, ::Oyster::Math::Float3 &worldPointOfContact ) const; bool Intersects( const ::Oyster::Collision3D::ICollideable &shape ) const; - unsigned int GetReference() 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 & GetCenter( ::Oyster::Math::Float3 &targetMem = ::Oyster::Math::Float3() ) const; diff --git a/Code/GamePhysics/PhysicsAPI.h b/Code/GamePhysics/PhysicsAPI.h index c1f6ef43..9bec064a 100644 --- a/Code/GamePhysics/PhysicsAPI.h +++ b/Code/GamePhysics/PhysicsAPI.h @@ -3,7 +3,6 @@ #include "OysterCollision3D.h" #include "OysterMath.h" -#include "Utilities.h" namespace Oyster { @@ -20,45 +19,181 @@ namespace Oyster namespace Constant { - const float gravity_constant = (const float)6.67284e-11; // The _big_G_! ( N(m/kg)^2 ) Used in real gravityforcefields. + const float gravity_constant = (const float)6.67284e-11; //!< The _big_G_! ( N(m/kg)^2 ) Used in real gravityforcefields. } + class MomentOfInertia + { + public: + static ::Oyster::Math::Float4x4 & CreateSphereMatrix( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius); + + static ::Oyster::Math::Float4x4 & CreateHollowSphereMatrix( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float radius); + + static ::Oyster::Math::Float4x4 & CreateCuboidMatrix( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float height, const ::Oyster::Math::Float width, const ::Oyster::Math::Float depth ); + + static ::Oyster::Math::Float4x4 & CreateCylinderMatrix( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float height, const ::Oyster::Math::Float radius ); + + static ::Oyster::Math::Float4x4 & CreateRodMatrix( const ::Oyster::Math::Float mass, const ::Oyster::Math::Float length ); + }; + class API { public: typedef void (*EventAction_Collision)( unsigned int, unsigned int ); typedef void (*EventAction_Destruction)( unsigned int, ::Utility::DynamicMemory::UniquePointer ); + /** Gets the Physics instance. */ static API & Instance(); - virtual void SetDeltaTime( float deltaTime ) = 0; + /******************************************************** + * Sets the time length of each physics update frame. + ********************************************************/ + virtual void SetDeltaTime( float seconds ) = 0; + + /******************************************************** + * Sets the Gravityconstant in the physics that will be + * used in ForceField calculations. + * @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. + ********************************************************/ virtual void SetAction( EventAction_Destruction functionPointer ) = 0; + /******************************************************** + * Triggers the engine to run next update frame. + * All accumulated forces and changes will be consumed. + * EventAction functions might be called. + ********************************************************/ virtual void Update() = 0; - virtual bool IsInLimbo( unsigned int objRef ) = 0; - virtual void MoveToLimbo( unsigned int objRef ) = 0; - virtual void ReleaseFromLimbo( unsigned int objRef ) = 0; + /******************************************************** + * An object in limbo state will be ignored during the physics frame Update. + * @param objRef: A pointer to the ICustomBody representing a physical object. + * @return true if object is in limbo state. + ********************************************************/ + virtual bool IsInLimbo( const ICustomBody* objRef ) = 0; + + /******************************************************** + * An object in limbo state will be ignored during the physics frame Update. + * This will put an object in Limbo state. + * @param objRef: A pointer to the ICustomBody representing a physical object. + ********************************************************/ + virtual void MoveToLimbo( const ICustomBody* objRef ) = 0; + + /******************************************************** + * An object in limbo state will be ignored during the physics frame Update. + * This will clear the accumulated force/torque and remove the Limbo state. + * @param objRef: A pointer to the ICustomBody representing a physical object. + ********************************************************/ + virtual void ReleaseFromLimbo( const ICustomBody* objRef ) = 0; - virtual unsigned int AddObject( ::Utility::DynamicMemory::UniquePointer handle ) = 0; - virtual ::Utility::DynamicMemory::UniquePointer ExtractObject( unsigned int objRef ) = 0; - virtual void DestroyObject( unsigned int objRef ) = 0; + /******************************************************** + * Introduces a new object into the engine. + * @param handle: A pointer along with the responsibility to delete. + ********************************************************/ + virtual void AddObject( ::Utility::DynamicMemory::UniquePointer handle ) = 0; - virtual const ICustomBody & Peek( unsigned int objRef ) const = 0; + /******************************************************** + * Fetches and removes an object from the engine. + * Will not call the provided EventAction_Destruction method. + * @param objRef: A pointer to the ICustomBody representing a physical object. + * @return A pointer along with the responsibility to delete. NULL if faulty objRef. + ********************************************************/ + virtual ::Utility::DynamicMemory::UniquePointer ExtractObject( const ICustomBody* objRef ) = 0; - virtual void ApplyForceAt( unsigned int objRef, const ::Oyster::Math::Float3 &worldPos, const ::Oyster::Math::Float3 &worldF ) = 0; - virtual void ApplyCollisionResponse( unsigned int objRefA, unsigned int objRefB, ::Oyster::Math::Float &deltaWhen, ::Oyster::Math::Float3 &worldPointOfContact ) = 0; + /******************************************************** + * Removes an object from the engine. + * Will call the provided EventAction_Destruction method. Not if objRef is faulty. + * @param objRef: A pointer to the ICustomBody representing a physical object. + ********************************************************/ + virtual void DestroyObject( const ICustomBody* objRef ) = 0; - virtual void SetMomentOfInertiaTensor_KeepVelocity( unsigned int objRef, const ::Oyster::Math::Float4x4 &localI ) = 0; - virtual void SetMomentOfInertiaTensor_KeepMomentum( unsigned int objRef, const ::Oyster::Math::Float4x4 &localI ) = 0; - virtual void SetMass_KeepVelocity( unsigned int objRef, ::Oyster::Math::Float m ) = 0; - virtual void SetMass_KeepMomentum( unsigned int objRef, ::Oyster::Math::Float m ) = 0; - virtual void SetCenter( unsigned int objRef, const ::Oyster::Math::Float3 &worldPos ) = 0; - virtual void SetRotation( unsigned int objRef, const ::Oyster::Math::Float4x4 &rotation ) = 0; - virtual void SetOrientation( unsigned int objRef, const ::Oyster::Math::Float4x4 &orientation ) = 0; + /******************************************************** + * Apply force on an object. + * @param objRef: A pointer to the ICustomBody representing a physical object. + * @param worldPos: Relative to the world origo. (Not relative to object) [m] + * @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. + * @param objRefB: A pointer to the ICustomBody representing a physical object. + * @param deltaWhen: The elapsed simulation time since last update frame. [s] + * @param worldPointOfContact: Point of Collision, relative to the world origo. (Not relative to the objects) [m] + ********************************************************/ + virtual void ApplyCollisionResponse( const ICustomBody* objRefA, const ICustomBody* objRefB, ::Oyster::Math::Float &deltaWhen, ::Oyster::Math::Float3 &worldPointOfContact ) = 0; + /******************************************************** + * Sets the MomentOfInertia tensor matrix of an object without changing it's angular velocity. + * Noticeable effect: The angular momentum will change. Changing the amount of kinetic energy. + * @param objRef: A pointer to the ICustomBody representing a physical object. + * @param localI: The tensor matrix relative to the axises of the object. @see MomentOfInertia namespace. + ********************************************************/ + virtual void SetMomentOfInertiaTensor_KeepVelocity( const ICustomBody* objRef, const ::Oyster::Math::Float4x4 &localI ) = 0; + + /******************************************************** + * Sets the MomentOfInertia tensor matrix of an object without changing it's angular momentum. + * Noticeable effect: The angular velocity will change. Can be used to create slow effects. + * @param objRef: A pointer to the ICustomBody representing a physical object. + * @param localI: The tensor matrix relative to the axises of the object. @see MomentOfInertia namespace. + ********************************************************/ + virtual void SetMomentOfInertiaTensor_KeepMomentum( const ICustomBody* objRef, const ::Oyster::Math::Float4x4 &localI ) = 0; + + /******************************************************** + * Sets the mass of an object without changing it's linear velocity. + * Noticeable effect: The linear momentum will change. Changing the amount of kinetic energy. + * @param objRef: A pointer to the ICustomBody representing a physical object. + * @param m: [kg] + ********************************************************/ + virtual void SetMass_KeepVelocity( const ICustomBody* objRef, ::Oyster::Math::Float m ) = 0; + + /******************************************************** + * Sets the mass of an object without changing it's linear velocity. + * Noticeable effect: The linear velocity will change. Can be used to create slow effects. + * @param objRef: A pointer to the ICustomBody representing a physical object. + * @param m: [kg] + ********************************************************/ + virtual void SetMass_KeepMomentum( const ICustomBody* objRef, ::Oyster::Math::Float m ) = 0; + + /******************************************************** + * Instantly moves an object. + * @param objRef: A pointer to the ICustomBody representing a physical object. + * @param worldPos: Relative to the world origo. (Not relative to object) [m] + ********************************************************/ + virtual void SetCenter( const ICustomBody* objRef, const ::Oyster::Math::Float3 &worldPos ) = 0; + + /******************************************************** + * Instantly redirects object. + * @param objRef: A pointer to the ICustomBody representing a physical object. + * @param rotation: New rotation. + ********************************************************/ + virtual void SetRotation( const ICustomBody* objRef, const ::Oyster::Math::Float4x4 &rotation ) = 0; + + /******************************************************** + * Instantly moves and redirects object. + * @param objRef: A pointer to the ICustomBody representing a physical object. + * @param orientation: New orientation. + ********************************************************/ + virtual void SetOrientation( const ICustomBody* objRef, const ::Oyster::Math::Float4x4 &orientation ) = 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. + ********************************************************/ virtual ::Utility::DynamicMemory::UniquePointer CreateSimpleRigidBody() const = 0; protected: @@ -76,7 +211,6 @@ namespace Oyster virtual bool Intersects( const ICustomBody &object, ::Oyster::Math::Float &deltaWhen, ::Oyster::Math::Float3 &worldPointOfContact ) const = 0; virtual bool Intersects( const ::Oyster::Collision3D::ICollideable &shape ) const = 0; - virtual unsigned int GetReference() const = 0; virtual ::Oyster::Collision3D::Sphere & GetBoundingSphere( ::Oyster::Collision3D::Sphere &targetMem = ::Oyster::Collision3D::Sphere() ) const = 0; virtual ::Oyster::Math::Float3 & GetNormalAt( const ::Oyster::Math::Float3 &worldPos, ::Oyster::Math::Float3 &targetMem = ::Oyster::Math::Float3() ) const = 0; virtual ::Oyster::Math::Float3 & GetCenter( ::Oyster::Math::Float3 &targetMem = ::Oyster::Math::Float3() ) const = 0; @@ -96,8 +230,7 @@ namespace Oyster }; namespace Error - { - const unsigned int not_a_reference = ::Utility::Value::numeric_limits::max(); + { //! The content in here is used as API return errorvalues. class NullBody : public ICustomBody { diff --git a/Code/Misc/Utilities.h b/Code/Misc/Utilities.h index 950c3933..a07143cf 100644 --- a/Code/Misc/Utilities.h +++ b/Code/Misc/Utilities.h @@ -1,7 +1,7 @@ -///////////////////////////////////////////////////////////////////// +//!//!//!//!//!//!//!//!//!//!//!//!//!//!//!//!//!//!//!//!//!//!//! // Utility Collection of Miscellanious Handy Functions // © Dan Andersson 2013 -///////////////////////////////////////////////////////////////////// +//!//!//!//!//!//!//!//!//!//!//!//!//!//!//!//!//!//!//!//!//!//!//! #ifndef UTILITIES_H #define UTILITIES_H @@ -16,50 +16,50 @@ namespace Utility { namespace DynamicMemory { - /// If dynamicInstance is not NULL, then delete + //! If dynamicInstance is not NULL, then delete template void SafeDeleteInstance( Type *dynamicInstance ); - /// If dynamicArray is not NULL, then delete [] + //! If dynamicArray is not NULL, then delete [] template void SafeDeleteArray( Type dynamicArray[] ); - template - struct UniquePointer - { /// Wrapper to safely transfer dynamic ownership/responsibility + //! Wrapper to safely transfer dynamic ownership/responsibility + template struct UniquePointer + { public: - /// 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. + //! 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. + //! Will auto delete assigned dynamic instance. ~UniquePointer(); - /// 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. + //! 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. + //! 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 + //! Access the assigned dynamic instance. Will crash if nothing there operator Type* (); - /// Access the assigned dynamic instance. Will crash if nothing there + //! 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 + //! 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 + //! 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. + //! 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. + //! This UniquePointer drops all claims of ownership/responsibility and returns the dynamic instance. Now it is your responsibility to delete. Type* Release(); - /// (inline) If true, this UniquePointer have a current ownership/responsibility of a dynamic instance. + //! (inline) If true, this UniquePointer have a current ownership/responsibility of a dynamic instance. bool HaveOwnership() const; private: @@ -68,38 +68,38 @@ namespace Utility template struct UniqueArray - { /// Wrapper to safely transfer dynamic ownership/responsibility + { //! 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. + //! 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. + //! Will auto delete assigned dynamic array. ~UniqueArray(); - /// 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. + //! 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. + //! 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 ); - /// 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. + //! 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. + //! 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; - /// If true, this UniqueArray have a current ownership/responsibility of a dynamic instance. + //! If true, this UniqueArray have a current ownership/responsibility of a dynamic instance. operator bool () const; - /// This UniqueArray drops all claims of ownership/responsibility and returns the dynamic array. Now it is your responsibility to delete. + //! This UniqueArray drops all claims of ownership/responsibility and returns the dynamic array. Now it is your responsibility to delete. Type* Release(); - /// (inline) If true, this UniqueArray have a current ownership/responsibility of a dynamic array. + //! (inline) If true, this UniqueArray have a current ownership/responsibility of a dynamic array. bool HaveOwnership() const; private: @@ -193,7 +193,7 @@ namespace Utility inline ValueType Degree( const ValueType &radian ) { return radian * (180.0f / 3.1415926535897932384626433832795f); } - // SPECIALIZATIONS ////////////////////////////////////////// + // SPECIALIZATIONS //!//!//!//!//!//!//!//!//!//!//!//!//!//! template<> inline char Average( const char &valueA, const char &valueB ) { return (valueA + valueB) >> 1; } diff --git a/Code/OysterMath/LinearMath.h b/Code/OysterMath/LinearMath.h index ba427bb6..0495939d 100644 --- a/Code/OysterMath/LinearMath.h +++ b/Code/OysterMath/LinearMath.h @@ -392,7 +392,7 @@ namespace LinearAlgebra3D ::LinearAlgebra::Vector3 right = normalizedDirection.Cross( normalizedUpVector ).GetNormalized(); return ::LinearAlgebra::Matrix4x4( ::LinearAlgebra::Vector4( right, 0.0f ), ::LinearAlgebra::Vector4( right.Cross( normalizedDirection ), 0.0f ), - ::LinearAlgebra::Vector4( -normalizedDirection, 0.0f ), + ::LinearAlgebra::Vector4( normalizedDirection, 0.0f ), ::LinearAlgebra::Vector4( worldPos, 1.0f ) ); } @@ -444,8 +444,8 @@ namespace LinearAlgebra3D ScalarType c = 1 / (nearClip - farClip); return targetMem = ::LinearAlgebra::Matrix4x4( 2/width, 0, 0, 0, 0, 2/height, 0, 0, - 0, 0, -c, 0, 0, - 0, nearClip*c, 1 ); + 0, 0, -c, nearClip*c, + 0, 0, 0, 1 ); } /******************************************************************* @@ -460,15 +460,27 @@ namespace LinearAlgebra3D *******************************************************************/ template ::LinearAlgebra::Matrix4x4 & ProjectionMatrix_Perspective( const ScalarType &vertFoV, const ScalarType &aspect, const ScalarType &nearClip, const ScalarType &farClip, ::LinearAlgebra::Matrix4x4 &targetMem = ::LinearAlgebra::Matrix4x4() ) - { /** @todo TODO: not tested */ + { ScalarType fov = 1 / ::std::tan( vertFoV * 0.5f ), dDepth = farClip / (farClip - nearClip); - return targetMem = ::LinearAlgebra::Matrix4x4( fov / aspect, 0, 0, 0, + return targetMem = ::LinearAlgebra::Matrix4x4( fov / aspect, 0, 0, 0, 0, fov, 0, 0, 0, 0, dDepth, -(dDepth * nearClip), 0, 0, 1, 0 ); } + template + ::LinearAlgebra::Matrix4x4 & ProjectionMatrix_Perspective( const ScalarType &left, const ScalarType &right, const ScalarType &top, const ScalarType &bottom, const ScalarType &nearClip, const ScalarType &farClip, ::LinearAlgebra::Matrix4x4 &targetMem = ::LinearAlgebra::Matrix4x4() ) + { /** @todo TODO: not tested */ + ScalarType fov = 1 / ::std::tan( vertFoV * 0.5f ), + dDepth = farClip / (farClip - nearClip); + return targetMem = ::LinearAlgebra::Matrix4x4( 2*nearClip/(right - left), 0, -(right + left)/(right - left), 0, + 0, 2*nearClip/(top - bottom), -(top + bottom)/(top - bottom), 0, + 0, 0, dDepth, -(dDepth * nearClip), + 0, 0, 1, 0 ); + } + + template inline ::LinearAlgebra::Vector3 VectorProjection( const ::LinearAlgebra::Vector3 &vector, const ::LinearAlgebra::Vector3 &axis ) { return axis * ( vector.Dot(axis) / axis.Dot(axis) ); } diff --git a/Code/OysterMath/OysterMath.cpp b/Code/OysterMath/OysterMath.cpp index 35df5975..a82658ee 100644 --- a/Code/OysterMath/OysterMath.cpp +++ b/Code/OysterMath/OysterMath.cpp @@ -141,6 +141,16 @@ namespace Oyster { namespace Math3D return targetMem = ::LinearAlgebra3D::OrientationMatrix_LookAtPos( worldLookAt, normalizedUpVector, worldPos ); } + Float4x4 & ViewMatrix_LookAtDirection( const Float3 &normalizedDirection, const Float3 &normalizedUpVector, const Float3 &worldPos, Float4x4 &targetMem ) + { + return ::LinearAlgebra3D::InverseOrientationMatrix( ::LinearAlgebra3D::OrientationMatrix_LookAtDirection( normalizedDirection, normalizedUpVector, worldPos ), targetMem ); + } + + Float4x4 & ViewMatrix_LookAtPos( const Float3 &worldLookAt, const Float3 &normalizedUpVector, const Float3 &worldPos, Float4x4 &targetMem ) + { + return ::LinearAlgebra3D::InverseOrientationMatrix( ::LinearAlgebra3D::OrientationMatrix_LookAtPos( worldLookAt, normalizedUpVector, worldPos ), targetMem ); + } + Float4x4 & InverseOrientationMatrix( const Float4x4 &orientationMatrix, Float4x4 &targetMem ) { return ::LinearAlgebra3D::InverseOrientationMatrix( orientationMatrix, targetMem ); diff --git a/Code/OysterMath/OysterMath.h b/Code/OysterMath/OysterMath.h index 125fd29d..ab3b307f 100644 --- a/Code/OysterMath/OysterMath.h +++ b/Code/OysterMath/OysterMath.h @@ -213,6 +213,12 @@ namespace Oyster { namespace Math3D /// Oyster's native math library specialized //! @todo TODO: Add documentation and not tested Float4x4 & OrientationMatrix_LookAtPos( const Float3 &worldLookAt, const Float3 &normalizedUpVector, const Float3 &worldPos, Float4x4 &targetMem = Float4x4() ); + //! @todo TODO: Add documentation and not tested + Float4x4 & ViewMatrix_LookAtDirection( const Float3 &normalizedDirection, const Float3 &normalizedUpVector, const Float3 &worldPos, Float4x4 &targetMem = Float4x4() ); + + //! @todo TODO: Add documentation and not tested + Float4x4 & ViewMatrix_LookAtPos( const Float3 &worldLookAt, const Float3 &normalizedUpVector, const Float3 &worldPos, Float4x4 &targetMem = Float4x4() ); + /// 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() );