• 13
• 18
• 19
• 27
• 10

# Tile-Based Collision\Physics Problem

This topic is 2083 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello guys this is my first post and I hope I'm posting it in the right place..

so I'm working on a tile-based game: 16x16 player, 16x16 tiles. the hitbox for the player is 16x16.

I got a problem that I have no idea how to solve, I tried different type of collisions to do it, basically the player's velocity is so high that he just flys above 1-tile holes..

let's say i'm trying to jump into 1 space hole vertically horizontally or whatever and I can't because of my velocity, even if it's very low, 1 pixel off is enough to make him not fit inside the hole, and I have no idea what to do, help?

##### Share on other sites
If I understod right: try 8x8 (or similar) hitbox from the center of player?

##### Share on other sites
I don't want to reduce the hitbox, I want to keep it the way it is but get the player to somehow still fall through holes without reducing the hitbox

##### Share on other sites
Most games just ignore this problem. If it's a tight fit and you're moving fast, you slide right over. If you want to improve it just a little bit, then you could divide the velocity by 2 and them move twice, effectively doubling the frame rate so you can be moving at twice the speed before missing.

Alternatively, you could do some kinda funky stuff, like, if you're going down and the bottom edge of the collision box is going to cross a tile boundary, then move down to that tile boundary (without bothering to check collisions since you couldn't hit anything without changing tiles), and then do the X motion, and then do the rest of the Y motion. That way regardless of how fast you're falling, you'll always be lined up properly to squeeze into small gaps in walls. Not sure how to apply this to gaps in floors/ceilings at the same time though.

##### Share on other sites
Divide and conquer: Move the objects pixel per pixel. If there are more than one possible movement axis (x and y as in your example) check both in a loop.

Pseudo code:

 DeltaX, DeltaY hold the intended movement. while ( ( DeltaX != 0 ) || ( DeltaY != 0 ) ) { if ( DeltaX > 0 ) Move1PixelRight if ( DeltaX < 0 ) Move1PixelLeft if ( DeltaY > 0 ) Move1PixelDown if ( DidNotFallYet ) CheckIfPlayerCanMoveDown1Pixel } 

##### Share on other sites
You can't use the velocity to move directly.
You need to use smaller step, such as "tileSize - 1", to move, until the total steps meet the velocity.
If the velocity is 20, and your tile size is 16, you first move 15 pixels, then move another 5 pixels.

@Endurion,
Isn't moving one pixel too small and may hit the performance? Any special reason?

##### Share on other sites
Why not just make the player a little smaller?

##### Share on other sites
I don't want to make the player smaller, and I tried all these stuff like dividing the velocity, like merging the x & y velocity and move 1 pixel at a time but it's like very annoying and no method works.. like I'm always led into problems and I know what causes them but I don't know how to fix it.. i feel like i'm asking for something that was never done before lol i know it's weird that everything is 16x16 and everybody tell me to change the hitbox but i really want everything to be 16x16..

*edit: i tried reducing the hitbox and it still flys above the gaps, i reduced it to about 3 pixels in every side

##### Share on other sites
Making the player smaller wont help!.. why not make tetris instead of this game..

wqking got the best answer.. I think you have to test if you must use "tileSize - 1" instead of "tileSize"..
now when u find a hole underneath, align the player to it and make it fall, whatever gravity u have. Edited by SloydD