PDA

View Full Version : 2D polygon intersection problem



Numberator
01-26-2010, 05:34 AM
I have to make a 2D game but I have not learnt collision detection. Are there any functions in OpenGL/GLUT lib that is able to detect if a polygon intersects or so called "collide" with each other??
If there isnt then what can I use for this intersection detection ??

For example the player must not walk through walls and objects. Not sure if polygon intersection is the right stuff to use for this case. If there are better methods out there please tell me about it

Abdallah DIB
01-26-2010, 07:35 AM
opengl is just a graphic library and it has nothing to do with collision detection.
There is a lot of collision detection library on the net like OPCODE : http://www.codercorner.com/Opcode.htm

to do collision detection, u should create a bounding box or bounding sphere for each object in the virtual scene and then test if this boxes or spheres collide together.

Kebbun
07-23-2010, 11:14 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

try this. Only strange phenomenon it has is it only works for top-left half triangle of a rectangle. :S