I was playing arround with hitboxes and ended up with following data:
final double targetSmallestX = targetCenter.getX() - targetHalfWidth;
final double targetSmallestY = targetCenter.getY() - targetHalfHeight;
final double targetHighestX = targetCenter.getX() + targetHalfWidth;
final double targetHighestY = targetCenter.getY() + targetHalfHeight;
final double sourceSmallestX = sourceCenter.getX() - sourceHalfWidth;
final double sourceSmallestY = sourceCenter.getY() - sourceHalfHeight;
final double sourceHighestX = sourceCenter.getX() + sourceHalfWidth;
final double sourceHighestY = sourceCenter.getY() + sourceHalfHeight;
What i want to do is to check, if the two given rectangles target and source are intersecting with each other. Could have done it like this
Rectangle target = new Rectangle(targetSmallestX, target.width, targetSmallestY, target.height);
Rectangle source = new Rectangle(sourceSmallestX, source.width, sourceSmallestY, source.height);
target.intersect(source);
But this would require me to work with integers.
All the algorithms I came up with seemed too long and complicated for such a seemingly simple task. Does anyone have an idea for a smart approach for this?
EDIT:
Current approach
(targetSmallestX < sourceSmallestX + this.width)
&& (sourceSmallestX < (targetSmallestX + target.width))
&& (targetSmallestY < sourceSmallestY + this.height)
&& (sourceSmallestY < targetSmallestY + target.height);
Does this check leave any possible constellations in which it wouldn't work correctly?
Intersecthere: referencesource.microsoft.com/#System.Drawing/commonui/System/…range1Start,range1Width,range2Start, andrange2Widthand returns whether those ranges intersect. Then, call that twice for x-values and y-values.