public static Vector[] getBounds(int ax, int ay, int aw, int ah, double angle) { int originX = aw / 2; int originY = ah / 2; return getBounds(ax, ay, aw, ah, originX, originY, angle); }
/** * Code found at: http://stackoverflow.com/a/21647567 */ public static boolean collidePolygon(int ax, int ay, int aw, int ah, double angle, int bx, int by, int bw, int bh, double bAngle) { Vector[] pointsA = getBounds(ax, ay, aw, ah, angle); Vector[] pointsB = getBounds(bx, by, bw, bh, bAngle); return collidePoints(pointsA, pointsB); }
public boolean colidePoint(int px, int py) { return CollisionDetector.collideRectPoint(x, y, width, height, px, py); }
private static boolean isSeparate(Vector[] coordsA, Vector[] coordsB) { edges: for (int i = 0; i < coordsA.length; i++) { double[] u = e2p(coordsA[i].x, coordsA[i].y); int ni = i + 1 < coordsA.length ? i + 1 : 0; double[] v = e2p(coordsA[ni].x, coordsA[ni].y); double[] pedge = getCrossProduct(u, v); for (Vector p : coordsB) { double d = getDotProduct(pedge, e2p(p.x, p.y)); if (d > -0.001) { continue edges; } } return true; } return false; }
/** * Rectangle To Circle. */ public static boolean collideRectCircle(double rectWidth, double rectHeight, double rectRotation, double rectCenterX, double rectCenterY, double circleCenterX, double circleCenterY, double circleRadius) { double tx, ty, cx, cy; if (rectRotation == 0) { //High speed for rectangles without rotation tx = circleCenterX; ty = circleCenterY; cx = rectCenterX; cy = rectCenterY; } else { tx = Math.cos(rectRotation) * circleCenterX - Math.sin(rectRotation) * circleCenterY; ty = Math.cos(rectRotation) * circleCenterY + Math.sin(rectRotation) * circleCenterX; cx = Math.cos(rectRotation) * rectCenterX - Math.sin(rectRotation) * rectCenterY; cy = Math.cos(rectRotation) * rectCenterY + Math.sin(rectRotation) * rectCenterX; } return collideRectPoint(rectWidth, rectHeight, rectRotation, rectCenterX, rectCenterY, circleCenterX, circleCenterY) || collideCircleLine(tx, ty, circleRadius, cx - rectWidth / 2, cy + rectHeight / 2, cx + rectWidth / 2, cy + rectHeight / 2) || collideCircleLine(tx, ty, circleRadius, cx + rectWidth / 2, cy + rectHeight / 2, cx + rectWidth / 2, cy - rectHeight / 2) || collideCircleLine(tx, ty, circleRadius, cx + rectWidth / 2, cy - rectHeight / 2, cx - rectWidth / 2, cy - rectHeight / 2) || collideCircleLine(tx, ty, circleRadius, cx - rectWidth / 2, cy - rectHeight / 2, cx - rectWidth / 2, cy + rectHeight / 2); }
public boolean collide(Layer b) { if (getAngle() == 0 && b.getAngle() == 0) { return CollisionDetector.collideRectRect(getX(), getY(), getW(), getH(), b.getX(), b.getY(), b.getW(), b.getH()); } else { return CollisionDetector.collidePolygon(getX(), getY(), getW(), getH(), getAngle(), b.getX(), b.getY(), b.getW(), b.getH(), b.getAngle()); } } }
public boolean colide(Feature feature) { return CollisionDetector.collideRectRect(x, y, width, height, feature.x, feature.y, feature.width, feature.height); }
public boolean collidePoint(double px, double py) { return CollisionDetector.colideEllipsePoint(center.x, center.y, angle, w, h, px, py); }
public static Vector[] getBounds(int ax, int ay, int aw, int ah, int originX, int originY, double angle) { Vector[] points = new Vector[4]; points[0] = new Vector(ax, ay); points[1] = new Vector(ax + aw, ay); points[2] = new Vector(ax + aw, ay + ah); points[3] = new Vector(ax, ay + ah); if (angle != 0) { for (Vector point : points) { point.rotate(ax + originX, ay + originY, angle); } } return points; }
public static boolean collideCircleCircle(int x, int y, int w, int h, int bx, int by, int bw, int bh) { float radius = w / 2; float cx = x + radius; float cy = y + radius; float bRadius = bw / 2; float bCx = bx + radius; float bCy = by + radius; return collideCircleCircle(cx, cy, radius, bCx, bCy, bRadius); }
public static boolean collidePoints(Vector[] pointsA, Vector[] pointsB) { return !(isSeparate(pointsA, pointsB) || isSeparate(pointsB, pointsA)); }
/** * Rectangle To Point. */ public static boolean collideRectPoint(double rectX, double rectY, double rectWidth, double rectHeight, double rectAngle, double pointX, double pointY) { double rectCenterX = rectX + rectWidth / 2; double rectCenterY = rectY + rectHeight / 2; if (rectAngle == 0) //High speed for rectangles without rotation return collideRectPointByCenter(rectWidth, rectHeight, rectCenterX, rectCenterY, pointX, pointY); final double cos = Math.cos(rectAngle); final double sin = Math.cos(rectAngle); double tx = cos * pointX - sin * pointY; double ty = cos * pointY + sin * pointX; double cx = cos * rectCenterX - sin * rectCenterY; double cy = cos * rectCenterY + sin * rectCenterX; return collideRectPointByCenter(rectWidth, rectHeight, cx, cy, tx, ty); }
/** * @param px * @param py * @return */ public boolean collideCirclePoint(int px, int py) { int radius = getW() / 2; int cx = getX() + radius; int cy = getY() + radius; return CollisionDetector.collideCirclePoint(cx, cy, radius, px, py); }
public boolean collideRectPoint(int bx, int by) { return CollisionDetector.collideRectPoint(getX(), getY(), getW(), getH(), bx, by); }
/** * @param bx * @param by * @param bw * @param bh * @return */ public boolean collideRectRect(int bx, int by, int bw, int bh) { return CollisionDetector.collideRectRect(getX(), getY(), getW(), getH(), bx, by, bw, bh); }
public boolean colideEllipsePoint(double px, double py) { return CollisionDetector.colideEllipsePoint(center.getX(), center.getY(), angle, w, h, px, py); }
/** * @param bx * @param by * @param bw * @param bh * @return */ public boolean collideCircleCircle(int bx, int by, int bw, int bh) { return CollisionDetector.collideCircleCircle(getX(), getY(), getW(), getH(), bx, by, bw, bh); }
@Override public boolean collideRectPoint(int px, int py) { int rectWidth = (int) (getW() * getScaleX()); int rectHeight = (int) (getH() * getScaleY()); return CollisionDetector.collideRectPoint(getX(), getY(), rectWidth, rectHeight, angle, px, py); }
/** * @param mx * @param my * @return */ public boolean onMouse(int mx, int my) { if (angle == 0) { if (scaleX == 1 && scaleY == 1) { return CollisionDetector.collideRectPoint(getX(), getY(), getW(), getH(), mx, my); } else { int rw = (int) (getW() * getScaleX()); int rh = (int) (getH() * getScaleY()); return CollisionDetector.collideRectPoint(getX() - rw / 4, getY() - rh / 4, rw, rh, mx, my); } } else { if (scaleX == 1 && scaleY == 1) { return CollisionDetector.collideRectPoint(getX(), getY(), getW() / 2, getH() / 2, getAngle(), mx, my); } else { int rw = (int) (getW() * getScaleX()); int rh = (int) (getH() * getScaleY()); return CollisionDetector.collideRectPoint(getX() - rw / 4, getY() - rh / 4, rw / 2, rh / 2, getAngle(), mx, my); } } }