View Full Version : Help with collision detection using axis aligned rectangular prisms

10-27-2012, 02:34 PM

Hello everybody, im currently trying to learn 3d game development (ive developed 2d games in the past, nothing this complex, i am a third year programming student). Ive been working on this project in my spare time for the past few weeks, and im really stumped. Im using axis aligned prisms for my collision detection, and I cant seem to get it to work correctly. Im using java lwjgl to program openGL. I would really appreciate any assistance you guys can offer (even if its just pointing me in the best direction to get assistance).

Basically, I havent been able to detect any pattern as to what is going on, but all i can say is the the code below flatly does not work, but rather random spots get tagged as collisions. What i do is say if there is any overlap in the x axis of the 2 rectangular prisms, then check the y axis, if there is any overlap there, then check the z axis. If all 3 overlap, then there must be a collision.

Directly below is that critical section that calculates the hit detection:

public boolean collides(Rectangle rect2)
float x2 = rect2.getX();
float y2 = rect2.getY();
float z2 = rect2.getZ();
float width2Over2 = rect2.getWidth2();
float height2Over2 = rect2.getHeight2();
float depth2Over2 = rect2.getDepth2();

float x1high = x + widthOver2;
float x1low = x - widthOver2;
float y1high = y + heightOver2;
float y1low = y - heightOver2;
float z1high = z + depthOver2;
float z1low = z - depthOver2;

float x2high = x2 + width2Over2;
float x2low = x2 - width2Over2;
float y2high = y2 + height2Over2;
float y2low = y2 - height2Over2;
float z2high = z2 + depth2Over2;
float z2low = z2 - depth2Over2;

if (((x1high > x2low) && (x1high < x2high)) || ((x1low > x2low) && (x1low < x2high)))
if (((y1high > y2low) && (y1high < y2high)) || ((y1low > y2low) && (y1low < y2high)))
if (((z1high > z2low) && (z1high < z2high)) || ((z1low > z2low) && (z1low < z2high)))
return true;
return false;

I am not sure what I am doing wrong, unless I've translated the coordinates wrong somewhere. I can post full source code for the application if it will help, just let me know.

Thank-you for any assistance at all.