Well, I can't use sprite collision, because:
Say the bullet is going so fast that on the next loop, it completely jumps over a ship. That bullet never "collided" with the ship, per se, but in reality it went through it, so technically collision should have occured.
The same goes for a ship that's moving very fast.
The way I'm doing it is pure math. The sprite is just there for looks, nothing more. The bullet produces a line segment (ax,ay) - (bx,by) and the ship being checked provides a point (cx,cy)
Now, with this information, I made a "distance to line segment" function to check how far away the ship's center is from the bullet. If the distance is <= the ship's size/2 + bullet's width/2, then the bullet must have hit the ship.
Here's the "distance to line segment" function I wrote:
float disttolineseg(float cx,float cy,float ax,float ay,float bx,float by) {
float rn = (cx-ax)*(bx-ax) + (cy-ay)*(by-ay);
float rd = (bx-ax)*(bx-ax) + (by-ay)*(by-ay);
float r = rn / rd;
float px = ax + r*(bx-ax);
float py = ay + r*(by-ay);
float s = ((ay-cy)*(bx-ax)-(ax-cx)*(by-ay)) / rd;
float distto = abs(s)*sqrt(rd);
if (r<0 || r>1) {
float dist1 = (cx-ax)*(cx-ax) + (cy-ay)*(cy-ay);
float dist2 = (cx-bx)*(cx-bx) + (cy-by)*(cy-by);
if (dist1 < dist2) {
distto = sqrt(dist1);
} else {
distto = sqrt(dist2);
}
}
return (distto);
}
So, what I'm hoping for is some sort of technique, maybe, for checking tons of information like this.
The one and only,