Pixel Collision

Hi,

At the moment my tiles are all 16x16 pixels and they take up all this space. Now, I want to add tiles that don’t, think half a tile, thus top half is transparent, bottom half is filled. So, when a collision occurs, what is best way of handling this type of tile so player moves down half the tile. Would you need to detect this type of tile and allow player to move down 8 more pixels? Would I need to use the alpha channel of the tile or something?

Thanks

How are you handling collisions right now?

Hi,

Basically take player position (world space), convert to my 2d array space. For instance, player at 0,96 would be 0,6 in 2d array. If array[0][6]==Tile then collision.

Thanks

Now I get where your problem is.

One way would be to ditch your current collision model and instead use a different collision representation for your world, like a map/list of rectangles, one for every tile.
You could save a collision file together with your tiles so that you can define the collision shape for every tile outside the code.
If this leads to bad performance, read up on spatial partitioning, quad trees or simple indexed buckets would fit your case.

Detecting the tile and allowing the player a different offset is possible, but that could lead to a big buggy mess once you add more tile types.

Hi,

Just trying to get it into my head how this list of rectangles would work for say slopes?

For instance, would want player to slide down these two slopes but at the moment, this would not happen.


\

I do know about spatial partitioning but didn’t want to go this route as gone a long way with my game and would need a lot of work for this.

I did read something on using Pixmaps, but guess this is buggy on different resolutions…

Thanks

You said nothing about slopes :stuck_out_tongue:
You could use polygons instead of quads and calculate the normal of the edge you are standing on, then apply movement direction and amplitude based on angle.

Or use special movement code for tiles marked as slopes, that makes the players behave different than on normal tiles.

You should spend some time researching: https://katyscode.wordpress.com/2013/01/18/2d-platform-games-collision-detection-for-dummies/
2d platformer movement and collision is not exactly new teritory ::slight_smile:

I’m doing this in my current game where all my tiles are 32x32. So I have static variables for width (I call it SX) and height (SY). I made a thinner tile that is somewhere around 6 pixels high instead of 32. I have a flag, that when I collide with this type of tile the offset from the tile and the player is different than other tile collisions.

So for instance, if I collide with the regular 32x32 tile on top, the players y position is tile.y - player.height

Conversely, if I collide with the other tile type that is 6 high from the bottom up, player.y = tile.y - player.height + offset (offset would be something like 26 with these numbers). This also requires the rectangle I do checks with to be smaller in size than the regular 32x32.

Hi,

I think detecting the tile I’m on and modifying player movement regardless be simplest for my sort of game.

Thanks for the link btw :slight_smile:

ndnwarrior15 - do you have slopes in your game?

Thanks

If a tile is (0,0) for top left and (32,32) for bottom right, and you only have straight slopes and different heights of platform to consider then all you need are two value for height-right and height-left (hr and hl for short). For a tile that fully occupies it’s space, hr and hl equal 0. for a tile that fill the bottom half of the space hr and hl are 16. For a slope hr might be 0 and hl might be 32.