public void growBy(CollisionVolume other) { final float maxX; final float minX; final float maxY; final float minY; if (mRadius > 0) { maxX = Math.max(getMaxX(), other.getMaxX()); minX = Math.min(getMinX(), other.getMinX()); maxY = Math.max(getMaxY(), other.getMaxY()); minY = Math.min(getMinY(), other.getMinY()); } else { maxX = other.getMaxX(); minX = other.getMinX(); maxY = other.getMaxY(); minY = other.getMinY(); } final float horizontalDelta = maxX - minX; final float verticalDelta = maxY - minY; final float diameter = Math.max(horizontalDelta, verticalDelta); final float newCenterX = minX + (horizontalDelta / 2.0f); final float newCenterY = minY + (verticalDelta / 2.0f); final float newRadius = diameter / 2.0f; mCenter.set(newCenterX, newCenterY); mRadius = newRadius; }
@Override public boolean intersects( Vector2 position, FlipInfo flip, CollisionVolume other, Vector2 otherPosition, FlipInfo otherFlip) { boolean result = false; if (other instanceof AABoxCollisionVolume) { // It's more accurate to do a sphere-as-box test than a box-as-sphere test. result = other.intersects(otherPosition, otherFlip, this, position, flip); } else { mWorkspaceVector.set(position); offsetByCenter(mWorkspaceVector, mCenter, flip); float otherRadius = 0; if (other instanceof SphereCollisionVolume) { SphereCollisionVolume sphereOther = (SphereCollisionVolume) other; mWorkspaceVector2.set(otherPosition); offsetByCenter(mWorkspaceVector2, sphereOther.getCenter(), otherFlip); mWorkspaceVector.subtract(mWorkspaceVector2); otherRadius = sphereOther.getRadius(); } else { // Whatever this volume is, pretend it's a sphere. final float deltaX = other.getMaxXPosition(otherFlip) - other.getMinXPosition(otherFlip); final float deltaY = other.getMaxYPosition(otherFlip) - other.getMinYPosition(otherFlip); final float centerX = deltaX / 2.0f; final float centerY = deltaY / 2.0f; mWorkspaceVector2.set(otherPosition); mWorkspaceVector2.x += centerX; mWorkspaceVector2.y += centerY; otherRadius = Math.max(deltaX, deltaY); } final float maxDistance = mRadius + otherRadius; final float distance2 = mWorkspaceVector.length2(); final float maxDistance2 = (maxDistance * maxDistance); if (distance2 < maxDistance2) { result = true; } } return result; }