PDA

View Full Version : Simple collision detection problem

DmanPwns
12-13-2009, 12:35 PM
Suppose we have a moving quad.

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

scratt
12-14-2009, 03:58 AM
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..

robertopengl
12-14-2009, 10:01 AM
ok in 3d?

robertopengl
12-14-2009, 10:03 AM
Ok if it is in 3d?

DmanPwns
12-14-2009, 11:52 AM
Its just a simple 2d square. I'd like to see some code if possible.

MaxH
12-14-2009, 12:59 PM
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.

mikau
12-14-2009, 06:01 PM
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.

Kebbun
07-23-2010, 11:26 PM
#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