Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8011335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:59:15+00:00 2026-06-04T18:59:15+00:00

Possible Duplicate: Determine if two rectangles overlap each other? Considering I have 2 squares

  • 0

Possible Duplicate:
Determine if two rectangles overlap each other?

Considering I have 2 squares for which I know the x and y positions and I also know the size, what would be the formula to use if I wanted to see if the objects collide with eachother.

if(   ((shapeA->getX() - shapeA->getSize()) > (player->getX() - player->getSize())
    && (shapeA->getX() + shapeA->getSize()) < (player->getX() + player->getSize()))
    && (shapeA->getY() - shapeA->getSize()  > player->getY() - player->getSize()
    && (shapeA->getY() + shapeA->getSize()) < (player->getY() + player->getSize()))
                )

This works, but it works strange (not all the time). I must be missing something

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:59:16+00:00Added an answer on June 4, 2026 at 6:59 pm

    It’s very easy to check whether a rectangle intersects or touches another rectangle. Have a look at the following picture:

    Rectangular intersection

    As you can see, two rectangles intersect if the intersections between ([x,x+a] and [X,X+A]) and ([y,y+b] and [Y,Y+B]) both aren’t empty.

    struct Rectangle{
        bool intersects(const Rectangle&);
    
        unsigned int a; //!< width of the rectangle
        unsigned int b; //!< height of the rectangle
        unsigned int x; //!< x position
        unsigned int y; //!< y position
    };
    
    bool Rectangle::intersects(const Rectangle& oRectangle){        
        return  (x < oRectangle.x + oRectangle.a) && // [x,x+a], [X,X+A] intersection
                (oRectangle.x < x + a)            && // [x,x+a], [X,X+A] intersection
                (y < oRectangle.y + oRectangle.b) && // [y,y+b], [Y,Y+B] intersection
                (oRectangle.y < y + b);              // [y,y+b], [Y,Y+B] intersection
    }
    

    So your code should be

    if(((shapeA->getX() + shapeA->getSize()) > (player->getX()) // x intersection
        && (shapeA->getX() < (player->getX() + player->getSize())) // x intersection
        && (shapeA->getY() < player->getY() + player->getSize()  // y intersection
        && (shapeA->getY() + shapeA->getSize()) > player->getY())  // y intersection
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Determine framework (CLR) version of assembly I have a library/DLL file which
Possible Duplicate: jQuery: How to determine which <li> tag was clicked? I have a
Possible Duplicate: Best way to determine if two path reference to same file in
Possible Duplicate: MySQL: determine which database is selected? I'm trying to debug some problems
Possible Duplicate: How to determine the class of a generic type? I have a
Possible Duplicate: Best way to determine if two path reference to same file in
Possible Duplicate: How to determine if a decimal/double is an integer? I have a
Possible Duplicate: How can I determine a web user's time zone? Which is the
Possible Duplicate: Best way to determine if two path reference to same file in
Possible Duplicate: Determine device (iPhone, iPod Touch) with iOS I have tried to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.