Home » Java » Java SAT collision response

Java SAT collision response

Posted by: admin June 14, 2018 Leave a comment

Questions:

Here is my current response, only for the bottom

So I created an SAT implementation in Java for collision detection, but I don’t know how to respond to it; aka the logic when two objects are colliding.

Here is my current code:

private void collision() {
    color = 0xff0000;
    for (int x = 0; x < level.tiles.length; x += Level.TILE_SIZE) {
        for (int y = 0; y < level.tiles[x].length; y += Level.TILE_SIZE) {
            TileEntity tile = level.tiles[x][y];
            if (tile != null && SAT.collision(hull, tile.hull)) {
                if (position.y < tile.position.y) {
                    position.y = tile.position.y - height;
                    falling = false;
                    jumping = false;
                    velY = 0;
                }
                color = 0x0000ff;
            } else falling = true;
        }
    }
}

Also, the hull class is the polygon or AABB around the player/tiles.

Here is the code for that:

public class Hull {

private Vector2[] points;
private Vector2 position;

public Hull() {
    this.points = new Vector2[0];
}

public Hull(int n) {
    this.points = new Vector2[n];
}

public Hull(Vector2... v) {
    this.points = v;
}

public Vector2 getPoint(int index) {
    return points[index];
}

public void setPoint(int i, Vector2 v) {
    points[i] = v;
}

public Vector2 getPosition() {
    return position;
}

public void setPosition(float x, float y) {
    position = new Vector2(x, y);
}

public Hull setPosition(Vector2 v) {
    position = v;
    return this;
}

public int getPointCount() {
    return points.length;
}

}

Answers: