Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: 2d-platforms game

  1. #1

    2d-platforms game

    I'm making a 2d-platforms game, and I already made the engine, with paralax scrolling, three layers, some sprites and colisions.

    The problem is the colision with the platforms (ground, walls, etc.). I already made it, I check 2 or 3 points on each side of the sprite to see if it collides with any wall. If it does, I do not let it go any further. The problem is...

    Do you know this platforms you can jump from below??
    How can I manage the collisions with this kind of platforms efficently.

    thank you

  2. #2

    2d-platforms game

    I think you would have to have a boolean like TopSideCollide: Boolean in the platform object which says:

    If (Player is falling) then (engage collision detection on platform object) else (Let him through)

    I'm going to assume that the player has x and y velocities, so when the players Y velocity is positive (going down the screen i.e falling) then make the collision detection effective. I think I understood your question correctly but it took me a few passes before I got it. Let me know if this is or isn't helpful.
    Isometric game development blog http://isoenginedev.blogspot.com/

  3. #3

    2d-platforms game

    Thank you, your're very kind.
    You understood perfectly what I was trying to ask.

    Anyway, I already considered that solution. But there's a big problem.

    Speed movement here is not framerate based (I don't use a fixed frame rate and move the sprites each frame at a fixed speed).
    I use speed based on how fast the frames are beeing rendered. If at a certain point the framerate drops, the speed per frame raises, and the other way round. this way, the objects move always at the same speed independent of the FPS and the CPU.

    And here is the problem.
    If I do what you say, in a certain point, imagine I'm about to land on a platform (5 pixels above), the movement for that frame could be bigger (imagine the FPS drops), so the sprite falls 10 pixels that frame (imagine), and passes by the ground, so it does not collide with its feet on the platform.

    I don't know If I explained myself clearly.. (hope so)

  4. #4

    2d-platforms game

    Yes, I understand what you mean and is a common problem with collision detection. What I would suggest is that instead of suddenly moving the sprite 10 pixels or however many,you do a loop and test each fraction of the movement of the sprite from 1 to HoweverManyPixels for collisions, then if there are no collisions you move the sprite the required amount of pixels otherwise you store the loop counter value at the point of collision and only move the sprite that far. This will mean that when the sprite moves it only moves as far as it can before colliding instead of passing through a solid object. This would only be a check loop and machine would not update the screen during this loop and should happen very fast and so shouldn't affect speed too much. I hope this helps, if your still having real trouble then you might be able to twist my arm into writing some example code but see how you get on with explanation. Good Luck.
    Isometric game development blog http://isoenginedev.blogspot.com/

  5. #5

    2d-platforms game

    Thank you once again

    No, there's no need for it, I understood what you said, and I think it is a great solution.
    Now that I see you know a lot of this, jejej, I would like to ask you another simple question if you don't mind (i find so little help around).

    Remember what I said in the first message? :
    "for collision.. I check 2 or 3 points on each side of the sprite to see if it collides with any wall. If it does, I do not let it go any further".
    I check right collisions when the sprite moves right (only in that case, not in other), the same with the left direction. I always check the down collision, and only check up collision when the sprite goes up (jumps)...

    Is this ok? Should I have to check collisions in all directions always?

    I thank you very much for your help, really.

  6. #6

    2d-platforms game

    Quote Originally Posted by patroclus02
    I find so little help around
    I've spotted some of your posts in other forums (Turbo, Delphi Sanctuary...) and you do seem to get few responses. If I could be so bold, I'd ask whether you'd be willing to write a small tutorial on collision detection for a 2d platformer once you've solved all your issues. I know that this seems like you're doing the grunt work, but you'd be helping a lot of people who might find themselves in the same position (having questions, but having a hard time getting answers, about 2d platformers).
    "All paid jobs absorb and degrade the mind."
    <br />-- Aristotle

  7. #7

    2d-platforms game

    Yes.

    I'm thinking of writing a tutorial not only for collision, but for making a 2d platforms game. If I manage to finish the game (or if I get a good Beta version), I'll try to write about how I implemented the engine, what features it has, how I manage movements, timers, colisions, paralax scrolling, sounds and level desings.
    As I'm very busy, I don't think I could wirte a very detailed tutorial, but I'll try to do something useful.

    Anyway, I still have several problems, and need the help of people like Crisp_N_Dry.

  8. #8

    2d-platforms game

    I think the best way of doing collision detection for a game like yours is to use a bounding box. I don't know whether you're familiar with the term so I'll explain. A bounding box is a set of coordinates that specifies the left, right, top and bottom extremes of your sprite. This is effectively a TRect. Using a bounding box and an easy collision function as shown below, you can check for collisions on all sides of the object at every pass. This saves you applying different collision detection rules for every possible direction. All you do is check whether the sprites bounding box is overlapping with the bounding box of another object.

    function Collision(Rect1,Rect2: TRect): Boolean;
    begin
    Collision:=False;
    //If top left corner of bounding box 1 (Rect1) is in bounding box 2 (Rect2) then make Collision=True
    If ((Rect1.Left>=Rect2.Left) and (Rect1.Top>=Rect2.Top) and
    (Rect1.Left<=Rect2.Right) and (Rect1.Top<=Rect2.Bottom)) or
    //...bottom right corner of bounding box 1(Rect1) is in bounding box 2 (Rect2) Then...
    ((Rect1.Right>=Rect2.Left) and (Rect1.Bottom>=Rect2.Top) and
    (Rect1.Right<=Rect2.Right) and (Rect1.Bottom<=Rect2.Bottom)) Then
    //...Collision detected
    Collision:=True;
    end;


    Now just apply this to to yur sprite(Rect1) and every active enemy/object(Rect2) on screen/level and hopefully you will have working collision detection that can stll be aplied to the methods I showed you in previous posts.

    I hope this helps you and you don't have to change too much code, but I know having to redo certain parts of code can often affect the rest of the code and means a hell of a lot of work so if you don't wanna use this idea then just stick to you current method. As always, if you don't think this is suitable or need anymore help then just post a message to that effect and I'll see what we can do.

    P.S. In answer to your original question, I can't think of any reason why your current method shouldn't work but, this method will make sure that you are always checking for collisions on the whole sprite and not just one side and so may be more definitive.
    Isometric game development blog http://isoenginedev.blogspot.com/

  9. #9

    2d-platforms game

    Yes, that solution will require a lot of changes.

    I use 2 different collision detection methods.
    What I do is this :
    1) I'm using Borland Delphi and Omega/DelphiX, so for enemy colision detection I use Omega's function (it automatically detect collision with other sprites, so it saves a lot of work).

    2) Platforms and walls are the real problem
    Here I need more information than just knowing that there's been a collision. I need directions. What I do is use 4 functions, one for left collision, another for right, up and down.
    Example :
    When the sprite moves right, I calculate the current speed and the pixels that are going to be "walked", then I call the right_collison function, that cheks that there are no walls or obstacles. If there are, then it returns the maximun numer of pixel that can be walked before we have to stop (colision). Same applies to left side, botton side, and top side. Botton side is always checked cause os gravity.

    What do you think?
    Today I've been working on diagonal platforms (45 degrees). I managed to do something, but I'm finding that it is quite difficult task. (i don't know really if finally i'll go on with it). Anyway, I need to definetly implement my collision detection method for platforms.

    Thanks man!

  10. #10

    2d-platforms game

    Crisp_n_Dry are you there?

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •