RectangleToRectangle intersect always returns true

I’m in the midst of creating a simple game and trying to use Phaser.Geom.Intersects.RectangleToRectangle to determine if any parts of my boxes overlap; however it always returns true, no matter how I use it.

Initially I thought it was something to do with the fact that I was creating a sprite within a prefab container, within another container, but getBounds always returns the correct global coordinates.

I started to put together a simple MCVE assuming I’d have to replicate my game’s container setup; but it looks like RectangleToRectangle always returns true with simple sprites directly:

https://jsfiddle.net/indextwo/opvd08ku/1/

As you can see in the example I’m trying RectangleToRectangle both against the sprite objects directly and on the GetBounds of the sprites, but in either case it’s always true even when they’re nowhere near each other.

Can anyone point out where I’m going wrong with this?

var RectangleToRectangle = function (rectA, rectB)
{
    if (rectA.width <= 0 || rectA.height <= 0 || rectB.width <= 0 || rectB.height <= 0)
    {
        return false;
    }

    return !(rectA.right < rectB.x || rectA.bottom < rectB.y || rectA.x > rectB.right || rectA.y > rectB.bottom);
};

RectangleToRectangle needs right and bottom.
Phaser.Display.Bounds.GetBounds() does not supply them.

This should work:
Phaser.Geom.Intersects.RectangleToRectangle(this.atari130.getBounds(), this.atari1200.getBounds()));

1 Like

Ah! Perfect, thank you! Slightly confusing that .getBounds() returns the expected parameters but Display.Bounds.GetBounds doesn’t :thinking: But it now totally works.

Because my boxes can butt up right against one another (and that’s technically an intersection) I’ve created a simple method that modifies the coordinate response:

myBounds: function() {
	var tempBounds = this.getBounds();

	tempBounds.top += 2;
	tempBounds.left += 2;
	tempBounds.right -= 2;
	tempBounds.bottom -= 2;

	return tempBounds;
}

console.log(
    Phaser.Geom.Intersects.RectangleToRectangle(rect1.myBounds(), rect2.myBounds());
);

You can also use Phaser.Display.Bounds.GetBounds(this.atari130, new Phaser.Geom.Rectangle()) etc.