# Thread: Simple collision detection problem

1. ## Simple collision detection problem

Suppose we have a moving quad.

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

2. ## Re: Simple collision detection problem

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?

4. ## Re: Simple collision detection problem

Ok if it is in 3d?

5. ## Re: Simple collision detection problem

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

6. ## Re: Simple collision detection problem

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.

7. ## Re: Simple collision detection problem

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.

8. ## Re: Simple collision detection problem

#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 &amp;&amp; ball[k].x-BSIZE < ball[l].x+BSIZE)&amp;&amp;
(ball[k].y+BSIZE > ball[l].x-BSIZE &amp;&amp; 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+2*BSIZE;
}else{
ball[l].x=ball[k].x-2*BSIZE;
}
tmp_dx=ball[l].dx;
ball[l].dx=ball[k].dx*PERCENT_AFTER_BOUNCE;
ball[k].dx=tmp_dx*PERCENT_AFTER_BOUNCE;
}else{
if(ball[l].y > ball[k].y){
ball[l].y=ball[k].y+2*BSIZE;
}else{
ball[l].y=ball[k].y-2*BSIZE;
}
tmp_dy=ball[l].dy;
ball[l].dy=ball[k].dy*PERCENT_AFTER_BOUNCE;
ball[k].dy=tmp_dy*PERCENT_AFTER_BOUNCE;
}
num_bounce++;
ball[k].collided=1;
ball[l].collided=1;
break;
}
}
}
#endif

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•