6
\$\begingroup\$

Background

My game is a static screen (ie, no scrolling) 2d platformer and my CD routine is split into 2 - after vertical movements, (ie, gravity etc) I check for collisions, and if they are detected, correct the sprite positions vertically. Then after horizonal movements do the same thing, correcting any overlaps in the horizontal direction.

My CD code iterates through all the tile coordinates, and when it find a collision, it corrects the position, then exits and checks no more until the next game tic.

This works perfectly for static tiles, however, I've introduced platforms which are comprised of 3 horizontal tiles and which move up and down (ie, like an elevator).

The problem

The problem occurs if my character is standing 'over the edge' of a static block and then the elevator is moving up from under him - what I'm after is for the platform to 'catch' his over-hanging side and take him with it if you see what I mean. However, what happens is that he gets shoved to the side. I know why this is, but I'm not sure of the best way to correct it.

I think it happens because gravity is applied, pushing him into the static block, then the elevator move up so he is now embedded in 2 blocks. The vertical CD routine detects the first overlap (say with the static block), corrects it and then moves onto the rest of the game logic (but now he is still embedded in the platform) - then it eventually gets to the horizontal collision detection which moves him out of the collision horizontally. Please see my diagrams below, I hope these make it clearer.

Possible solution

Basically, I guess what I need is for the CD routine to carry on checking once it's corrected the first collision, but this would be so wasteful and 99% of the time it only needs to check one collision. I thought about simply making it check through the tile coordinates twice, but like I say, this seems inefficient. However, I'm trying to keep things as simple as possible and this seems the simplest solution.

Diagrams

enter image description here

enter image description here

enter image description here

enter image description here

Would appreciate any suggestions for how best to get around this........

\$\endgroup\$
0

1 Answer 1

4
\$\begingroup\$

You shouldn't go through all tiles. Go through all characters instead, and do the collision checks and correction against blocks in there. Moving blocks shouldn't be considered blocks, but rather a character that moves, and pushes other characters.

Do the static block collision AFTER the characters moves and collided and got pushed.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Hey @Zupoman, thanks, I did what you suggested. I created a separate object for my platforms and their own collision detection - works great! Cheers :-) \$\endgroup\$ Aug 30, 2013 at 17:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .