I've been struggling to implement SAT for a long time now and this is my last resort, after doing visual checks on my code it seems that I am correctly getting the face normals of the shapes that I am checking (axis), and also correctly projecting each vertices onto each axis, however SAT is returning false because it is not detecting an overlap, if anyone could point me in the right direction i would be grateful.
SAT Class: (DevEntity)
public void obtainEdges(){
for(int i = 0; i < vertices.length; i++){
Vector2 e1 = vertices[i];
Vector2 e2 = vertices[i + 1 == vertices.length ? 0 : i + 1];
Vector2 edge = e1.subtract(e2);
Vector2 perp = edge.perp(edge);
axis[i] = perp;
}
}
public Vector2 projectAxis(Vector2 axis){
float min = axis.dot(vertices[0]);
float max = min;
for(int i = 1; i < vertices.length; i++){
float proj = axis.dot(vertices[i]);
if(proj < min){
min = proj;
}
if(proj > max){
max = proj;
}
}
proj = new Vector2(min,max);
return proj;
}
public boolean separatingAxisTheorem(){
obtainEdges();
for(DevEntity e : handler.getDevWorld().getDevM().getDevEntities()){
if(e.equals(this)){
return false;
}
Vector2[] axes1 = axis;
Vector2[] axes2 = e.axis;
for(int i = 0; i < axes1.length; i++){
Vector2 p1 = projectAxis(axes1[i]);
Vector2 p2 = e.projectAxis(axes1[i]);
if(!p1.overlap(p2)){
return false;
}
}
for(int i = 0; i < axes2.length; i++){
Vector2 p1 = projectAxis(axes2[i]);
Vector2 p2 = e.projectAxis(axes2[i]);
if(!p1.overlap(p2)){
return false;
}
}
}
return true;
}
Vector Class:
public class Vector2 {
public float x, y;
public Vector2(float x, float y){
this.x = x;
this.y = y;
}
public Vector2(Vector2 vec){
this.x = vec.x;
this.y = vec.y;
}
public float dot(Vector2 b){
float d = b.x * x + b.y * y;
return d;
}
public Vector2 normalize(Vector2 vec){
float mag = (float) Math.sqrt(vec.x * vec.x + vec.y * vec.y);
Vector2 b = new Vector2(vec.x/mag, vec.y/mag);
return b;
}
public Vector2 subtract(Vector2 vec){
Vector2 s = new Vector2(x - vec.x, y - vec.y);
return s;
}
public Vector2 subtract(float x, float y){
return new Vector2(this.x - x, this.y - y);
}
public Vector2 perp(Vector2 vec){
Vector2 p = new Vector2(-vec.y, vec.x);
return p;
}
public boolean overlap(Vector2 vec){
if(y > vec.x && vec.y > x){
return true;
}
return false;
}