I have tried to follow a few guides explaining the theory behind Sat but this implementation is not working for me, Could I get some help?
here is my class with my SAT code, its part of the DevEntity class, every object that I want to implement SAT extends this class, and the vertex points are set in those classes.
protected Vector2[] axis;
public void obtainEdges(){
axis = new Vector2[vertices.length];
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(new Vector2(x + vertices[i].x, y + vertices[i].y));
if(proj < min){
min = proj;
}
if(proj > max){
max = proj;
}
}
return new Vector2(min, max);
}
public boolean separatingAxisTheorem(){
obtainEdges();
for(DevEntity e : handler.getDevWorld().getDevM().getDevEntities()){
Vector2[] axes1 = axis;
Vector2[] axes2 = e.axis;
if(e.equals(this))
return false;
for(int i = 0; i < axes1.length; i++){
Vector2 axis = axes1[i];
Vector2 p1 = projectAxis(axis);
Vector2 p2 = e.projectAxis(axis);
if(!p1.overlap(p2)){
return false;
}
}
for(int i = 0; i < axes2.length; i++){
Vector2 axis = axes2[i];
Vector2 p1 = projectAxis(axis);
Vector2 p2 = e.projectAxis(axis);
if(!p1.overlap(p2)){
return false;
}
}
}
return true;
}
here’s my Vector Class
package shapegame.engine;
import java.util.Vector;
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){
return x * b.x + y * b.y;
}
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){
return new Vector2(x - vec.x, y - vec.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 false;
}
return true;
}
}