# Setting a displacement or offset between Snake segements

I have a problem in maths or I do not know. I have drawn few circles that simulates the body of a snake, the head of the snake moves and others should follow. but the is no offset or displacement between the circles, because the current code sets the previous position of the nodes to the current, when I add few numbers to both x, y axis, the body segments get distorted. How do I set the offsets between the segments?

for ( int i = 0 ; i < nodes.size(); i++ )
{
if( i == 0 )
{
// Do calculations for new position

//Set position and previous position
nodes[i].m_PrevPos.x = nodes[i].m_Pos.x;
nodes[i].m_PrevPos.y = nodes[i].m_Pos.y;

}
else
{
nodes[i].m_PrevPos.x = nodes[i-1].m_Pos.x;
nodes[i].m_PrevPos.y = nodes[i-1].m_Pos.y;
nodes[i].m_Pos.x = nodes[i-1].m_PrevPos.x;
nodes[i].m_Pos.y = nodes[i-1].m_PrevPos.y;
}
}


I would like also to check the head's node velocity for example:

 if ( nodes.m_Pos.x < 5 )
{

nodes.m_Vel.y = -2;
nodes.m_Vel.x = 0;

if(nodes.counter--<1)
{
nodes.m_Vel.y = 0;
nodes.m_Vel.x = -2;
nodes.counter = 5;
}

}


In fact you don't need to calculate offset for each part in each iterate.

just do it for first part. (How?)

for ( int i = nodes.size()-1 ; i >= 0; --i )
{
if( i != 0 )
{
// Here Do not think about offset. just follow old position of next snake part
nodes[i].m_Pos.x = nodes[i-1]m_Pos.x ;
nodes[i].m_Pos.y = nodes[i-1]m_Pos.y ;
}
else
{
// Do calculations for new position "with a proper offset".
nodes.m_Pos.x = .. ;// calculated position
nodes.m_Pos.y = .. ;// calculated position
}
}


Assume current head's position is (x,y). so new position simply can be calculate like this: and more specific : • Yea, I would like to calculate the offset, I do not want the circles to overlap on each others due to the copying of the previous positions into the current positions. Furthermore, I'm trying to play with the head's velocity up and down. Aug 27 '13 at 7:17
• You haven't mentioned this in question. ok Aug 27 '13 at 7:20
• Thanks, so when I change the velocity of the head, I must check if its downwards or upwards directional, and then recalculate the offsets ? Aug 27 '13 at 7:39
• Yes. It must be clear how to calculate next position according to x/y velocity of snake. you also can handle cross-direction ( for xVelocity=+1 and yVelocity=+1 ). If you need more clarity, I can explain more. but I think you got it. :) Aug 27 '13 at 7:48
• I need more clarity, If you can, I stayed much to get it :) Aug 27 '13 at 7:50