Originally Posted by
Salem
Code:
if ( ship.right < roid.left || ship.left > roid.right ||
ship.top < roid.bottom || ship.bottom > roid.top ) {
// no overlap
} else {
// overlap
}
Why omit the circle test and not omit the box test? With the above method, each frame you execute, for example, this for the collision test:
Code:
ship.right = shipX + 0.6f;
ship.left = shipX - 0.6f;
ship.top = shipY - 0.6f;
ship.bottom = shipY + 0.6f;
roid.right = roidX + 0.8f;
roid.left = roidX - 0.8f;
roid.top = roidY - 0.8f;
roid.bottom = roidY + 0.8f;
if ( ship.right < roid.left || ship.left > roid.right ||
ship.top < roid.bottom || ship.bottom > roid.top ) {
// no overlap
} else {
// overlap
}
in which you do 8 additions/subtractions, 4 comparisions, and 3 logical operations.
If you use the optimized circle test, your code would be like this:
Code:
dx = roidX - shipX;
dx *= dx;
dy = roidY - shipY;
dy *= dy;
if( dx + dy - shipConstant - roidConstant)
{
//overlap
}
in which you need 5 additions/subtractions, 2 multiplications and 1 comparision.
Now what would be faster?