How do I set up moving platforms in a 2D side scroller to interact with the player properly?

  • How do I set up moving platforms in a 2D side scroller to interact with the player properly? user975989

    Edit: I changed the way that gravity was applied so that it is applied even when a player is standing on a block, so instead of the game outputting that you are stepping on a tile as true and false (because if it is true gravity isn't applied, and it is moved out of collision thus in the next frame it does not collide) so now I'll be easier to check if the player is still colliding with a moving tile. I think that I will make a bool that checks if the player was colliding with the tile in the last frame, and if so set the velocities to equal that of the tile plus whatever they are doing (jumping, gravity, walking left and right).

    I am trying to create moving tiles such as the ones in basically any platformer. The game is a 2d sidescroller, and the problem that I am running into is handling standing on moving tiles.

    The tiles are stored in a vector, but I am unsure how to make it so that the play will have the same y and x velocity as the tile they are standing on, as well as not fall off it.

    I was thinking of creating a pointer to the tile they are colliding with on the top (that is standing on it) and setting the velocities to be equal to that plus whatever the player presses (so going left or right or jumping).

    However, I am unsure how to check if they collide and how to check if the player had jumped off the tile. I know how to check if the player's collision is at the top using half widths and half heights, but beyond that is a mystery.

  • This can be a bit tedious to do in raw C++. Are you using any libraries, if so what are they?

    My personal suggestion would be to use a 2d physics engine called Box2D.

  • Your idea to pass a pointer is the right way. If a collision occurs between two objects pass a pointer to each. Then each object can decide how to alter the properties of the coliding one. Try to differ between objects by attaching a property (like PLAYER, OBSTACLE, POWERUP) to handle collisions with different types in other ways.

    For checking collisions the easiest way is to use libraries. At least just look at the docs to get some ideas how to implement collision detection in your own way. For Example

  • I think you are having trouble because you might have confused yourself for world coordinates with camera coordinates.

    In a simple 2D game, the player is always in the 0,0 (center) camera view (assume no camera easing), but the world coordinate keeps changing.

    When the scene/background/hit-map moves, you can think of it as a camera position offset. However, everything on the map is still the same position relative to the world coordinates.

    So when you do hit collision, just use world coordintates. Don't try to do hit collision in camera coordinates, at least not conceptually, and unless it is a mouse click collision test. Or your brain will explode!

    And if your chracter changes world position, just apply the character world position offset to the scene's camera view offset.

    EDIT: For moving tiles, you can "tag" the character to the tile when he lands, and "de-tag" it when he is in the jumping state. On each animation frame, change the position of the tile by an offset and for each tagged object, change their position by the same offset. Since at the beginning of each frame snapshots are static, just do bounding box collision test like normal.

    This "tagging" is that same idea as you would equip a 3D character RPG with armor and weapons.

    To handle the case where player simply walks off the edge: while the character is tagged to a tile, check if character x coord is outside or inside tile min and max x-coord to see if it is still on the tile. De-tag or re-tag to another tile as necessary.

    imo, we want to make the game run as efficient as possible and do away with unecessary computations. so I would not think the gravity idea is the best. gravity only needs to be applied when character is in mid-air. Can easily use the chracter jump and land events to turn gravity on and off

  • Hahah, this is a pretty common problem, here's what I do->

    Step 1: collision detection: Be sure you're separating your map into sections or using quadtrees to speed up grouping,

    Step 2: basic collision resolution, my preferred method works great for moving platforms, I always preform this check

    if (myPreviousBottom <= theirPreviousTop){ //bottom side collision
      //move upwards outside of range of other block
      //add their velocity to our position (THIS is what you want to implement)
      //I also have a quick method that's overloaded in other classes called_bottom collide() 
      //and another method of sending events between objects, I send an event notifying
        //the other object of the collision here
    if (myPreviousTop >= theirPreviousBottom){
      //simply move our position outside of theirs
    if (collide(us,them)){
      if (myPreviousRight <= theirPreviousLeft) { //we have a right hand collision
       //check to see if we should slip by the platform
       //move object in question to the position of the other object - our object's size
       //repeat for left side

    The important step here is when you have a bottom collision you add the other object's velocity to your position. This allows you to move about freely on their platform and have your own physics unaffected by it's (you may want to, instead, change your axis velocity to theirs and add their other axis's position to the object in question)

  • There are several ways to deal with this situation. One way is, whenever the player is standing on something (a platform, a monster's back, etc.), to work (conceptually) in a reference frame moving along with whatever the player is standing on. So, as far as the player's movement is considered, the platform they're standing on isn't moving — the rest of the world is.

    Of course, this means you need to keep track of what (if anything) the player is standing on. It also means that whenever this changes, e.g. because the player jumps, falls off or steps from a moving platform to a stationary one (or vice versa), you need to change reference frames; depending on your implementation, this typically means e.g. adding the previous platform's velocity to that of the player when they jump off it.

    One convenient feature of this method is that you don't need to worry about gravity except when the player is airborne. In fact, you can make jump physics and walking physics as different as you like, which is often useful in games — the approximations that work well for (semi)ballistic motion in mid-air are not the same as those which work when you're in contact with a solid immobile surface.

    Conversely, one thing that may be a problem with this approach is if the platform your player is standing on undergoes rapid acceleration. (Think of a spring-loaded platform that launches the player into the air like a catapult.) In that case, you may need to include extra checks to see if the player should fly off or slide along the platform (or fall over, if you model that kind of stuff).

    (In principle, the player should be tossed off if the downward acceleration of the platform they're standing on exceeds gravity, and should slide if the sideways acceleration exceeds the total downforce (sum of upward acceleration and gravity) times the coefficient of friction. Of course, many games essentially assume infinite sliding friction and otherwise neglect most effects of horizontal acceleration. In particular, if your platform moves horizontally and suddenly reverses direction, that should cause anyone standing on it to stumble — but I've never seen a game actually do that.)

c++ collision-detection physics
Related questions and answers
  • == true){/*do something here or i might not need it.....*/} } /* end of if(kbhit())*/ char coll; // the buffer for the collided char theScreen.check_collision(player, xMove, yMove...Alright so i'm making a vertical side scroller where you are an '8' character traveling downward while avoiding my randomly generated walls (the "generation" function) on the left and right sides... from it. As an added touch i have made it so after you collide while traveling down the randomly generated map (or rather as the walls move uppward while your character stays put) the X chars you've

  • well... I'm building the animation system of my game engine (the skeletal and skinned animation stuff), and I came to a point where I added so much functionality in the frame and node structures... value of the reflection (a transition of this value can be used to achieve some effects) bool bHidden; // Set this variable to true to skip rendering this frame bool... I was thinking about making smaller structures with the fTime variable each so I can have attribute keyframes, light keyframes and buffer keyframes, then make the node structure look like

  • I am trying to create a 2D platformer (Mario-type) game and I am some having some issues with handling collisions properly. I am writing this game in C++, using SDL for input, image loading, font... I need to start with. What I need is an easy way to both detect which side the collision occurred on and handle the collisions properly. So, basically, if the player collides with the top..., so that's worth explaining. coords[0] represents the x position (left) of the object (where it starts on the x axis). coords[1] represents the y position (top) of the object (where it starts

  • am trying to get my gravity to work. The code I have so far is if (jumpvelocity < 0){ yvelocity = jumpvelocity * t *2/gravity; jumpvelocity += 185.f*t * 2 / gravity; } else if(!grounded...Edit: I fixed the problem by making jumpvelocity the one that is modified and added onto, and yvelocity just equal jumpvelocity * t. This is what I have right now: if (GUI->Space &&... is 1. Jumpvelocity is set to 185 when the player wishes to jump. My problem is that you fall slower at a slower framerate, but you jump at the same speed as if it was a higher framerate. How would I

  • I'm making a game with Box2D with a top left coordinate system. I multiply positions by an M_TO_PX_RATIO of 10.0f to convert from meters to pixels. I noticed that when I set gravity to 9.8, the simulation is rather slow. When I set gravity to 9.8 * M_TO_PX_RATIO, the simulation runs at the correct speed, like normal gravity. However, the high gravity causes jittering. Am I doing it properly? When using a top left, pixel coordinate system, must I do anything else to account for meters to pixels? Thanks

  • second and 75 frame draws per second. Up until now, it is in the physics update routine that I do the collision detection and response. Your ship can collide with a rock. Your photons (bullets) can collide with a rock. Now I am considering improving it to use interpolation as per all the good articles recommend: and on as large a Universe as possible. So a physics update solution that is compatible with the networked game that is required. Is the interpolation worth the effort? Right now. I rarely if ever see

  • After spending time today to jot down some notes regarding the implementation of walls into my tile-based game, I've suddenly realized it's not going to be as simple as I imagined before. While... the players double-wall the whole map, I'm going to beat them to it. Every wall has two halves that are attached to the edge of the tile from inside. So, to make a single "Wall unit" I'll have to create... it - three distinct ways of making walls between tiles. If there are any alternatives out there - I'll be happy to check them out. If there are any benefits/downfalls to any of the approaches that I didn't

  • ; glBegin(GL_QUADS); // Top-left vertex (corner) glTexCoord2f( sourceX, sourceY); glVertex2i(x, y); // Bottom-left vertex (corner) glTexCoord2f( sourceX + texscale, sourceY); glVertex2i( x + tileWidth, y); // Bottom-right vertex (corner) glTexCoord2f( sourceX + texscale, texscale + sourceY); glVertex2i( x + tileWidth, y + tileHeight); // Top-right vertex (corner) glTexCoord2f( sourceX...I'm using SDL & openGL to render a tile-map. The issue is that the tile-map rendering is extremely messed up, and I'm just a bit unsure what I'm doing wrong exactly. It should just be the first

  • to collide with.. as follows. I expect there is issues with this as I get objects colliding with the ground but half way through it and they glitch around like crazy on collision. void GameState...::toString(m_pNumEntities), "cube.mesh"); entity->setCastShadows(true); Ogre::AxisAlignedBox boundingB = entity->getBoundingBox(); // The ogre bounding box is slightly bigger so I am reducing... physics properties. I know there will be errors here as I get the items colliding with the ground but not with each other properly. So I would appreciate some input on what I am doing wrong. void