I am trying to make a collision detection system and the aabb-sphere collision isn't working. Here is my code.
This method checks to see which whether I should collide with AABB-AABB, Sphere-Sphere, or AABB-Sphere.
public static boolean collides(BoundingBox box,BoundingBox other) {
if(box instanceof AABB && other instanceof AABB) {
return collides((AABB) box,(AABB) other);
}
if(box instanceof Sphere && other instanceof Sphere) {
return collides((Sphere) box,(Sphere) other);
}
if(box instanceof AABB && other instanceof Sphere) {
return collides((AABB) box,(Sphere) other);
}
if(box instanceof Sphere && other instanceof AABB) {
return collides((Sphere) box,(AABB) other);
}
return false;
This method does the actual colliding.
public static boolean collides(AABB other,Sphere sphere) {
Vector3f p = closestPointToAabb(other, sphere.getPos());
Vector3f v = Vector3f.sub(p, sphere.getPos(), null);
float dist = Vector3f.dot(v, v);
boolean collision = dist <= sphere.getRadius() * sphere.getRadius();
return collision;
};
This method calculates the point closest to aabb.
public static Vector3f closestPointToAabb(AABB aabb,final Vector3f point) {
if(point.x > aabb.getMax().x || point.y > aabb.getMax().y ||point.z > aabb.getMax().z ) {
return aabb.getMax();
}else if(point.x < aabb.getMin().x || point.y < aabb.getMin().y ||point.z < aabb.getMin().z ) {
return aabb.getMin();
}
return point;
}
This method does all the logic.
public static void updateObjects() {
for(int collider = 1; collider < axisList.size(); collider++) {
BoundingBox colliderBox = axisList.get(collider);
for(int collidee = 0; collidee < collider; collidee++) {
BoundingBox collideeBox = axisList.get(collidee);
if((colliderBox.getStatic() && collideeBox.getStatic()) && !(colliderBox instanceof Player || collideeBox instanceof Player)) {
continue;
}
if(BoundingBox.collides(colliderBox, collideeBox)) {
System.out.println("Collision with " + colliderBox.getName() + " and " + collideeBox.getName());
collisionList.add(new Collision(colliderBox,collideeBox));
}
}
}
}
}
Could anyone please help me. Also How should I specify my min and max and the sphere's centre and radius.
Edit:
It seems that the collision is working but the Bounding Boxes collide in random places. I think it is a problem in how I am specifying my min and max and the sphere's centre and radius.
Here is how I specify it.
public static void addObject(Entity entity) {
if(entity instanceof Player) {
Sphere sphere = new Sphere(entity.getModel().getMin(),entity.getModel().getMax(),entity.getPosition(),false,entity.getName());
axisList.add(sphere);
entity.setBox(sphere);
return;
}
axisList.add(entity.getBox());
}
And here is three methods(The code is pretty bad but I will fix it).
removeUnusedVertices(vertices);
float[] verticesArray = new float[vertices.size() * 3];
float[] texturesArray = new float[vertices.size() * 2];
float[] normalsArray = new float[vertices.size() * 3];
float furthest = convertDataToArrays(vertices, textures, normals, verticesArray,
texturesArray, normalsArray);
int[] indicesArray = convertIndicesListToArray(indices);
Vector3f min = min(vertices);
Vector3f max = max(vertices);
TexturedModel model = new TexturedModel(Loader.loadToVao(verticesArray, texturesArray, normalsArray, indicesArray),min,max,vertices,furthest, textureLoc);
return model;
private static Vector3f min(List<Vertex> vertices) {
List<Float> xArray= new ArrayList<>();
List<Float> yArray = new ArrayList<>();
List<Float> zArray = new ArrayList<>();
for(Vertex vertex : vertices) {
xArray.add(vertex.getPosition().x);
yArray.add(vertex.getPosition().y);
zArray.add(vertex.getPosition().z);
}
return new Vector3f(Collections.min(xArray),Collections.min(yArray),Collections.min(zArray));
}
private static Vector3f max(List<Vertex> vertices) {
List<Float> xArray= new ArrayList<>();
List<Float> yArray = new ArrayList<>();
List<Float> zArray = new ArrayList<>();
for(Vertex vertex : vertices) {
xArray.add(vertex.getPosition().x);
yArray.add(vertex.getPosition().y);
zArray.add(vertex.getPosition().z);
}
return new Vector3f(Collections.max(xArray),Collections.max(yArray),Collections.max(zArray));
}
My entity intiialization
box = new AABB(getModel().getMin(),getModel().getMax(),position,name,staticObject);
AABB and sphere:
public AABB(Vector3f min,Vector3f max,Vector3f pos,String name,boolean staticObject) {
this.min = min;
this.max = max;
this.name = name;
this.staticObject = staticObject;
this.pos = pos;
}
public Sphere(Vector3f min,Vector3f max,Vector3f pos,boolean staticObject,String name) {
radius = Vector3f.dot(max, min);
center = Vector3f.sub(max, min,null);
this.pos = pos;
this.staticObject = staticObject;
this.name = name;
}
Edit Again:
I debugged my code again and found that when I make contact with an aabb it reports that I'm colliding with all the aabb's in the scene.
I have no idea what could be causing this.