2013-11-06 22:52:00 +01:00
|
|
|
#include "Sphere.h"
|
2013-11-12 12:33:52 +01:00
|
|
|
#include "OysterCollision3D.h"
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-10 02:27:16 +01:00
|
|
|
using namespace ::Oyster::Collision3D;
|
2013-11-06 22:52:00 +01:00
|
|
|
using namespace ::Oyster::Math;
|
|
|
|
|
2013-11-27 15:20:29 +01:00
|
|
|
Sphere::Sphere( ) : ICollideable(Type_sphere)
|
|
|
|
{
|
2013-12-18 08:57:27 +01:00
|
|
|
this->center = Float4::standard_unit_w;
|
2013-11-27 15:20:29 +01:00
|
|
|
this->radius = 0.0f;
|
|
|
|
}
|
|
|
|
|
2013-12-18 08:57:27 +01:00
|
|
|
Sphere::Sphere( const Float3 &p, const Float &r ) : ICollideable(Type_sphere)
|
2013-11-27 15:20:29 +01:00
|
|
|
{
|
2013-12-18 08:57:27 +01:00
|
|
|
this->center = Float4( p, 1.0f );
|
|
|
|
this->radius = r;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sphere::Sphere( const Float4 &p, const Float &r ) : ICollideable(Type_sphere)
|
|
|
|
{
|
|
|
|
this->center = p;
|
|
|
|
this->radius = r;
|
2013-11-27 15:20:29 +01:00
|
|
|
}
|
|
|
|
|
2013-11-10 02:27:16 +01:00
|
|
|
Sphere::~Sphere( ) {}
|
2013-11-06 22:52:00 +01:00
|
|
|
|
|
|
|
Sphere & Sphere::operator = ( const Sphere &sphere )
|
|
|
|
{
|
|
|
|
this->center = sphere.center;
|
|
|
|
this->radius = sphere.radius;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-11-13 14:50:08 +01:00
|
|
|
::Utility::DynamicMemory::UniquePointer<ICollideable> Sphere::Clone( ) const
|
2013-12-17 14:14:54 +01:00
|
|
|
{
|
|
|
|
return ::Utility::DynamicMemory::UniquePointer<ICollideable>( new Sphere(*this) );
|
|
|
|
}
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-25 14:22:38 +01:00
|
|
|
bool Sphere::Intersects( const ICollideable &target ) const
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
2013-11-25 14:22:38 +01:00
|
|
|
switch( target.type )
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
2013-12-17 14:14:54 +01:00
|
|
|
case Type_universe: return true;
|
|
|
|
case Type_point: return Utility::Intersect( *this, (const Point&)target );
|
|
|
|
case Type_ray: return Utility::Intersect( *this, (const Ray&)target, ((const Ray&)target).collisionDistance );
|
|
|
|
case Type_sphere: return Utility::Intersect( *this, (const Sphere&)target );
|
|
|
|
case Type_plane: return Utility::Intersect( (const Plane&)target, *this );
|
|
|
|
//case Type_triangle: return false; // TODO:
|
|
|
|
case Type_box_axis_aligned: return Utility::Intersect( (const BoxAxisAligned&)target, *this );
|
|
|
|
case Type_box: return Utility::Intersect( (const Box&)target, *this );
|
|
|
|
//case Type_frustrum: return false; // TODO:
|
|
|
|
default: return false;
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 08:57:27 +01:00
|
|
|
bool Sphere::Intersects( const ICollideable &target, Float4 &worldPointOfContact ) const
|
2013-12-16 09:38:04 +01:00
|
|
|
{
|
|
|
|
switch( target.type )
|
|
|
|
{
|
2013-12-17 14:14:54 +01:00
|
|
|
case Type_universe:
|
|
|
|
worldPointOfContact = this->center;
|
|
|
|
return true;
|
|
|
|
case Type_point: return Utility::Intersect( *this, (const Point&)target, worldPointOfContact );
|
|
|
|
case Type_ray: return Utility::Intersect( *this, (const Ray&)target, ((const Ray&)target).collisionDistance, worldPointOfContact );
|
|
|
|
case Type_sphere: return Utility::Intersect( *this, (const Sphere&)target, worldPointOfContact );
|
|
|
|
case Type_plane: return Utility::Intersect( (const Plane&)target, *this, worldPointOfContact );
|
|
|
|
//case Type_triangle: return false; // TODO:
|
|
|
|
case Type_box_axis_aligned: return Utility::Intersect( (const BoxAxisAligned&)target, *this, worldPointOfContact );
|
|
|
|
case Type_box: return Utility::Intersect( (const Box&)target, *this, worldPointOfContact );
|
|
|
|
//case Type_frustrum: return false; // TODO:
|
|
|
|
default:
|
|
|
|
worldPointOfContact = Float3::null;
|
2013-12-16 09:38:04 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-25 14:22:38 +01:00
|
|
|
bool Sphere::Contains( const ICollideable &target ) const
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
2013-11-25 14:22:38 +01:00
|
|
|
switch( target.type )
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
2013-12-17 14:14:54 +01:00
|
|
|
case Type_point: return Utility::Intersect( *this, (const Point&)target );
|
|
|
|
case Type_sphere: return Utility::Contains( *this, (const Sphere&)target );
|
|
|
|
//case Type_triangle: return false; // TODO:
|
|
|
|
//case Type_box_axis_aligned: return false; // TODO:
|
|
|
|
//case Type_box: return false; // TODO:
|
|
|
|
//case Type_frustrum: return false; // TODO:
|
|
|
|
default: return false;
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
2014-02-03 15:48:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Float Sphere::TimeOfContact( const ICollideable &deuterStart, const ICollideable &deuterEnd ) const
|
|
|
|
{
|
|
|
|
if( deuterStart.type != deuterEnd.type )
|
|
|
|
return -1.0f;
|
|
|
|
|
|
|
|
switch( deuterStart.type )
|
|
|
|
{
|
|
|
|
//case Type_point: // not implemented
|
|
|
|
//case Type_sphere: // not implemented
|
|
|
|
//case Type_box: // not implemented
|
|
|
|
case Type_universe: return 0.0f;
|
|
|
|
default: return 1.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Oyster { namespace Math
|
|
|
|
{
|
|
|
|
Sphere & Nlerp( const Sphere &start, const Sphere &end, Float t, Sphere &targetMem )
|
|
|
|
{
|
|
|
|
Float4 i = Lerp( Float4(start.center.xyz, start.radius), Float4(end.center.xyz, end.radius), t );
|
|
|
|
targetMem.center.xyz = i.xyz;
|
|
|
|
targetMem.radius = i.w;
|
|
|
|
return targetMem;
|
|
|
|
}
|
|
|
|
} }
|