0
votes

I am working on a platform game and I am having issues with some collision detection. The game is set up with many blocks that are next to each other or spaced out so the player jumps from one block to the next. Right now the player is able to jump on top of the blocks, run into the side of the blocks and get stopped if jumping into the bottom of the blocks. The issues that I am having right now is that if I get in a rare scenario where the player falls on top of a certain part of the block, the game recognizes it as the side of the block for the block next to it and the player falls through the block. So if I am jumping onto the very edge of blocks[3] and blocks[4] is right next to it, the game recognizes that the player has jumped into the side of blocks[4]. Any idea how I can go about fixing this? Below is the code for my collision detection.

function checkBlockCollisions() {
    // If a player collides with a block, their motion should be stopped.
for(var i = 0; i < blocks.length; i ++) {
            var angle = blocks[i].angleTo(player);
            var thisTop = blocks[i].y - blocks[i].height / 2;
            var thisBottom = blocks[i].y + blocks[i].height / 2;
            var playerTop = player.y - player.height / 2;
            var playerBottom = player.y + player.height / 2;

    if(blocks[i].collidesWith(player)) {
        if((angle >= 55 && angle <= 360) && player.falling) {
        // If the player is above the block, then they should stop falling.
            player.falling = false;
            player.onBlock = true;
            player.setDY(0);
              // Make sure the player is not inside the block.
            player.setPosition(player.x, thisTop - player.height / 2);
                } 
                    //top of block is lower than the middle of the player to the top and bottom of block is higher than the middle of the player to the bottom
              if(thisTop > (player.y - player.height / 2) && thisBottom < (player.y + (player.height / 2 ))) {
                // If the player runs into the side of a block, only stop the sideways motion.
                    player.setPosition(player.x - player.dx, player.y);
                    player.setDX(0);
                } // end if
                else if(player.falling) {
                // If the player is below the block, then they should keep falling but stop accelerating upwards and sideways.
                    player.setDY(0);
                    player.setDX(0);
                    player.setPosition(player.x, thisBottom + player.height / 2 + 1);
                } // end if
            } // end if
        } // end for
    } // end checkBlockCollisions
1
This question gets asked a lot. Collision detection is HARD. If you manage to solve this little bug, you'll find another one, and then another one and your engine will always be in a state that "works 99% of the times! only tiiiiny bug, when $stuff then $weirdstuff happens". IMHO the only way out is to use a library... go with box2d - Exceptyon

1 Answers

0
votes

Check out my engine, it's an engine written entirely for collisions and is very simple to use: https://github.com/Murplyx/AAE---Axis-Aligned-Engine