I am currently developing a side-scrolling platformer game using Matter.js. However, I'm encountering issues with the collision detection feature. I need assistance with creating a collision detector that can determine if the player is touching any object in the game.
Currently, I have a detector that uses the following code:
Matter.Events.on(engine, 'collisionStart', function(event) {
event.pairs.forEach(pair => {
if (pair.bodyA.label === 'foot' || pair.bodyB.label === 'foot') {
touchingWall = true;
}
});
});
Matter.Events.on(engine, 'collisionEnd', function(event) {
touchingWall = false;
});
Note that 'foot'
refers to a smaller hitbox placed below the player's cube, which prevents a bug where the player is launched into the air if they are close to a wall.
While the code currently works, there are multiple issues with it. It stops working when I add cubes ( not static ) to the game. Can you help me with this problem?
If you wish to view what i currently have, here is a link to my github repo: https://github.com/MemeCake789/Delta
What I have right now is functional, but I need to modify the code to enable the player to jump only when they are on top of a surface, and disable the jumping ability when they are in mid-air or not in contact with any surface.