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