src/Collision.js
/**
 * Helper intersection function. Intersects a ray with a sphere.
 *
 * @param {Ray2}   ray      Ray for intersecting
 * @param {Circle} circle   Circle to be intersected
 * @return {boolean}        If the ray intersects the sphere
 */
Helper intersection function. Intersects a ray with a sphere.
@param {Ray2}   ray      Ray for intersecting
@param {Circle} circle   Circle to be intersected
@return {boolean}        If the ray intersects the sphere
Helper intersection function . Intersects a ray with a sphere . @param { Ray2 } ray Ray for intersecting @param { Circle } circle Circle to be intersected @return { boolean } If the ray intersects the sphere
function IntersectRaySphere(ray, circle) {
    closure = function () {
        return localCollisionTime;   
    };
    var m = new Vec2(ray.point.x, ray.point.y);
    m.sub(circle.c);
    var a = ray.normal.Dot(ray.normal);
    var b = m.Dot(ray.normal);
    var c = m.Dot(m) - circle.r * circle.r;
    if (c > 0 && b > 0) {
        return false;
    }
    var discr = b * b - a * c;
    if (discr < 0) {
        return false;
    }
    var localCollisionTime = (-b - Math.sqrt(discr)) / a;
    if (localCollisionTime < 0 || localCollisionTime > 1) {
        return false;
    }
    return true;
}
function IntersectRaySphere ( ray , circle ) { closure = function ( ) { return localCollisionTime ; } ; var m = new Vec2 ( ray . point . x , ray . point . y ) ; m . sub ( circle . c ) ; var a = ray . normal . Dot ( ray . normal ) ; var b = m . Dot ( ray . normal ) ; var c = m . Dot ( m ) - circle . r * circle . r ; if ( c > 0 && b > 0 ) { return false ; } var discr = b * b - a * c ; if ( discr < 0 ) { return false ; } var localCollisionTime = ( - b - Math . sqrt ( discr ) ) / a ; if ( localCollisionTime < 0 || localCollisionTime > 1 ) { return false ; } return true ; }
Helper intersection function.
Helper intersection function .
// Is the ray heading towards the circle? Otherwise it won't intersect // Do the ray intersect the sphere if the ray was infinitely long? // Check that the collision time is on the ray