#include "collision.h"

int cvinsideBox(vector pos, box wall)
{
    return pos.x < wall.x+wall.w &&
           pos.y < wall.y+wall.h &&
           pos.x > wall.x &&
           pos.y > wall.y;
}

int cvoutsideBoxV(vector pos, box wall)
{
    return pos.y > wall.y+wall.h ||
           pos.y < wall.y;
}

int cvoutsideBoxH(vector pos, box wall)
{
    return pos.x > wall.x+wall.w ||
           pos.x < wall.x;
}

int cboxIntersect(box a, box b)
{
    return cvinsideBox(vnew(a.x, a.y), b) ||
           cvinsideBox(vnew(a.x+a.w, a.y), b) ||
           cvinsideBox(vnew(a.x, a.y+a.h), b) ||
           cvinsideBox(vnew(a.x+a.w, a.y+a.h), b) ||
           cvinsideBox(vnew(b.x, b.y), a) ||
           cvinsideBox(vnew(b.x+a.w, b.y), a) ||
           cvinsideBox(vnew(b.x, b.y+b.h), a) ||
           cvinsideBox(vnew(b.x+b.w, b.y+b.h), a);
}

int cvinsideSphere(vector pos, vector center, float radius)
{
    return vmagnitudeSQ(vsub(center,pos)) < pow(radius,2);
}

int cboxIntersectSphere(box a, vector center, float radius)
{
    return cvinsideSphere(vnew(a.x,a.y), center, radius) ||
           cvinsideSphere(vnew(a.x+a.w,a.y), center, radius) ||
           cvinsideSphere(vnew(a.x,a.y+a.h), center, radius) ||
           cvinsideSphere(vnew(a.x+a.w,a.y+a.h), center, radius);
}

int csphereIntersectSphere(vector a, float ra, vector b, float rb)
{
    return vmagnitudeSQ(vsub(a,b)) < pow(ra,2)+pow(rb,2);
}


