Ask coding questions

← Back to all posts
how to make wall detection in a platformer using p5js?
avibeskrowni (153)

(ignore the repl)

i have planned out a platformer game, but while coding it in vscode i encountered a problem that i don't know how to fix. i don't know how to do wall/collision detection. i have a list of where all the walls and floors are but i have no idea where to start here.

(ps i once saw a p5 phaser game tutorial with wall detection on, but i can't find it anymore.)

Answered by tussiez (1503) [earned 5 cycles]
View Answer
tussiez (1503)

AABB collisions.
In JavaScript:

const checkCollision(obj,obj2) {
return obj.x < obj2.width && obj2.width > obj.x && obj.y < obj2.height && obj.height > obj2.y
// Takes two objects with x,y,height, and width
avibeskrowni (153)

@tussiez that would work, except for the fact that i need to figure out how to position/reposition the player, since platformers have gravity. i'm not really looking for code (though if there was a simple p5 or js function for this that would be nice) but more of the math involved

tussiez (1503)

@avibeskrowni p5 is a canvas library, not really a complete game engine. MDN has a good tutorial on very simple physics in canvas. You can try that, or use a physics engine like box2d

avibeskrowni (153)

@tussiez i've checked out box2d, it's very helpful! thank you