Home | Rules and Guide | Sign In/Create Account | Write a Post | Reddit | #LD48 | #ludumdare on irc.afternet.org (Info)

Ludum Dare 30 — August 22nd-25th 2014 — Theme: Connected Worlds
  • Judging Ends: in 14 days, 17 hours, 18 minutes, 16 seconds
  • [ Real World Gatherings | Ludum Deals | Warmup Weekend (Games) | MiniLD #53 | Wallpaper (1) (2) (3) | Mailing List ]

    [ Play+Rate | Edit | View All Games | My Game ]


    Collisions

    Posted by of Platymuus (twitter: @SpaceManiacX)
    October 6th, 2008 6:03 pm

    I need help with collision code. I want to keep the player from falling through floors, pretty much. Help?

    EDIT: Okay, I’ve got some good collision detection, but there’s something wrong with my moving code.

    bool checkCollision(SDL_Rect &movable, double &vx, double &vy, SDL_Rect solid, bool move) {
    int moveLeft = movable.x;
    int moveTop = movable.y;
    int moveRight = moveLeft + movable.w;
    int moveBottom = moveTop + movable.h;

    int solidLeft = solid.x;
    int solidTop = solid.y;
    int solidRight = solidLeft + solid.w;
    int solidBottom = solidTop + solid.h;

    int clipLeft = max(moveLeft, solidLeft);
    int clipTop = max(moveTop, solidTop);
    int clipRight = min(moveRight, solidRight);
    int clipBottom = min(moveBottom, solidBottom);

    if(clipLeft == clipRight || clipTop == clipBottom)
    return false;

    if(!move)
    return true;

    int clipWidth = clipRight – clipLeft;
    int clipHeight = clipBottom – clipTop;

    if(clipWidth solidX) {
    // moving to right
    movable.x += solidRight – moveLeft;
    } else {
    // moving to left
    movable.x -= moveRight – solidLeft;
    }
    } else {
    // moving along y axis
    vy = 0;
    int moveY = moveTop + movable.h / 2;
    int solidY = solidLeft + solid.h / 2;
    if(moveY > solidY) {
    // moving to bottom
    movable.y += solidBottom – moveTop;
    } else {
    // moving to top
    movable.y -= moveBottom – solidTop;
    }
    }

    return true;
    }

    Please help! If you’re interested in seeing what’s happening, here’s the program: http://files.chocoboheaven.com/uploads/Guests/files/85267_PaintWorld.zip

    Tags: , , , ,

    5 Responses to “Collisions”

    1. demize says:

      Hmm… If the player has a floor below them, then don’t let them move down. All it takes is: if(player.y != floor.y+z){(insert your downwards movement code here)}. Replace z with how much you move per frame, and change this to support how you’re doing it.

      This help?

    2. wonderwhy-er says:

      Not enough info it seems… Is it only floor or there are walls. Is it platformer style where you can jump trough some platforms etc etc. More info please :)

    3. GirlFlash says:

      I usually have a ‘grounded’ variable on objects, and a conditional splits up what movement I allow it before input and collision, where I may need to move the object again or change its grounded variable for the next tick (or whatever you call a timer event) ^_^

      keep in mind this is for a sidescrolly platformer, though I think the template should fit most things, as different platform types can be handled during the collision after you have moved the object (before drawing it though of course).

    4. SpaceManiac says:

      Only walls – no platforms you can sometimes go through. I’m thinking of the kind of walls in Ninja Kitty Vs. The Nukebots. They way the cat hits the walls in that game is almost exactly what I’m looking for.

    5. Detox says:

      Well there is always Box2D if you want it all:
      http://www.box2d.org/

      I’ve seen people store line segments for each floor/platform area and cast a ray out from the center of your player downward, doing quick checks. It’s fast because you can pre-sort the segments and eliminate them quickly based on the character’s x-coord. and it handles multi-floor levels and whatnot well.

    Leave a Reply

    You must be logged in to post a comment.


    All posts, images, and comments are owned by their creators.

    [cache: storing page]