# Simple collision detection problem

Suppose we have a moving quad.

How can we detect collision of the quad with the borders of a window?

Are you working in 2D or 3D?

If it’s 2D and you have an Ortho view then it could be as simple as an x,y range check.

If it’s 3D then you the solution is somewhat different…

ok in 3d?

Ok if it is in 3d?

Its just a simple 2d square. I’d like to see some code if possible.

You’d get a more enthusiastic response if you posted your code and asked for help. That way we’d know that you’ve put some effort into solving the problem. Also you could post a screen capture. What does your program do now? Does it generate a static square on the screen? Forgetting about collision detection for a moment, can you move the square using a clock function, or interactively via the keyboard? If you can do all that, solving the collision detection problem would be trivial. If you can’t do it, then you have much more basic problems than collision detection.

is it a 2d square in a 2d world? or a 2d square in a 3d world?

either way… you would need to consider both your modelview transformation matrix, and your perspective transformation matrix.

#ifdef COLLISION
float tmp_dx, tmp_dy;
for(int k=0; k<BNUM-1; k++){
for (int l=k+1; l<BNUM; l++){
if((ball[k].x+BSIZE > ball[l].x-BSIZE && ball[k].x-BSIZE < ball[l].x+BSIZE)&&
(ball[k].y+BSIZE > ball[l].x-BSIZE && ball[k].y-BSIZE < ball[l].y+BSIZE)){
if(ball[k].x - ball[l].x > ball[k].y - ball[l].y){
if(ball[l].x > ball[k].x){
ball[l].x=ball[k].x+2BSIZE;
}else{
ball[l].x=ball[k].x-2
BSIZE;
}
tmp_dx=ball[l].dx;
ball[l].dx=ball[k].dxPERCENT_AFTER_BOUNCE;
ball[k].dx=tmp_dx
PERCENT_AFTER_BOUNCE;
}else{
if(ball[l].y > ball[k].y){
ball[l].y=ball[k].y+2BSIZE;
}else{
ball[l].y=ball[k].y-2
BSIZE;
}
tmp_dy=ball[l].dy;
ball[l].dy=ball[k].dyPERCENT_AFTER_BOUNCE;
ball[k].dy=tmp_dy
PERCENT_AFTER_BOUNCE;
}
num_bounce++;
ball[k].collided=1;
ball[l].collided=1;
break;
}
}
}
#endif