Jump to content
  • Advertisement
Sign in to follow this  
umen242

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
Share on other sites

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

struct Segment
{
    Vec2 position;
    Segment * next_to_head;
};

Head has empty next_to_head field. Next_to_head of segment after the head points to head and so on.

 

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;
  if( segment->next_to_head )
  {
    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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!