Skip to content

Instantly share code, notes, and snippets.

@Lomeli12
Last active February 4, 2017 00:09
Show Gist options
  • Save Lomeli12/6bd9cced761f4e9f4f78a7c1668ac5de to your computer and use it in GitHub Desktop.
Save Lomeli12/6bd9cced761f4e9f4f78a7c1668ac5de to your computer and use it in GitHub Desktop.
Axis Aligned Bounding Box - Anything missing or wrong?
package net.lomeli.savetown.util.math;
public class AABB {
public static final AABB ZERO_BOX = new AABB(0, 0, 0, 0);
private float x1, x2, y1, y2;
public AABB(float x1, float x2, float y1, float y2) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
public AABB expand(float xChange, float yChange) {
float newX1 = this.x1 - xChange;
float newX2 = this.x2 + xChange;
float newY1 = this.y1 - yChange;
float newY2 = this.y2 + yChange;
return new AABB(newX1, newX2, newY1, newY2);
}
public AABB expand(float change) {
return expand(change, change);
}
public boolean doBoxesCollide(float x1, float x2, float y1, float y2) {
return !(this.x2 < x1 || x2 < this.x1 || this.y2 < y1 || y2 < this.y1);
}
public boolean doBoxesCollide(AABB box) {
return doBoxesCollide(box.getX1(), box.getX2(), box.getY1(), box.getY2());
}
public void translateBox(float xChange, float yChange) {
this.x1 += xChange;
this.x2 += xChange;
this.y1 += yChange;
this.y2 += yChange;
}
public void setPosition(float x, float y) {
float xDif = this.x2 - this.x1;
float yDif = this.y2 - this.y2;
this.x1 = x;
this.y1 = y;
this.x2 = x + xDif;
this.y2 = y + yDif;
}
public float getX1() {
return x1;
}
public float getX2() {
return x2;
}
public float getY1() {
return y1;
}
public float getY2() {
return y2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment