Jump to content
  • Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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),
        auto thisRect = Rect(
                 this->getPosition().x - (this->getContentSize().width/2),
                 this->getPosition().y - (this->getContentSize().height/2),
         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 ||
             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()),
                                               thisRect.getMaxY() ,
        if((thisRect.getMinX()) < (otherBlockRect.getMaxX()/2)  && thisRect.getMaxY() > otherBlockRect.getMinY() && thisRect.getMaxY() < otherBlockRect.getMaxY())
            CCLOG("This is over maxY");
            return true;

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



Share this post

Link to post
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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!