# Smooth snake movement

This is language-agnostic question, more about model of my game.

I have a snake game with elements, but I move the elements smoothly, they don't just move 1 block each time, but instead they move some amount of pixels every frame.

I have an update loop that calculates the positions of the element, but I am stuck on correct calculations.

I have heading for each element:

```typedef NS_ENUM(int, kElementHeading)
{
};
```

I also have velocity (x, y) that determines in what direction snake is going. I have problem with snake movement, because my elements are in wrong positions. I managed to localize the thing for 2 elements, but my solution fails on more elements.

First solution I tried is to save point of rotation where the head changes direction. This worked, but due to different circumstances element can move different amount of pixels each turn. Often the element would skip the point. I tried increasing the zone where it should rotate, but it adds up error. I tried fixing this error, but element would still separate from snake (quite often).

On the second try I decided to keep the snake head in center of the screen and move the world around it. It worked good for 2 elements, as I just smoothly move the next element to desired position relatively to head. But this fails badly on more elements. If you make fast turns they start dancing and not following the path.

Third thing that I tried is leaving a path for other elements to follow. But that didn't work because I intend to keep my snake on center of the screen and technically it never moves to create a path.

I'm looking to replicate the movement pattern like in Nimble Quest (or any snake).

How should I implement snake elements moving to have no errors?

Here is my code for the first method, problem with it is that often the elements would fall off. The code is pretty self-explanatory. Rotation points are the places where to change direction.

```CFTimeInterval delta = self.lastTime - currentTime;

CGPoint currentPosition = self.playerSnake.head.sprite.position;
CGPoint velocity = self.playerSnake.velocity;

self.playerSnake.head.sprite.position = CGPointMake(currentPosition.x + velocity.x * delta * CONSTANTSPEEDFACTOR , currentPosition.y + velocity.y * delta * CONSTANTSPEEDFACTOR);

for (SnakeElement *element in self.playerSnake.elements) {

CGPoint currentPositionE = element.sprite.position;
CGPoint velocityE = element.velocity;

element.sprite.position = CGPointMake(currentPositionE.x + velocityE.x * delta * CONSTANTSPEEDFACTOR , currentPositionE.y + velocityE.y * delta * CONSTANTSPEEDFACTOR);
}

BOOL markToDelete = NO;
NSDictionary *deleteDictionary;

for (NSDictionary *dict in self.playerSnake.rotationPoints) {

CGPoint positionCoordinate = CGPointFromString(dict[@"position"]);
CGPoint velocityNew = CGPointFromString(dict[@"velocity"]);
double newAngle = [dict[@"angle"] doubleValue];

for (SnakeElement *element in self.playerSnake.elements) {

int xDifference = element.sprite.position.x - positionCoordinate.x;
int yDifference = element.sprite.position.y - positionCoordinate.y;

if ((xDifference > -2 && xDifference < 2) && (yDifference > -2 && yDifference < 2)   ) {

element.velocity = velocityNew;
element.sprite.position = CGPointMake(element.sprite.position.x + xDifference, element.sprite.position.y + yDifference);
SKAction *action = [SKAction rotateToAngle:newAngle duration:0.2 shortestUnitArc:YES];
[element.sprite runAction:action];
if ([element isEqual:[self.playerSnake.elements lastObject]]) {
markToDelete = YES;
deleteDictionary = dict;
}
}
}
}

[self.playerSnake.rotationPoints removeObject:deleteDictionary];
```

If I try increase the catch zone for the turning point, the elements tend to fall off more often then when it is 1 or 2 pixels wide. I'm not sure why this happens.

This is what I was suggesting you do in the comments in terms of handling your turning on points :

1.. calculate the distance that the element should move that frame based on speed and your elapsed time since last frame. (delta)

2.. calculate distance from element's current position to the turn point. This is the beforeDistance I spoke of in the comments

3.. calculate the distance the element should move towards the NEW target turning point AFTER the turn

afterDistance = distanceToMoveThisFrame - beforeDistance

4.. Calculate the new position for your element, starting at the current turning point towards the next target turning point of the element using afterDistance

If you follow this logic, you will NEVER overshoot or undershoot the turning point.

### How do i replace a key up keyCode with another in jQuery?

I currently have a table which only has a single editable column. I have a jQCuery change() event associated with the column's input controls to prevent any non numeric keys being pressed, other th...

### html color to alternative rows of dynamic table

I have a Dynamic table that I want to give color to on alternative rows. How can I achieve this with css? I need the code to work in IE7+