# Delta time & Collision Detection problems

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

## Recommended Posts

I'm moving my game from fixed 1ms update to full delta time use and I'm having problems with collision detection now.
If my computer lags for a moment, my character goes trough a ground (ground / objects / walls) .

I tried to find good solution for fixed timestep etc. but I found it too complicated. What would be the best solution for me?

My game is 2D platformer with simple tile based collision detection.
I'm updating everything with dt like (speed = speed + acc * dt)

This is a code snippet for checking "X" collision.
void CPlayer::_checkCollisionX(float dt) { bool update = true; int xStart, xEnd, yStart, yEnd; xStart = (int)(getX() - (PLAYER_WIDTH / 2) - max_walk_speed*dt) / 32; xEnd = (int)(getX() + (PLAYER_WIDTH / 2) + max_walk_speed*dt) / 32; yStart = (int)(getY() - (PLAYER_HEIGHT / 2)) / 32; yEnd = (int)(getY() + (PLAYER_HEIGHT / 2) - 1) / 32; for (int i = yStart; i<= yEnd; i++) { if(getSpeedX() > 0) { if(world.map.Attributes[xEnd] == 1) { setX((xEnd * TILE_WIDTH) - (PLAYER_WIDTH / 2)); update = false; setSpeedX(0.0f); _frame = 0; break; } } else if(getSpeedX() < 0) { if(world.map.Attributes[xStart] == 1) { setX(((xStart+1) * TILE_WIDTH) + (PLAYER_WIDTH / 2)); update = false; setSpeedX(0.0f); _frame = 0; break; } } } if (update) { setX(getX() + getSpeedX() * dt); } }

##### Share on other sites
What about setting a max limit on the deltaTime? If it goes over a certain amount (say 0.066 for 15fps) then you cap it at 0.066. So you might see a judder in the game when your pc lags but you shouldn't get jumps that are big enough for an entity to pass through another.

D

##### Share on other sites
If you need a consistent level of accuracy, you need a constant delta time. If more time has elapsed than one dt (or max dt if you don't need consistency), you have to update the physics/collision more than once. Nothing seems wrong with your collision code itself.

An alternative is to not do collision detection based on an object's state (current position, velocity, etc.) but rather its path over time. This would require saving your previous position and using a more complicated collision function, but since your game seems to be tile-based, maybe that wouldn't be all that hard.

It's about having a constant dt, but even if you don't need or want that, you might find it helpful.

##### Share on other sites
@Gorbstein: This simple trick solved all my ugly problems, thanks a lot! I know it's not the best move against the problem but it serves fine for my purpose.

@Splinter of Chaose: I wanted to go with "check everything on it's path" algorithm in case I don't find anything more simple but I'll leave that for the next time. Thanks