mutoo
1/9/2014 - 2:20 AM

## http://www.gamasutra.com/view/feature/3383/simple_intersection_tests_for_games.php

``````#include "vector.h"

class PLANE
{
public:

VECTOR N;
//unit normal

SCALAR D;
//distance from the plane to the origin

//from a normal and a point
PLANE( const VECTOR& p0, const VECTOR& n ):
N(n),
D(-N.dot(p0))
{}

//from 3 points
PLANE( const VECTOR& p0, const VECTOR& p1, const VECTOR& p2 ):
N((p1-p0).cross(p2-p0).unit()),
D(-N.dot(p0))
{}

//signed distance from the plane topoint 'p' along
//the unit normal
const SCALAR distanceToPoint( const VECTOR& p ) const
{
return N.dot(p) + D;
}
};

const bool SpherePlaneSweep
(
const VECTOR&	C0,	//previous position of sphere
const VECTOR&	C1,	//current position of sphere
const PLANE&	plane,	//the plane
VECTOR&	 Ci,	//position of sphere when it first touched the plane
SCALAR&	 u	//normalized time of collision
)
{
const SCALAR d0 = plane.distanceToPoint( C0 );
const SCALAR d1 = plane.distanceToPoint( C1 );

//check if it was touching on previous frame
if( fabs(d0) <= r )
{
Ci = C0;
u = 0;
return true;
}

//check if the sphere penetrated during this frame
if( d0>r && d1<r )
{
u = (d0-r)/(d0-d1);	 //normalized time
Ci = (1-u)*C0 + u*C1;	//point of first contact
return true;
}
return false;
}``````