# Pixel perfect collisions for platforms

I'm trying to create pixel perfect collision detection but I don't know how to handle collisions when there is a jump. Here I have one image of a sample map:

But maybe I had a bad supposition assuming that all the pixels are little collision points.

I mean , there is no differences between y-axis collision and x-axis collision in my collision map.

I have been treating each axis collision split, but that is bad done when diagonal jump have been made, cause you'll get an error in x or in y while correcting the position.

Then I thought that maybe I was wrong in my supposition that I didn't need to split for pixel collision type.

I will resume it in 2 questions:

• how can I handle pixel perfect collisions for a platformer?
• if I'm doing it well, How to manage the x-axis, y-axis problem?

P.S: My 'player' uses a box collider.

• I don't understand the problem description. Could you draw it? Also, "pixel perfect" might mean one of very many things. What does it mean here? – Anko Mar 18 '13 at 14:04
• There is no single question of how to handle collision for a platformer. About pixel perfect collision? It really depends what you mean by that. With the image you provided, you don't need to involve pixels into collisions as they are unnecessary and resource-intensive. You can do tile-based collision, have you tried that? You also mention that you've made some collision but that it has some x-axis and y-axis problem, could you elaborate on that? The general answer is if it works, there is no reason to fix it. What exactly is the problem? – TomTsagk Sep 5 '18 at 10:08

I use masks to do pixel-perfect collision checks between my player and enemies. (I don't ever feel the need for pixel-level precision when it comes to platforms and walls.)

A mask is essentially a silhouette of a sprite, using 1-bit per pixel to encode where the sprite's image has transparency and where it doesn't. Many engines and libraries have built-in functions for creating masks from images and checking for overlap between masks.

If you do end up using masks for collision tests, you should always test for regular rectangular collision first, and only test for mask collision in the event of rectangular collision. Testing for mask collision is computationally expensive, but testing for rectangular collision is not.

For one-way platforms, I usually create a boolean attribute in my platform class called "is_one_way", and set it to true for platforms I want to be one-way. In my collision code, if the wall is one-way, I only resolve in the event that the sprite is above the platform and his y-velocity is positive.

There are different approaches you can use. For a start, I'd use a rather simple (maybe even a bit naive) approach:

You're essentially checking two points on your character for now:

• The position where he's standing (feet; actually a bit right below the character).
• The position of his head (head).

The character is then moved based on the following rules: - If feet is not solid, drop down. - While feet is solid, move up. - If feet and head both are solid, the character dies (squeezed). Ifheadis solid, stop vertical velocity/jumping. - Whilehead is solid, move down.

Checking left/right works in a similar way. Doing this right will also solve stuff such as ramps instantly for you.

Reading through the poitns above, it should be clear, that you essentially "bounce" the character instead of having him standing still. This is true, but the player won't notice, because you don't draw inbetween these steps.

• the question is that i can't stop the player cause he must go through the panels when jumping , and another thing is that if i move up the player maybe he musn't be there , he must maybe be on the left cause he couldn't have gone on the top. – A.Quiroga Mar 18 '13 at 11:48
• Ah, so those are jump-through platforms? Rather easy, you just exclude them when checking your head` point. Once you've got these basics, you can check more positions (or just check rectangles rather than points). So you'd end up with 4 rectangles. The bottom one keeps you from falling through platforms, the top one keeps you from going into solid ceilings and the left/right one keep you from running/jumping into walls or ramps. – Mario Mar 18 '13 at 12:01