EDITED AGAIN 如果你希望對象停止之前實際碰撞而不共享相同的實際邊緣像素,試試這個:
bool Collision(int x1,int y1,int h1,int w1,int x2,int y2,int h2,int w2){
if((x1 + w1) >= (x2 - 1) || // object 1 hitting left side of object 2
(x1 - 1) <= (x2 + w2) || // object 1 hitting right side of object 2
(y1 - 1) <= (y2 + h2) || // Object 1 hitting bottom of object 2 (assuming your y goes from top to bottom of screen)
(y1 + h1) >= (y2 - 1)) // Object 1 hitting top of object 2
return 1;
return 0;
}
OR
int Collision(int x1,int y1,int h1,int w1,int x2,int y2,int h2,int w2){
if((x1 + w1) >= (x2 - 1)) return 1; // object 1 hitting left side of object 2
if((x1 - 1) <= (x2 + w2)) return 2; // object 1 hitting right side of object 2
if((y1 - 1) <= (y2 + h2)) return 3; // Object 1 hitting bottom of object 2 (assuming your y goes from top to bottom of screen)
if((y1 + h1) >= (y2 - 1)) return 4; // Object 1 hitting top of object 2
return 0; // no collision
}
這樣他們絕不應該共享相同的像素。
ORIGINAL 我認爲,你想要去的這更像是:
bool Collision(int x1,int y1,int h1,int w1,int x2,int y2,int h2,int w2){
if((x1 + w1) >= x2 || // object 1 hitting left side of object 2
x1 <= (x2 + w2) || // object 1 hitting right side of object 2
y1 <= (y2 + h2) || // Object 1 hitting bottom of object 2 (assuming your y goes from top to bottom of screen)
(y1 + h1) >= y2) // Object 1 hitting top of object 2
return 1;
return 0;
}
這個答案假定您想知道,當他們兩個Object完好佔據相同的座標邊緣(即小於/大於或等於沒有等於)
這個答案不返回那邊是交互邊緣。如果你想這樣做,那麼你可以按照這些方法做更多的事情。
int Collision(int x1,int y1,int h1,int w1,int x2,int y2,int h2,int w2){
if((x1 + w1) >= x2) return 1; // object 1 hitting left side of object 2
if(x1 <= (x2 + w2)) return 2; // object 1 hitting right side of object 2
if(y1 <= (y2 + h2)) return 3; // Object 1 hitting bottom of object 2 (assuming your y goes from top to bottom of screen)
if((y1 + h1) >= y2) return 4; // Object 1 hitting top of object 2
return 0; // no collision
}
現在在外面,你只需要您的邊緣檢測的情況下解碼的1 - 4
你的問題是什麼? – 2014-10-03 21:13:11
如何使它在碰撞後停止,並且不會繼續通過對象 – 2014-10-03 21:18:19
@ Dr.President if(碰撞(x1,y1,h1,w1,x2,y2,h2,w2))StopAndNotContinueThroughTheObject(); '? – Marlon 2014-10-03 21:19:48