Handling collision in grid based game (Snake)

by wixy0   Last Updated September 10, 2019 22:13 PM

I'm trying to implement smooth movement in my snake game, so far everything is working well except when it comes to collision with edges. Basically there is not time to make a turn, I don't know how to explain it better.

void UpdateSnake(Snake *snake, float timeStep)
{
    float f = CalculateTime(snake->head->position.x, snake->head->position.y, timeStep);

    snake->head->position.x += (snakeSpeed * fabs(f) * direction.x);
    snake->head->position.y += (snakeSpeed * fabs(f) * direction.y);
}

timeStep = (1.0f / 60.0f) * 1000.0f (ms)
snakeSpeed = 0.5

Algorithm: When making turn (so key is pressed) I'm calculating time needed to get to the closest grid aligned point, if there is space for another step f = timeStep otherwise f is that time.

Snake repository Everything is in Game.c and GameObjects.c



Related Questions


Tile based smooth snake movement

Updated September 02, 2019 01:13 AM


Keeping simulations in sync over long periods of time

Updated January 03, 2019 11:13 AM

Timestep with multithreading

Updated September 19, 2017 21:13 PM

Calculate TimeStep from FPS

Updated September 20, 2017 15:13 PM