View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Delta time & Collision Detection problems

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1Blodyavenger  Members

Posted 13 May 2011 - 11:08 AM

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[i][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[i][xStart] == 1)
{
setX(((xStart+1) * TILE_WIDTH) + (PLAYER_WIDTH / 2));
update = false;
setSpeedX(0.0f);
_frame = 0;
break;
}
}
}

if (update)
{
setX(getX() + getSpeedX() * dt);
}
}

### #2Gorbstein  Members

Posted 13 May 2011 - 12:15 PM

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

### #3Splinter of Chaos  Members

Posted 13 May 2011 - 12:24 PM

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.

### #4Blodyavenger  Members

Posted 13 May 2011 - 01:44 PM

@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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.