Search code examples
openglcollision-detection

Computing AABB collision


I'm making a game in OpenGL. The game consists on a plane flying free on a 3D world, wich has to go through some especific areas, like if it was going through windows. Right now, I'm trying to compute the collision of the plane with that mentioned "windows". I thought as a first approach, to have an swept AABB for the trajectory of the plane, and some AABBs for each window, such as if these AABB overlaps, there are potencial cases of the plane going through it. But the problem starts when I try to calculate the AABBs, since every algorithm I know needs to know the coordinates of both objects (plane+window) in the same reference system. My program draws the world as if it is what's moving, instead of the plane, which is centered always at the origin (actually, at z=-10, so it's rendered). The real problem is how to compute the coordinates of each "window" as seen from the plane. This is the code I use to draw the scene:

glLoadIdentity();

//these are the rotations of the plane of the ground
glRotatef(RotHorizont, 0.0f, 0.0f, 1.0f);
glRotatef(-directionX, 1.0f, 0.0f, 0.0f);
glRotatef(-directionY, 0, 1.0f, 0);

//and these are the movement of the plane
//(the multiplication by 0.0174532925f is to convert the angles to radians
Tx -= (float)Math.sin(directionY * 0.0174532925f) * velocity;
Tz -= (float)Math.cos(directionY * 0.0174532925f) * velocity;
Ty -= (float)Math.sin(directionX * 0.0174532925f) * velocity;

glTranslatef(-Tx, Ty, -Tz);

/*
here I draw the ground and the windows
*/
....

//and now I draw the plane
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -10.0f);
/* draw the plane */

So, how the heck do I get the coordinates of the "windows"? Or is there some other way to compute the collision? I really need your help, I will appreciate it so much. Thank you all!


Solution

  • You can define an AABB by a position (3-coordinate vector) and its size (3-coordinate vector), and apply any AABB collision detection algorithm to resolve your problem.

    As you pointed out, the computation has to be done in the same coordinate system. However, since you are dealing with AABBs (and not OBBs), you do not need to deal with orientation (either the AABB is computed so that the object will remain in it after rotating, or you recompute it after every object rotation). Since "AA" means "Axis-Aligned", you are normally already dealing with a common frame of reference (else it would be OBBs). Thus, the only "conversion" that could be needed is a translation.

    If the center of your plane is the center of your coordinate system, your plane's AABB's position will be (0,0,0) (or (0,0,-10)). For your windows, well it depends on how you define those, but it should be fairly easy to find their centers (with OpenGL you are dealing with products of transformation matrices, find the ones taking you from the original coordinate system to the windows one). If you provide more information, we could give you some more details on how to achieve that.

    Links: