Trevor Bollinger
Trevor Bollinger

Reputation: 31

Ive been working on a "mario like" game engine in p5.js, im having some serious trouble with collision avoidance

By collision avoidance, i mean stopping the player from walking through something. Like in mario, he can't just walk through the blocks. I technically succeeded in making this, but it's very very bad. The player often gets stuck on the block once the player hits it, and i cant figure out how to fix it. I put all the code together on an online p5.js editor, Here

In the code i linked, I'm trying to get the player to not go through any of the terrain constructions i make, the one i have currently set up is a red square named 'block1'

Upvotes: 0

Views: 229

Answers (1)

Kevin Workman
Kevin Workman

Reputation: 42174

Stack Overflow isn't really designed for general "how do I do this" type questions. It's for specific "I tried X, expected Y, but got Z instead" type questions. But I'll try to help in a general sense.

You need to take a step back and break your problem down into smaller steps and then take those steps on one at a time. Can you get a simple program working that just shows two hard-coded rectangles that change color if they're colliding? Get that working perfectly before moving on.

Shameless self-promotion: I wrote a tutorial on collision detection available here. That's for regular Processing, but everything should be basically the same for P5.js. Generally you probably want to use grid-based collision detection to figure out which cell your player is in, and then use rectangle-rectangle collision detection to actually check whether the player has hit a block.

If you're having trouble, please debug your code and try to narrow your problem down to a MCVE and ask a specific technical question. Good luck.

Upvotes: 0

Related Questions