diff --git a/Code/OysterPhysics3D/Box.cpp b/Code/OysterPhysics3D/Box.cpp index 382c6732..59c98372 100644 --- a/Code/OysterPhysics3D/Box.cpp +++ b/Code/OysterPhysics3D/Box.cpp @@ -54,6 +54,12 @@ bool Box::Intersects( const ICollideable &target ) const } } +bool Box::Intersects( const ICollideable &target, Float3 &worldPointOfContact ) const +{ + //! @todo TODO: implement stub properly + return this->Intersects( target ); +} + bool Box::Contains( const ICollideable &target ) const { switch( target.type ) diff --git a/Code/OysterPhysics3D/Box.h b/Code/OysterPhysics3D/Box.h index d40c9f30..bdef2f9a 100644 --- a/Code/OysterPhysics3D/Box.h +++ b/Code/OysterPhysics3D/Box.h @@ -34,6 +34,7 @@ namespace Oyster { namespace Collision3D virtual ::Utility::DynamicMemory::UniquePointer Clone( ) const; bool Intersects( const ICollideable &target ) const; + bool Intersects( const ICollideable &target, ::Oyster::Math::Float3 &worldPointOfContact ) const; bool Contains( const ICollideable &target ) const; }; } } diff --git a/Code/OysterPhysics3D/BoxAxisAligned.cpp b/Code/OysterPhysics3D/BoxAxisAligned.cpp index f50f4b6d..f1fc2a78 100644 --- a/Code/OysterPhysics3D/BoxAxisAligned.cpp +++ b/Code/OysterPhysics3D/BoxAxisAligned.cpp @@ -57,6 +57,12 @@ bool BoxAxisAligned::Intersects( const ICollideable &target ) const } } +bool BoxAxisAligned::Intersects( const ICollideable &target, Float3 &worldPointOfContact ) const +{ + //! @todo TODO: implement stub properly + return this->Intersects( target ); +} + bool BoxAxisAligned::Contains( const ICollideable &target ) const { switch( target.type ) diff --git a/Code/OysterPhysics3D/BoxAxisAligned.h b/Code/OysterPhysics3D/BoxAxisAligned.h index a0e109b2..374c888f 100644 --- a/Code/OysterPhysics3D/BoxAxisAligned.h +++ b/Code/OysterPhysics3D/BoxAxisAligned.h @@ -29,6 +29,7 @@ namespace Oyster { namespace Collision3D virtual ::Utility::DynamicMemory::UniquePointer Clone( ) const; bool Intersects( const ICollideable &target ) const; + bool Intersects( const ICollideable &target, ::Oyster::Math::Float3 &worldPointOfContact ) const; bool Contains( const ICollideable &target ) const; }; } } diff --git a/Code/OysterPhysics3D/Frustrum.cpp b/Code/OysterPhysics3D/Frustrum.cpp index 7099c2ce..39e27318 100644 --- a/Code/OysterPhysics3D/Frustrum.cpp +++ b/Code/OysterPhysics3D/Frustrum.cpp @@ -5,8 +5,8 @@ #include "Frustrum.h" #include "OysterCollision3D.h" -using namespace Oyster::Math; -using namespace Oyster::Collision3D; +using namespace ::Oyster::Math; +using namespace ::Oyster::Collision3D; namespace PrivateStatic { @@ -218,6 +218,12 @@ bool Frustrum::Intersects( const ICollideable &target ) const } } +bool Frustrum::Intersects( const ICollideable &target, Float3 &worldPointOfContact ) const +{ + //! @todo TODO: implement stub properly + return this->Intersects( target ); +} + bool Frustrum::Contains( const ICollideable &target ) const { switch( target.type ) diff --git a/Code/OysterPhysics3D/Frustrum.h b/Code/OysterPhysics3D/Frustrum.h index afd8d8bd..c59d89f5 100644 --- a/Code/OysterPhysics3D/Frustrum.h +++ b/Code/OysterPhysics3D/Frustrum.h @@ -39,6 +39,7 @@ namespace Oyster { namespace Collision3D virtual ::Utility::DynamicMemory::UniquePointer Clone( ) const; bool Intersects( const ICollideable &target ) const; + bool Intersects( const ICollideable &target, Oyster::Math::Float3 &worldPointOfContact ) const; bool Contains( const ICollideable &target ) const; }; diff --git a/Code/OysterPhysics3D/ICollideable.h b/Code/OysterPhysics3D/ICollideable.h index 038caf0f..14676a1e 100644 --- a/Code/OysterPhysics3D/ICollideable.h +++ b/Code/OysterPhysics3D/ICollideable.h @@ -34,7 +34,7 @@ namespace Oyster { namespace Collision3D //! Contains a collection of 3D shapes virtual ~ICollideable(); virtual ::Utility::DynamicMemory::UniquePointer Clone( ) const = 0; - virtual bool Intersects( const ICollideable &target, Oyster::Math::Float3 &worldPointOfContact ) const = 0; + virtual bool Intersects( const ICollideable &target, ::Oyster::Math::Float3 &worldPointOfContact ) const = 0; virtual bool Intersects( const ICollideable &target ) const = 0; virtual bool Contains( const ICollideable &target ) const = 0; }; diff --git a/Code/OysterPhysics3D/Line.cpp b/Code/OysterPhysics3D/Line.cpp index 17fe55f7..0d3c3dcd 100644 --- a/Code/OysterPhysics3D/Line.cpp +++ b/Code/OysterPhysics3D/Line.cpp @@ -46,8 +46,29 @@ bool Line::Intersects( const ICollideable &target ) const return true; } - if( this->ray.Intersects( target ) ) if( this->ray.collisionDistance >= 0.0f ) if( this->ray.collisionDistance <= this->length ) + if( this->ray.Intersects(target) ) if( this->ray.collisionDistance >= 0.0f ) if( this->ray.collisionDistance <= this->length ) + { return true; + } + + this->ray.collisionDistance = 0.0f; + return false; +} + +bool Line::Intersects( const ICollideable &target, Float3 &worldPointOfContact ) const +{ + if( target.type == Type_universe ) + { + this->ray.collisionDistance = 0.0f; + worldPointOfContact = this->ray.origin; + return true; + } + + if( this->ray.Intersects(target) ) if( this->ray.collisionDistance >= 0.0f ) if( this->ray.collisionDistance <= this->length ) + { + worldPointOfContact = this->ray.origin + this->ray.direction * this->ray.collisionDistance; + return true; + } this->ray.collisionDistance = 0.0f; return false; diff --git a/Code/OysterPhysics3D/Line.h b/Code/OysterPhysics3D/Line.h index f062c0dc..e90e7ac4 100644 --- a/Code/OysterPhysics3D/Line.h +++ b/Code/OysterPhysics3D/Line.h @@ -30,6 +30,7 @@ namespace Oyster { namespace Collision3D virtual ::Utility::DynamicMemory::UniquePointer Clone( ) const; bool Intersects( const ICollideable &target ) const; + bool Intersects( const ICollideable &target, ::Oyster::Math::Float3 &worldPointOfContact ) const; bool Contains( const ICollideable &target ) const; }; } } diff --git a/Code/OysterPhysics3D/Universe.cpp b/Code/OysterPhysics3D/Universe.cpp index 725e2839..f1887e0b 100644 --- a/Code/OysterPhysics3D/Universe.cpp +++ b/Code/OysterPhysics3D/Universe.cpp @@ -1,6 +1,8 @@ #include "Universe.h" #include "OysterCollision3D.h" +using namespace ::Utility::Value; +using namespace ::Oyster::Math; using namespace ::Oyster::Collision3D; using namespace ::Utility::DynamicMemory; @@ -29,6 +31,46 @@ bool Universe::Intersects( const ICollideable &target ) const return true; } +bool Universe::Intersects( const ICollideable &target, Float3 &worldPointOfContact ) const +{ // universe touches everything + switch( target.type ) + { + case Type_point: + worldPointOfContact = ((Point*)&target)->center; + break; + case Type_sphere: + worldPointOfContact = ((Sphere*)&target)->center; + break; + case Type_plane: + worldPointOfContact = ((Plane*)&target)->normal * ((Plane*)&target)->phasing; + break; + case Type_box_axis_aligned: + worldPointOfContact = Average( ((BoxAxisAligned*)&target)->minVertex, ((BoxAxisAligned*)&target)->maxVertex ); + break; + case Type_box: + worldPointOfContact = ((Box*)&target)->center; + break; + case Type_frustrum: + worldPointOfContact = Average( ((Frustrum*)&target)->leftPlane.normal * ((Frustrum*)&target)->leftPlane.phasing, ((Frustrum*)&target)->rightPlane.normal * ((Frustrum*)&target)->rightPlane.phasing ); + worldPointOfContact = Average( worldPointOfContact, Average( ((Frustrum*)&target)->bottomPlane.normal * ((Frustrum*)&target)->bottomPlane.phasing, ((Frustrum*)&target)->topPlane.normal * ((Frustrum*)&target)->topPlane.phasing ) ); + worldPointOfContact = Average( worldPointOfContact, Average( ((Frustrum*)&target)->nearPlane.normal * ((Frustrum*)&target)->nearPlane.phasing, ((Frustrum*)&target)->farPlane.normal * ((Frustrum*)&target)->farPlane.phasing ) ); + break; + case Type_ray: + ((Ray*)&target)->collisionDistance = 0.0f; + worldPointOfContact = ((Ray*)&target)->origin; + break; + case Type_line: + ((Line*)&target)->ray.collisionDistance = 0.0f; + worldPointOfContact = ((Line*)&target)->ray.origin; + break; + default: + worldPointOfContact = Float3::null; + break; + } + + return true; +} + bool Universe::Contains( const ICollideable &target ) const { return true; } // universe contains everything diff --git a/Code/OysterPhysics3D/Universe.h b/Code/OysterPhysics3D/Universe.h index ff8366be..fce8179e 100644 --- a/Code/OysterPhysics3D/Universe.h +++ b/Code/OysterPhysics3D/Universe.h @@ -20,6 +20,7 @@ namespace Oyster { namespace Collision3D virtual ::Utility::DynamicMemory::UniquePointer Clone( ) const; bool Intersects( const ICollideable &target ) const; + bool Intersects( const ICollideable &target, ::Oyster::Math::Float3 &worldPointOfContact ) const; bool Contains( const ICollideable &target ) const; }; } }