Jump to content
  • Advertisement
Sign in to follow this  
sublixt

AABB sweep collision detection problem

This topic is 2302 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am having trouble trying to figure out aabb collision detection. I am trying converting the code from this tutorial (http://www.gamasutra...ames.php?page=3) to java/groovy but it doesnt work. I have checked through the code several times and I could not see any logic errors caused by translation. I was wondering if anyone could point out or suggest what might be wrong with it.

the main collision class with the problem that I am having.
//this class is made in groovy to take advantage of overloading constructors
public class Collision {
public static boolean collide(AABB first, AABB other, Float u0, Float u1){
//AABB class contains 3 vectors
//location - the x, y, z location
//extents - the extents of the bounding box
//sweep - the distance traveled that frame
Vector va = new Vector(first.sweep);
Vector vb = new Vector(other.sweep);

Vector v = vb - va;
Vector v0 = new Vector(0.0f, 0.0f, 0.0f);
Vector v1 = new Vector(1.0f, 1.0f, 1.0f);

if(overlap(first, other)){
u0 = 0.0f;
u1 = 0.0f;
return true;
}


for(int i = 0; i < 3; i++){
if(max(i, first) < min(i, other) && v < 0)
v0 = (float)((max(i, first) - min(i, other) / v));
else if(max(i, other) < min(i, first) && v > 0)
v0 = (float)((min(i, first) - max(i, other)) / v);

if(max(i, other) > min(i, first) && v < 0)
v1 = (float)((min(i, first) - max(i, other)) / v);
else if(max(i, first) < min(i, other) && v > 0)
v1 = (float)((max(i, first) - min(i, other)) / v);
}

u0 = Math.max(v0.x, Math.max(v0.y, v0.z));
u1 = Math.min(v1.x, Math.min(v1.y, v1.z));
return u0 <= u1;
}

private static boolean overlap(AABB first, AABB other) {
Vector T = other.location - first.location;

return Math.abs(T.x) <= (first.location.x + other.location.x) &&
Math.abs(T.y) <= (first.location.y + other.location.y) &&
Math.abs(T.z) <= (first.location.z + other.location.z);
}

private static float max(int i, AABB bla){
bla.location + bla.bounds;
}

private static float min(int i, AABB bla){
bla.location;
}
}


the class that handels the vectors
it is a wraper class for vector3f letting me overload operators
class Vector extends Vector3f {
public Vector(float f, float g, float h) {
super(f, g, h);
}

public Vector(Vector3f a){
super(a);
}

public Vector plus(Vector other){
return new Vector((float) (x + other.x), (float) (y + other.y), (float) (z + other.z));
}

public Vector minus(Vector other){
return new Vector((float) (x - other.x), (float) (y - other.y), (float) (z - other.z));
}

public Float getAt(Integer i){
if(i > 2 || i < 0)
throw new IndexOutOfBoundsException("Vector index out of bounds");

switch(i){
case 0: return x;
case 1: return y;
case 2: return z;
}
}

public void putAt(Integer i, Float a){
if(i > 2 || i < 0)
throw new IndexOutOfBoundsException("Vector index out of bounds");

switch(i){
case 0: x = a; return;
case 1: y = a; return;
case 2: z = a; return;
}
}

public static Vector downcast(Vector3f down){
return new Vector(down.x, down.y, down.z);
}
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!