-1
\$\begingroup\$

I am going to be making a 3D game and I need to be able to test for accurate collision. I was able to get bounding sphere collision working (too inaccurate to be used for the whole game), but AABB collision is much harder (and more accurate!). I used the code from this website and I can't seem to get it to work. I have my code below for the AABB collision method:

public boolean isColliding (AABB other) {
Vector3f otherMax = new Vector3f(other.pos.x + other.w, other.pos.y + other.h, other.pos.z + other.d);
Vector3f otherMin = new Vector3f(other.pos.x, other.pos.y, other.pos.z);

Vector3f thisMin = new Vector3f(this.pos.x + this.w, this.pos.y + this.h, this.pos.z + this.d);
Vector3f thisMax = new Vector3f(this.pos.x, this.pos.y, this.pos.z);

return (otherMax.x > thisMin.x && otherMin.x < thisMax.x
        && otherMax.y > thisMin.y && otherMin.y < thisMax.y
        && otherMax.z > thisMin.z && otherMin.z < thisMax.z);
}

I'm not quite sure where this is going wrong, any ideas?

\$\endgroup\$
1
\$\begingroup\$

You have your thisMin and thisMax declarations reversed.

Vector3f thisMin = new Vector3f(this.pos.x + this.w, this.pos.y + this.h, this.pos.z + this.d);
Vector3f thisMax = new Vector3f(this.pos.x, this.pos.y, this.pos.z);

should be:

Vector3f thisMin = new Vector3f(this.pos.x, this.pos.y, this.pos.z);
Vector3f thisMax = new Vector3f(this.pos.x + this.w, this.pos.y + this.h, this.pos.z + this.d);
\$\endgroup\$
  • 1
    \$\begingroup\$ Another key takeaway is that you'll go much further (and faster) when you can suspect and debug basic things like this... \$\endgroup\$ – david van brink Oct 26 '14 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.