2013-11-06 22:52:00 +01:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// Created by Dan Andersson 2013
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "Ray.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;
|
|
|
|
using namespace ::Oyster::Math3D;
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-27 15:20:29 +01:00
|
|
|
Ray::Ray( ) : ICollideable(Type_ray)
|
|
|
|
{
|
2013-12-18 08:57:27 +01:00
|
|
|
this->origin = Float4::standard_unit_w;
|
|
|
|
this->direction = Float4::standard_unit_z;
|
2013-11-27 15:20:29 +01:00
|
|
|
this->collisionDistance = 0.0f;
|
|
|
|
}
|
|
|
|
|
2013-12-18 08:57:27 +01:00
|
|
|
Ray::Ray( const Float3 &o, const Float3 &d ) : ICollideable(Type_ray)
|
2013-11-27 15:20:29 +01:00
|
|
|
{
|
2013-12-18 08:57:27 +01:00
|
|
|
this->origin = Float4( o, 1.0f );
|
|
|
|
this->direction = Float4( d, 0.0f );
|
2013-11-27 15:20:29 +01:00
|
|
|
this->collisionDistance = 0.0f;
|
|
|
|
}
|
|
|
|
|
2013-12-18 09:25:04 +01:00
|
|
|
Ray::Ray( const Float4 &o, const Float4 &d ) : ICollideable(Type_ray)
|
|
|
|
{
|
|
|
|
this->origin = o;
|
|
|
|
this->direction = d;
|
|
|
|
this->collisionDistance = 0.0f;
|
|
|
|
}
|
|
|
|
|
2013-11-10 02:27:16 +01:00
|
|
|
Ray::~Ray( ) {}
|
2013-11-06 22:52:00 +01:00
|
|
|
|
|
|
|
Ray & Ray::operator = ( const Ray &ray )
|
|
|
|
{
|
|
|
|
this->origin = ray.origin;
|
|
|
|
this->direction = ray.direction;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-11-13 14:50:08 +01:00
|
|
|
::Utility::DynamicMemory::UniquePointer<ICollideable> Ray::Clone( ) const
|
2013-11-27 15:20:29 +01:00
|
|
|
{
|
|
|
|
return ::Utility::DynamicMemory::UniquePointer<ICollideable>( new Ray(*this) );
|
|
|
|
}
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-25 14:22:38 +01:00
|
|
|
bool Ray::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-11-10 02:27:16 +01:00
|
|
|
case Type_universe:
|
|
|
|
this->collisionDistance = 0.0f;
|
|
|
|
return true;
|
2013-12-17 14:14:54 +01:00
|
|
|
case Type_point: return Utility::Intersect( *this, (const Point&)target, this->collisionDistance );
|
|
|
|
case Type_ray: return Utility::Intersect( *this, (const Ray&)target, this->collisionDistance, ((const Ray&)target).collisionDistance );
|
|
|
|
case Type_sphere: return Utility::Intersect( (const Sphere&)target, *this, this->collisionDistance );
|
|
|
|
case Type_plane: return Utility::Intersect( (const Plane&)target, *this, this->collisionDistance );
|
2013-12-18 08:57:27 +01:00
|
|
|
//case Type_triangle: return false; // TODO:
|
2013-12-17 14:14:54 +01:00
|
|
|
case Type_box_axis_aligned: return Utility::Intersect( (const BoxAxisAligned&)target, *this, this->collisionDistance );
|
|
|
|
case Type_box: return Utility::Intersect( (const Box&)target, *this, this->collisionDistance );
|
2013-12-18 08:57:27 +01:00
|
|
|
//case Type_frustrum: return false; // TODO:
|
2013-12-17 14:14:54 +01:00
|
|
|
default: return false;
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 08:57:27 +01:00
|
|
|
bool Ray::Intersects( const ICollideable &target, Float4 &worldPointOfContact ) const
|
2013-12-16 09:38:04 +01:00
|
|
|
{
|
|
|
|
switch( target.type )
|
|
|
|
{
|
|
|
|
case Type_universe:
|
|
|
|
this->collisionDistance = 0.0f;
|
2013-12-17 14:14:54 +01:00
|
|
|
worldPointOfContact = this->origin;
|
2013-12-16 09:38:04 +01:00
|
|
|
return true;
|
2013-12-17 14:14:54 +01:00
|
|
|
case Type_point: return Utility::Intersect( *this, (const Point&)target, this->collisionDistance, worldPointOfContact );
|
|
|
|
case Type_ray: return Utility::Intersect( *this, (const Ray&)target, this->collisionDistance, ((const Ray&)target).collisionDistance, worldPointOfContact );
|
|
|
|
case Type_sphere: return Utility::Intersect( (const Sphere&)target, *this, this->collisionDistance, worldPointOfContact );
|
|
|
|
case Type_plane: return Utility::Intersect( (const Plane&)target, *this, this->collisionDistance, worldPointOfContact );
|
|
|
|
//case Type_triangle: return false; // TODO:
|
|
|
|
case Type_box_axis_aligned: return Utility::Intersect( (const BoxAxisAligned&)target, *this, this->collisionDistance, worldPointOfContact );
|
|
|
|
case Type_box: return Utility::Intersect( (const Box&)target, *this, this->collisionDistance, 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 Ray::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, this->collisionDistance );
|
|
|
|
case Type_ray: return Utility::Contains( *this, (const Ray&)target );
|
|
|
|
default: return false;
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
2013-11-10 02:27:16 +01:00
|
|
|
}
|