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

  • SEARCH
  • Home
  • 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 7600511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:55:40+00:00 2026-05-30T22:55:40+00:00

How do I calculate a collision between two rects in C++ & SDL, and

  • 0

How do I calculate a collision between two rects in C++ & SDL, and how do I make the player not able to go through this rect (i.e. ensure one rect cannot pass through the other)?

I know to stop the player would be playeryvel = 0, making the player’s Y velocity 0, so they cannot pass through it. My problem is, this will stop ALL vertical movement, when I want to stop movement through the other rect.

My current code uses a function named check_collision(SDL_Rect, SDL_Rect). Here’s my the code for the usage, and the actual function.

// This loops through a vector, containing rects.
for (int i=0; i<MAP::wall.size(); i++)
{
    std::cout << i << std::endl;
    cMap.FillCustomRect(MAP::wall.at(i), 0xFFFFFF);
    if (check_collision(cMap.wall.at(i), cDisplay.getPlayer(playerx, playery)))
    {
        exit(0); // It exits just as an example to show if there actually is a collision
    }
}


bool check_collision( SDL_Rect A, SDL_Rect B )
{
    //The sides of the rectangles
    int leftA, leftB;
    int rightA, rightB;
    int topA, topB;
    int bottomA, bottomB;

    //Calculate the sides of rect A
    leftA = A.x;
    rightA = A.x + A.w;
    topA = A.y;
    bottomA = A.y + A.h;

    //Calculate the sides of rect B
    leftB = B.x;
    rightB = B.x + B.w;
    topB = B.y;
    bottomB = B.y + B.h;

     //If any of the sides from A are outside of B
    if( bottomA <= topB )
    {
        return false;
    }

    if( topA >= bottomB )
    {
        return false;
    }

    if( rightA <= leftB )
    {
        return false;
    }

    if( leftA >= rightB )
    {
        return false;
    }

    //If none of the sides from A are outside B
    return true;
}
  • 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-05-30T22:55:41+00:00Added an answer on May 30, 2026 at 10:55 pm

    Simply push player away from the point of collision, every frame. You don’t need boolean tests (collide or not), you need to adjust player position if collision occurs.

    1. Update player position (position += velocity * time);
    2. If collision occurs, push player away from point of collision so no collision occur.

    This can work really well, because you’ll be able to “slide” along walls, etc. To do that you need to find point of contact, depth of intersection and direction of intersection (i.e. in which direction you should push the player to move away).

    Of course, you’ll need to calculate how far you should move the player, but in 2D it is extremely easy to do.

    You need to calculate how much two rectangles overlap. Then you push player rectangle in the direction (x or y) that overlaps the most (only in x or y direction, unless they’re equal).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I calculate the distance between two points specified by latitude and longitude?
I want to calculate total say Sundays,Mondays...Saturdays between two days. I want a do
How do you calculate the distance between 2 cities?
I want to calculate the time span between 2 times which I saved in
Okay, so I'm trying to make a game that uses this algorithm: http://www.codeproject.com/Articles/15573/2D-Polygon-Collision-Detection But
I have a hierarchy like this: Scene  - gameWorld (CCLayer Z:0)     - player (CCSprite
Function calculate_attribute does not return a value; it only works through side effects. Often
What is the difference between a multi-collision in a hash function and a first
I would like your suggestion about this problem... To make it simple, I'll consider
In calculate if two arbitrary regular expressions have any overlapping solutions (assuming it's possible).

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.