Collision Detection in OpenGL

This is a discussion on Collision Detection in OpenGL within the Game Programming forums, part of the General Programming Boards category; 1.) How is it done? 2.) Is it hard to learn how to do? 3.) Could you link me to ...

  1. #1
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    Question Collision Detection in OpenGL

    1.) How is it done?
    2.) Is it hard to learn how to do?
    3.) Could you link me to some good tutorials on it?

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    1.)
    2.) fairly
    3.) did you search Google? edit: or NeHe?
    Away.

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    have u followed the links in the links thread at the top of this forum?

  4. #4
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Originally posted by RoD
    have u followed the links in the links thread at the top of this forum?
    Most of 'em, yes.

    I'm actually afraid to read NeHe's tut on Collision Detection...it might just blow my fragile mind. :-\

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    When you say something like, "how is collision detection done in OpenGL" that's pretty vague. CD in OpenGL is the same as it would be in D3D except for the function calls. Do you mean 3D collisions? Because you can make 2D games in OpenGL and the collision detection is way different then in a 3D game. So, what kind of game are you wondering about?
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  6. #6
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    here are 2 easy ways.

    draw a box around every object.
    if any of the boxes overlap then you have a collision.

    draw a circule around every object.

    d = distance between 2 objects.
    a = radis of the circule on the first object.
    b = radis of the circule on the second object.

    if ( a + b >= d ) colission
    else no colission

    forgive the spelling.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  7. #7
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Originally posted by Nor
    Things
    Would there be any particular functions you'd use?

    Originally posted by MrWizard
    So, what kind of game are you wondering about?
    I want to learn how to detect collisions between two simple 2D rectangles. If I were going to start making a game, it'd be 2D.
    Last edited by Krak; 08-06-2003 at 07:45 AM.

  8. #8
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    >>Would there be any particular functions you'd use?

    Of course! One you wrote, which does exactly what Nor just said. There isn't a function that you can just use to make every part of a game. You have to actually program things. Look at what Nor said. You can't get much more specific than that without writing the code.
    draw a circule around every object.

    d = distance between 2 objects.
    a = radis of the circule on the first object.
    b = radis of the circule on the second object.

    if ( a + b >= d ) colission
    else no colission
    Away.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    You want to click me...you can't wait to click me...oh yes, you can feel it now...yes...yes....

    If your just talking about basic bounding box collision detection, then this or this may help you. Those are Allegro specific, I know, but you'll get the idea...
    Last edited by funkydude9; 08-06-2003 at 11:04 AM.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  10. #10
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Code:
    take your pick.
    //#define twod_collision 0
    /* or */
    //#define threed_collision 0
    
    bool detectcollision(int x1, int y1, int z1,int radis1, int x2, int y2, int z2, int radis2)
    {
        int xd, yd, zd, Distance;
        
        //2d
        #ifdef twod_collision
    	    xd = x2-x1;
    	    yd = y2-y1;    	
    	    Distance = SquareRoot(xd*xd + yd*yd)
        #endif
        
        //3d
        #ifdef threed_collision
    	    xd = x2-x1;
    	    yd = y2-y1;
    	    zd = z2-z1;
    	    Distance = SquareRoot(xd*xd + yd*yd + zd*zd);
        #endif
    	
    	if( radis1 + radis2 >= Distance)
    	    return true; //collision
    	return false;    //no collision    
    }
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    1
    You can do it better with this libary i think:

    http://www.cs.unc.edu/~geom/collide/index.shtml

    with this you can detect all collisions and more.
    Ist free and open. But not quite simple.

    Do it! Have much fun with it

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Krak View Post
    I want to learn how to detect collisions between two simple 2D rectangles. If I were going to start making a game, it'd be 2D.
    Have you gotten a 2D rectangle to move in a straight line yet? The first collision you probably want to detect is the edge of the window...after that you will find it Pretty Damn Easy. Look:
    Code:
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include <GL/glut.h>
    #include <GL/glext.h>
    #include <stdlib.h>
    
    /* bouncing-blocks.c */
    
    #define VOL 150
    #define NP 7
    
    struct square {
            int x;  // x position
            int y;  // y position
            int w;  // size
            int v;  // vertical velocity
            int vd; // vertical direction (1 or -1)
            int h;  // horizontal velocity
            int hd; // horizontal direction (1 or -1)
    } *piec;
    
    void scene() {
    	glClear(GL_COLOR_BUFFER_BIT);
    	glColor3f(0.0f, 1.0f, 0.0f);
    	glRecti(piec[0].x,piec[0].y,piec[0].x+piec[0].w,piec[0].y-piec[0].w);
    	glColor3f(0.0f, 0.5f, 0.0f);
    	glRecti(piec[1].x,piec[1].y,piec[1].x+piec[1].w,piec[1].y-piec[1].w);
    	glColor3f(0.0f, 0.0f, 0.5f);
    	glRecti(piec[2].x,piec[2].y,piec[2].x+piec[2].w,piec[2].y-piec[2].w);
    	glColor3f(1.0f, 1.0f, 0.0f);
    	glRecti(piec[3].x,piec[3].y,piec[3].x+piec[3].w,piec[3].y-piec[3].w);
    	glColor3f(0.5f, 0.0f, 0.0f);
    	glRecti(piec[4].x,piec[4].y,piec[4].x+piec[4].w,piec[4].y-piec[4].w);
    	glColor3f(0.0f, 0.0f, 0.0f);
    	glRecti(piec[5].x,piec[5].y,piec[5].x+piec[5].w,piec[5].y-piec[5].w);
    	glColor3f(1.0f, 1.0f, 1.0f);
    	glRecti(piec[6].x,piec[6].y,piec[6].x+piec[6].w,piec[6].y-piec[6].w);
    	glutSwapBuffers();
    }
    
    void setup(float R, float G, float B, GLdouble P) {
    	GLdouble N=0-P;
    	glClearColor(R,G,B,1.0f);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	glOrtho(N,P,N,P,N,P);
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    }
    
    void Vcollision (int P) {
    	int i;
    	for (i=0;i<NP;i++) {
    		if (i==P) continue;
    		if ((piec[P].y==piec[i].y-piec[i].w) && (piec[P].x<=piec[i].x+piec[i].w) && 
    			(piec[P].x+piec[P].w>=piec[i].x)) {piec[P].vd=-1;
    			piec[P].y-=1;
    			if (piec[i].vd==-1) piec[i].vd=1;
    			if (piec[P].v>=piec[i].v) piec[i].v--;
    			else {piec[P].v++;piec[i].v++;}}
    		else if ((piec[P].y-piec[P].w==piec[i].y) && (piec[P].x<=piec[i].x+piec[i].w) && 
    			(piec[P].x+piec[P].w>=piec[i].x)) {piec[P].vd=1;
    			piec[P].y+=1;
    			if (piec[i].vd==1) piec[i].vd=-1;
    			if (piec[P].v>=piec[i].v) piec[i].v--;
    			else {piec[P].v++;piec[i].v++;}}
    	}
    }
    
    void Hcollision (int P) {
    	int i;
    	for (i=0;i<NP;i++) {
    		if (i==P) continue;
    		if ((piec[P].x==piec[i].x+piec[i].w) && (piec[P].y>=piec[i].y-piec[i].w) && 
    			(piec[P].y-piec[P].w<=piec[i].y)) {piec[P].hd=1;
    			piec[P].x+=1;
    			if (piec[i].hd==1) piec[i].hd=-1;
    			if (piec[P].h>=piec[i].h) piec[i].h--;
    			else {piec[P].h++;piec[i].h++;}}
    		else if ((piec[P].x+piec[P].w==piec[i].x) && (piec[P].y>=piec[i].y-piec[i].w) && 
    			(piec[P].y-piec[P].w<=piec[i].y)) {piec[P].hd=-1;
    			piec[P].x-=1;
    			if (piec[i].hd==-1) piec[i].hd=1;
    			if (piec[P].h>=piec[i].h) piec[i].h--;
    			else {piec[P].h++;piec[i].h++;}}
    	}
    }
    
    void move(int T) {
    	int rendintrv, i;
    	for (rendintrv=0; rendintrv<15; rendintrv++) {
    		if (T==1000001) T=1;
    		for (i=0; i<NP; i++) {
    			if ((piec[i].v>0) && (T%piec[i].v==0)) {piec[i].y+=piec[i].vd;
    				if (piec[i].y-piec[i].w<=-VOL) {piec[i].vd=1;
    					piec[i].y=-VOL+piec[i].w+1; piec[i].v--;}
    				else if (piec[i].y>=VOL) {piec[i].vd=-1;
    					piec[i].y=VOL-1; piec[i].v--;}
    				else Vcollision(i);
    			}
    			if ((piec[i].h>0) && (T%piec[i].h==0)) {piec[i].x+=piec[i].hd; 
    				if (piec[i].x+piec[i].w>=VOL) {piec[i].hd=-1;
    					piec[i].x=VOL-piec[i].w-1; piec[i].h--;}
    				else if (piec[i].x<=-VOL) {piec[i].hd=1;
    					piec[i].x=-VOL+1; piec[i].h--;}
    				else Hcollision(i);
    			}
    			if (piec[i].v<=1) piec[i].v=2;		// min-max speeds
    			else if (piec[i].v>30) piec[i].v=50;
    			if (piec[i].h<=1) piec[i].h=2;
    			else if (piec[i].h>30) piec[i].h=50;
    		}
    		T++;
    	}
    	scene();
    	glutTimerFunc(1,move,T);
    }	
    
    	 
    int main(int argc, char *argv[]) {
    	glutInit(&argc,argv);
    	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
            glutInitWindowSize(900,900);
    	glutCreateWindow("openGL test");
    	glutDisplayFunc(scene);
    	glutTimerFunc(1500,move,1);
    	
    	piec=malloc(NP*sizeof(*piec));
    	piec[0].x=-100;piec[0].y=0; piec[0].w=30; piec[0].v=8;piec[0].vd=1; piec[0].h=4;piec[0].hd=1;
    	piec[1].x=10;piec[1].y=50; piec[1].w=18; piec[1].v=4;piec[1].vd=-1; piec[1].h=8;piec[1].hd=-1;
    	piec[2].x=50;piec[2].y=0; piec[2].w=14; piec[2].v=8;piec[2].vd=-1; piec[2].h=8;piec[2].hd=1;
    	piec[3].x=75;piec[3].y=90; piec[3].w=22; piec[3].v=6;piec[3].vd=1; piec[3].h=6;piec[3].hd=-1;
    	piec[4].x=-50;piec[4].y=0; piec[4].w=60; piec[4].v=5;piec[4].vd=-1; piec[4].h=5;piec[4].hd=1;
    	piec[5].x=100;piec[5].y=-60; piec[5].w=45; piec[5].v=3;piec[5].vd=-1; piec[5].h=6;piec[5].hd=1;
    	piec[6].x=-120;piec[6].y=-100; piec[6].w=27; piec[6].v=7;piec[6].vd=-1; piec[6].h=4;piec[6].hd=1;
    	setup(1.0,0.0,1.0,VOL);
    
    	glutMainLoop();
    	return 0;
    }
    This was my first openGL program (for real) and I think I am managing things slightly differently now in 3D, but it works and it's all about collision and acceleration/deceleration. Seven brightly colored but differently sized squares bounce around and off walls perpetually, causing each other to reverse direction and speed up or slow down as circumstances dictate. Rated: G Running Time: infinite
    Last edited by MK27; 03-02-2009 at 07:10 AM. Reason: /* add helpful comments */
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Waking old thread time?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by matsp View Post
    Waking old thread time?

    --
    Mats
    Oh I just noticed that! Silly bOMI and me -- at least I didn't write that for the thread. I must have watched bouncing blocks for like, hours.

    But there's no point in waiting for Krack to come back, I guess.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Collision Detection Problems
    By Dark_Phoenix in forum Game Programming
    Replies: 1
    Last Post: 12-17-2006, 02:25 PM
  2. Collision Detection
    By Grantyt3 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2005, 03:21 PM
  3. 2d Collision Detection in openGL
    By Shamino in forum Game Programming
    Replies: 4
    Last Post: 05-12-2005, 11:02 AM
  4. collision detection
    By DavidP in forum Game Programming
    Replies: 2
    Last Post: 05-11-2002, 01:31 PM
  5. Replies: 4
    Last Post: 05-03-2002, 09:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21