Collision Detection in OpenGL

Started by
3 comments, last by Krak 20 years, 8 months ago
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?
Advertisement
Open <i>Graphics Library</i>. Graphics. Not Game.

Your game should need no knowledge of its graphics rendering to perform collision detection. You perform collision detection checks directly on your game data.

If you can''t grap that concept, go think about it in more detail. Then start googling for various geometry intersection testing methods (circle/circle or sphere/sphere is about as easy as it gets).
quote:Original post by Anonymous Poster
Open Graphics Library. Graphics. Not Game.

Your game should need no knowledge of its graphics rendering to perform collision detection. You perform collision detection checks directly on your game data.

If you can''t grap that concept, go think about it in more detail. Then start googling for various geometry intersection testing methods (circle/circle or sphere/sphere is about as easy as it gets).


I''m only talking about detecting if two 2D rectangles collide. Any particular functions or methods you''d use?
there are no functions to do that in opengl
as he said, opengl > gl = graphic library
it simply put your graphics on screen

you have to perform your collision yourself(google!!!!)
but for two triangle, you should have their position and size store somewhere and test if they collide

here a simple example in c

typedef struct{
int pos[2];
int size[2];
}rect_t;

bool collide(rect_t r1,rect_t r2)
{
// here you test the position of the rectangles to check collision
}
For 2d rects, you can either have them axis-aligned, or orientated (rotated). Axis aligned is easy - you just need to see if the rectangles overlap on both the x and y axies.

First a class for the rects - held with position (x,y) and size (width,height). x+(width/2) is one edge, x-(width/2) is the other etc.

1. Find separation on x-axis. This is rect1.position - rect2.position. This may be negative, so make it posative if it is.

2. Find the total size of both controls in the x-axis. This is rect1.width/2 + rect2.width/2.

3. If separation < total size, then they overlap on the x-axis.

4. Repeat using the y coords and the height. If both test pass, the rectangles intersect

If you want rotated boxes, you''re on your own. Try googling for OBB (orientated bounding box).

This topic is closed to new replies.

Advertisement