public boolean intersects(Shape shape) { if (shape == null) { return false; } checkPoints(); boolean result = false; float points[] = getPoints(); float thatPoints[] = shape.getPoints(); int length = points.length; int thatLength = thatPoints.length; double unknownA; double unknownB; if (!closed()) { length -= 2; } if (!shape.closed()) { thatLength -= 2; } for (int i = 0; i < length; i += 2) { int iNext = i + 2; if (iNext >= points.length) { iNext = 0; } for (int j = 0; j < thatLength; j += 2) { int jNext = j + 2; if (jNext >= thatPoints.length) { jNext = 0; } unknownA = (((points[iNext] - points[i]) * (double) (thatPoints[j + 1] - points[i + 1])) - ((points[iNext + 1] - points[i + 1]) * (thatPoints[j] - points[i]))) / (((points[iNext + 1] - points[i + 1]) * (thatPoints[jNext] - thatPoints[j])) - ((points[iNext] - points[i]) * (thatPoints[jNext + 1] - thatPoints[j + 1]))); unknownB = (((thatPoints[jNext] - thatPoints[j]) * (double) (thatPoints[j + 1] - points[i + 1])) - ((thatPoints[jNext + 1] - thatPoints[j + 1]) * (thatPoints[j] - points[i]))) / (((points[iNext + 1] - points[i + 1]) * (thatPoints[jNext] - thatPoints[j])) - ((points[iNext] - points[i]) * (thatPoints[jNext + 1] - thatPoints[j + 1]))); if (unknownA >= 0 && unknownA <= 1 && unknownB >= 0 && unknownB <= 1) { result = true; break; } } if (result) { break; } } return result; }
public boolean contains(Shape other) { if (other.intersects(this)) { return false; } for (int i = 0; i < other.getPointCount(); i++) { float[] pt = other.getPoint(i); if (!contains(pt[0], pt[1])) { return false; } } return true; }
public RectBox getCollisionBox() { Shape shape = getShape(); return getRect(shape.getX(), shape.getY(), shape.getWidth(), shape.getHeight()); }