struct dsCollisionResult
{
float distance;
};
struct dsCollisionCircle
{
float radius;
};
struct dsCollisionBoundingBox
{
float width, height;
};
struct dsPoint2
{
float x, y;
};
struct dsPoint3
{
float x, y, z;
};
struct dsCollisionPoly2D
{
vector<dsPoint2> points;
};
// object data calculations
float boundingBoxCenterX(dsCollisionBoundingBox &bb)
{
return (bb.width / 2.0f);
}
float boundingBoxCenterY(dsCollisionBoundingBox &bb)
{
return (bb.height / 2.0f);
}
// result analysis
bool hasCollided(dsCollisionResult &result)
{
return (result.distance < 0);
}
// collision circle vs circle
bool checkCollisionCircleVsCircle(dsCollisionResult &result, dsCollisionCircle* cir1, dsCollisionCircle* cir2)
{
result.distance = distance(objectA, objectB) - (cir1.radius + cir2.radius);
return (result.distance < 0);
}
// collision bounding box vs bounding box
bool checkCollisionBoundingBoxVsBoundingBox(dsCollisionResult &result, dsCollisionBoundingBox* bb1, dsCollisionBoundingBox* bb2)
{
return (abs(objectA.x - objectB.x) - (bb1.width+bb2.width)) < 0
&& (abs(objectA.y - objectB.y) - (bb1.height+bb2.height)) < 0;
//result.distance =
}
// collision bounding box vs circle
bool checkCollisionBoundingBoxVsBoundingBox(dsCollisionResult &result, dsCollisionBoundingBox* bb, dsCollisionCircle* cir)
{
// objectA in this case contains bb, and objectB conatains cir
// temp point data
dsPoint2 Bc;
// STEP ONE
// CALC Bc
if(objectB.x > objectA.x+boundingBoxCenterX(bb))
// cir is to the right of bb's right border
{
// Bc cannot get any farther out right than this
Bc.x = objectA.x+boundingBoxCenterX(bb);
}
else if(objectB.x < objectA.x-boundingBoxCenterX(bb))
// cir is to the left of bb's top border
{
// Bc cannot get any farther out left than this
Bc.x = objectA.x-boundingBoxCenterX(bb);
}
else
// now you're somewhere inbetween the left and right edges
// Bc's x should reflect the x of the circle
{
Bc.x = objectB.x;
}
if(objectB.y > objectA.y+boundingBoxCenterY(bb))
// cir is under bb's bottom border
{
// Bc cannot get any farther down than this
Bc.y = objectA.y+boundingBoxCenterY(bb);
}
else if(objectB.y < objectA.y-boundingBoxCenterY(bb))
// cir is above bb's top border
{
// Bc cannot get any farther above
Bc.y = objectA.y-boundingBoxCenterY(bb);
}
else
// now you're somewhere inbetween the top and bottom edges
// Bc's y should reflect the y of the circle
{
Bc.y = objectB.y;
}
// STEP TWO
// DISTANCE FACTOR Bc AND CIRCLE
return (distance(objectA, objectB) - cir.radius) < 0;
}