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 9228233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:16:21+00:00 2026-06-18T05:16:21+00:00

I am implementing some very basic platforming physics in C++ for a simple tile-based

  • 0

I am implementing some very basic platforming physics in C++ for a simple tile-based platformer. I am following the algorithm here (https://gamedev.stackexchange.com/questions/18302/2d-platformer-collisions) as exactly as I can yet I am still getting a weird glitch where the player bounces on a tile. I’m not exactly sure what is going on. The code is in C++ using SDL

void Player::update(vector< vector<Tile> > map) {
vector<Tile> collidingTiles;

x += xVel;
y += yVel;

boundingBox = Rect(x,y,16,16);

for(int iy=0;iy<MAPH;iy++) {
    for(int ix=0;ix<MAPW;ix++) {
        if (map[ix][iy].solid == true) {
            if (boundingBox.collide(map[ix][iy].boundingBox)) {
                collidingTiles.push_back(map[ix][iy]); // store all colliding tiles, will be used later
                Rect intersectingRect = map[ix][iy].boundingBox; // copy the intersecting rect
                float xOffset = x-intersectingRect.x; // calculate x-axis offset
                float yOffset = y-intersectingRect.y; //calculate y-axis offset
                if (abs(xOffset) < abs(yOffset)) {
                    x += xOffset;
                }
                else if (abs(xOffset) > abs(yOffset)) {
                    y += yOffset;
                }
                boundingBox = Rect(x,y,16,16); // reset bounding box
                yVel = 0;
            }
        }
    }
}
if (collidingTiles.size() == 0) {
    yVel += gravity;
}
};
  • 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-18T05:16:22+00:00Added an answer on June 18, 2026 at 5:16 am
    if (abs(xOffset) < abs(yOffset)) {
        x += xOffset;
    }
    else if (abs(xOffset) > abs(yOffset)) {
        y += yOffset;
    }
    yVel = 0;
    

    In this code, if abs(xOffset) == abs(yOffset), nothing happens, meaning that the player can enter a solid tile diagonally. Removing the second test should remove the issue and modify y if the algorithm has to make a choice:

    Also, I’m wondering if you really want to reset the vertical velocity if a horizontal collision occurs. This also implies that gravity should still apply if the collision was horizontal (otherwise the walls and ceiling would be sticky).

    int gravityApplies = true;//fix 2a
    
    ...
    
        if (abs(xOffset) < abs(yOffset)) {
            x += xOffset;
            xVel = 0;      //fix 2
        }
        else {             //fix 1
            y += yOffset;
            yVel = 0;      //fix 2
            if( yOfsset < 0 ){ // fix 2a
                gravityApplies = false;
            }
        }
    
     ...
    
     if (gravityApplies) { //fix 2a
         yVel += gravity;
     }
    

    If you want elastic collisions, use yVel = -yVel instead of yVel = 0 (and similar for the other direction). You can even do yVel = yVel * rest where rest ranges from -1 to 0 (-0.8 is a good choice) to get semi-elastic collisions.

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

Sidebar

Related Questions

I had some troubles implementing Lawler's algorithm but thanks to SO and a bounty
I try to create a very simple physics engine for my study (processing used
I'm working on implementing a very, very basic component system in C , but
I'm implementing a naive keyword extraction algorithm. I'm self-taught though so I lack some
We've seen some very useful methods of implementing EF4 Repository and Unit of Work
This could be a very basic question but i want to confirm here. Im
I am implementing a graph library and I want to include some basic graph
I'm implementing some classes for linear algebra operations on very small constant size vector
EDIT (2011-07-23) Have gotten some very helpful answers, both of which I've tried implementing.
I am very new to OpenGL ES. I am implementing some demo app to

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.