Jump to content

  • Log In with Google      Sign In   
  • Create Account

AABB sweep collision detection problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 sublixt   Members   -  Reputation: 103

Like
0Likes
Like

Posted 29 March 2012 - 12:45 PM

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[i] < 0)
	v0[i] = (float)((max(i, first) - min(i, other) / v[i]));
   else if(max(i, other) < min(i, first) && v[i] > 0)
	v0[i] = (float)((min(i, first) - max(i, other)) / v[i]);
  
   if(max(i, other) > min(i, first) && v[i] < 0)
	v1[i] = (float)((min(i, first) - max(i, other)) / v[i]);
   else if(max(i, first) < min(i, other) && v[i] > 0)
	v1[i] = (float)((max(i, first) - min(i, other)) / v[i]);
  }

  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[i] + bla.bounds[i];
}

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

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);
}
}


Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS