Jump to content

  • Log In with Google      Sign In   
  • Create Account


simple collision detection for nearest rectangle


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 umen242   Members   -  Reputation: 141

Like
0Likes
Like

Posted 05 February 2014 - 01:39 PM

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 .

 

 

Attached Thumbnails

  • rec_collision.png


Sponsor:

#2 ProtectedMode   Members   -  Reputation: 721

Like
0Likes
Like

Posted 05 February 2014 - 01:45 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS