Alrighty well straight to it, I'm using this guide as I learn SDL and I'm having difficulties with the per pixel collision part. My implementation to this is slightly different to the guides and it is causing some unwanted issues. The issue being the moving ball goes straight through the stationary ball (it's meant to be solid and block the moving ball as you might expect). I know that the rectangular collision boxes are in the proper places because I've recoloured the moving ball to be white (the ball.bmp is loaded red in colour) and the stationary ball to be blue, The collision boxes on top of each ball has been recoloured.
My Collision detection function is as follow
bool checkCol(std::vector<SDL_Rect> A,std::vector<SDL_Rect> B){
int TOP1, TOP2;
int BOTTOM1, BOTTOM2;
int LEFT1, LEFT2;
int RIGHT1,开发者_如何学JAVA RIGHT2;
for (int i = 0; i < A.size(); i++){
TOP1 = A[ i ].y;
BOTTOM1 = A[ i ].y + A[ i ].h;
LEFT1 = A[ i ].x;
RIGHT1 = A[ i ].x + A[ i ].w;
for (int j = 0; j < B.size(); j++){
TOP2 = B[ i ].y;
BOTTOM2 = B[ i ].y + B[ i ].h;
LEFT2 = B[ i ].x;
RIGHT2 = B[ i ].x + B[ i ].w;
if ( RIGHT1 <= LEFT2 ){ return false; }
if ( LEFT1 >= RIGHT2 ){ return false; }
if ( TOP1 >= BOTTOM2 ){ return false; }
if ( BOTTOM1 <= TOP2 ){ return false; }
}
}
return true;
}
here is the full source code(too large to post here) and executable(linux).
Could someone please tell me where I've gone wrong?.
[edit] Ignore the rectangle in the middle.
Looks like the algorithm logic is incorrect. When you have a list of conditions and you want to check that all of them are false, you should not do return false;
as soon as you have found one which is false.
精彩评论