# How to make snake movement smooth not grid based like stihter io snake

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

## Recommended Posts

Hello all

i try to find the right formula to mimic the smooth none grid movement of the stihter io snake  .

i can do it manually to save each point the "head" movement  went and do some interpolation in between its body parts but it is very error prone.

also i find this : http://www.emanueleferonato.com/2014/06/10/html5-string-avoider-game-made-with-phaser/
which is not working in my case but do show something similar
here is a picture to express what i want to do :

[sharedmedia=core:attachments:33572]

any idea how to do it right ?

Thanks!

##### Share on other sites

I think it works in the same way as grid like snake. You move the head, then you move the next segment (1) to where the head was, then segment (2) is moved to where segment (1) was, etc.

##### Share on other sites

Your snake is sequence of segments. Natural way to represent the snake is using linked list:

struct Segment
{
Vec2 position;
};

Each frame, you will update positions of heads and positions of all following segments and move them towards their next_to_head segment. You should update segments in specific order (from head to tail or from tail to head). Or you can have two copies of each segment, one at the beginnig of the frame, second at the end of the frame and in that case, you could paralelize the simulation.

The simple version, how to update a segment is to force it segment to be in given distance from its next_to_head segment:

void update_segment( Segment * segment )
{
constexpr float RequiredSegmentDistance = 15.0f;
{
Vec2 dist_v = segment->next_to_head->position - segment->position;
float dist = sqrt( dist_v.x * dist_v.x + dist_v.y * dist_v.y );
float move_dist = RequiredSegmentDistance - dist;
if( move_dist > 0.0f )
{
Vec2 change = dist_v * move_dist / dist;
segment->position += change;
}
}
}

If you want it to look more dynamic during acceleration and decceleration, you will not have fixed distance of segments. Distance between segments will be bigger if snake accelerates and lower if snake deccelerated.

##### Share on other sites
Not to be a wise guy, but basically there's always a grid. You just have to decide how big your grid segment has to be, and then build around that (could be 1x1 or 100x100 pixels).

##### Share on other sites

I think you mean http://slither.io and also remember seeing this question on gamedev.stackexchange.com.

##### Share on other sites
You can follow the others folks' tips or simply make transitions between the classics 8 X 8 (or whatever) grids, being snake not inmediately controlable until next grid is reached.

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
5. 5
A4L
13

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633743
• Total Posts
3013644
×