Snake movement

Hello!

I’m looking for some specific behavior for a snake. I’m going to describe it
the best I can. Alternatively, you can look here for similar movement.

Here are the few premises that this works with.

  1. The snake is made of induvidual parts, and it can change the angle the head it heading.
  2. Each segment moves at the same pace, all the time.
  3. The segments follow eachother, with the head leading.
  4. Each segment has a set distance to the ones in front of it, and the one behind it. This distance must be maintained.

I have tried to emulate this behavior. I have tried to have each segment find the
segment in front of it, and point/move towards it. This way, however, they don’t maintain the same distance
all the time. If I then explicitly make them wait if the distance is too great, a long snake will only move
it’s segments in the front while turning. This is a problem. Avoiding obstacles will be difficult.

Any inputs as to how this can be achieved.

Based on what I’m seeing at this game site, I’m thinking the commands to the snake should be in a queue structure (FIFO). Initialize it so that, for starters, every command in the queue is the same (leads directly to the next snake link). When you get a new command, place it in the head of the queue to have it operate on the head of the snake and move all the commands down one position, discarding the last.

Also, have a fixed amount of time between when the commands (all together) are passed down the chain.

Anyway, that is my first guess–that is what I would try.

This would work, but it’s a difficult implementation. I will give it a go, though. Thanks for your thought.

Whilst i cant click the link (work blocks it ) so i assume that the snake segments to not change direction/move independantly, rather they inherit the direction from the parent segment.

Surely then you can do what I did back in the day with my snake game and just keep on creating segments at the head and destroying them at the tail for each movement and do not modify the middle segments at all.

It moves smoothly though. Imagine if beads on a string were dragged across a floor. Its close to that.

store the past positions from the head. sum the distances to get the position for the rest.

eg.
past position
(1,2),(1,3),(1,4),(1,5)
distance to folwing parts is 2
so you summ up :
(1,2)+(1,3)+(1,4)==>2 so the next position in 1.3 and so on.

Or
Bind the commands to positions on the map. If a snake segment mass that position the command is executed. Probably hard to kepp it deterministic.

That was extremely hard to follow. Could you rephrase, perhaps?

Pseudocode:


     class SnakeHead{
		 Vector2 direction;
		 Vector2 position;
		 double angelspeed;
		 ArrayList<Vector2> pastPositions;
	 }
	 
     class SnakePart{
		 Vector2 position;
		 double distanceToHead;
	 }
     
     class Snake{
    	 SnakeHead head;
    	 ArrayList<SnakePart> parts; 
	 }
     
	 void gameloop(long passedtime){
		snake.head.pastPositions.add(snakeHead.position)
		rotateSnakeDirection();
		moveSnakeHead();
		
		double distancetohead=0;
		for (int i=pastPositions.size-1;i>0;i--){
			nextdistancetohead=distancetohead +pastPositions.get(i).distance(pastPositions.get(i-1));
			
			for (SnakePart snakePart: snake.parts){
				if (nextdistancetohead< snakePart.distancetohead <nextdistancetohead){
					snakePart.position.setXY(pastPositions.get(i));
				}
			}
			distancetohead=nextdistancetohead;
		}
		
		if (pastPositions.size>10000){
			pastPositions.delete(0);
		}
	 }