# simple collision detection for nearest rectangle

This topic is 2081 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I try to find the nearest rectangle top or bottom and detect collision
i have rectangle that i can get its
x,y, and width and height and position .
(im using cocos2d-x c++ but its doesn't matter in which language )
any way in the game update loop i run throw all the rectangles in the view except the selected on and each rectangle i send to the selected on function that check intersection like this : and this is only to detect the bottom of the rectangle :
this tedious task of checking the x,y,width and height is to much and i not include the
need to check what is the nearest rectangle that should intersect
my question is is there better algorithm or way to do this ?

    bool Block::isIntersectsRect(Block* otherBlock)    {

//CCLOG("The other Block is:%d",otherBlock->getTag());
auto otherBlockRect = Rect(
otherBlock->getPosition().x - (otherBlock->getContentSize().width/2),
otherBlock->getPosition().y - (otherBlock->getContentSize().height/2),
otherBlock->getContentSize().width,
otherBlock->getContentSize().height);

auto thisRect = Rect(
this->getPosition().x - (this->getContentSize().width/2),
this->getPosition().y - (this->getContentSize().height/2),
this->getContentSize().width,
this->getContentSize().height);

/*
bool thisMaxX_less_rectMaxX = BlockIsLeftIntersectsRect(thisRect,otherBlockRect);
bool rectMaxX_less_thisMaxX = BlockIsRightIntersectsRect(thisRect,otherBlockRect);
bool thisMaxY_less_rectMaxY = BlockIsButtonIntersectsRect(thisRect,otherBlockRect);
bool rectMaxY_less_thisMaxY = BlockIsTopIntersectsRect(thisRect,otherBlockRect);

if(!(thisMaxX_less_rectMaxX ||
rectMaxX_less_thisMaxX ||
thisMaxY_less_rectMaxY ||
rectMaxY_less_thisMaxY))
{
return true;
}
*/

CCLOG(" thisRect.getMinX():%f<(otherBlockRect.getMaxX()/2):%f && thisRect.getMaxY():%f>=otherBlockRect.getMaxX():%f  && thisRect.getMaxY():%f < otherBlockRect.getMaxY():%f| the other:%d",(thisRect.getMinX()),
(otherBlockRect.getMaxX()/2),
thisRect.getMaxY(),
otherBlockRect.getMaxX(),
thisRect.getMaxY() ,
otherBlockRect.getMaxY(),
otherBlock->getTag());

if((thisRect.getMinX()) < (otherBlockRect.getMaxX()/2)  && thisRect.getMaxY() > otherBlockRect.getMinY() && thisRect.getMaxY() < otherBlockRect.getMaxY())
{

CCLOG("This is over maxY");

return true;
}
else
{

}
}

i also attaching photo to illustrate what i mean i hope i draw it right .

##### Share on other sites

Your checks look good. An (I think) more usual way of doing this is by checking if the box is outside the other box, instead of checking if it's inside the box. Obviously, if it's not outside the box it's inside... This is / could also be faster because you only need to check all statements if the box was inside the other box. I think you can figure out yourself how to do that.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 20
• 20
• 11
• 9