Followers 0

# Snake body movement problem

## 4 posts in this topic

Hi All,

I'm trying to simulate snake body movement, the problem is the previous nodes of the snake doesn't follow exactly the head node.

This is my code to update the nodes

for ( int i = 1 ; i < nodes.size(); i++ )
{

Vec2f diff =  nodes[i-1].m_Pos -nodes[i].m_Pos;

float length = diff.length();
Vec2f norm = diff.normalized();

nodes[i].m_Pos+=(length - 8)*norm;

}if ( nodes[0].m_Pos.x < 5 )
{
nodes[0].m_Vel.y = -2;
nodes[0].m_Vel.x = 0;

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

}

nodes[0].m_Pos-=nodes[0].m_Vel;


Kindly find attached exe file which shows the problem.

0

##### Share on other sites

Hi,

I realize that this may make me sound like a jerk so let me clarify that that is absolutely not my intention.

If you want people to help you debug your code it is essential that you make it as easy for them to understand it as possible.

To that end, you should post a few screenshots depicting the problem you're having. No offense, but I'd rather not download and run any ol' exe file.

It would also be helpful if you explained the algorithm you're using to position the segments of the snake's body, as well as including a few comments in the code you posted.

- why do you subtract 8 from the length when positioning the node?

- what is the purpose of the counter datamember attached to node 0 ?

As I understand it, you're using the vector [previous segment -> current segment] to position the current segment? I may be wrong, but I think you'll want to go from head to tail in that scenario. That is, instead of

for(int x = 0; x < nodes.size(); ++x)

use

for(int x = nodes.size() - 1; x >= 0; --x)

Edited by molehill mountaineer
0

##### Share on other sites

Hi,

I'm really sorry for misunderstanding, English is not my first language. I have commented the code. The problem that the segements of the snake doesn't follow the directions of the first head node, when it bounces to the edges.

// loop thru the nodes

for ( int i = 1 ; i < nodes.size(); i++ )
{

// get the difference between the head and the previous nodes
Vec2f diff =  nodes[i-1].m_Pos -nodes[i].m_Pos;

float length = diff.length();
Vec2f norm = diff.normalized();

// get the normal vector, subtract 8 from the length, so that we have a displacement between the nodes
nodes[i].m_Pos+=(length - 8)*norm;

}
// check the screen left boundary
if ( nodes[0].m_Pos.x < 5 )
{
// reverse the velocities
nodes[0].m_Vel.y = -2;
nodes[0].m_Vel.x = 0;

// check how many time elapse to again reverse the velocities of the snake to right direction
if(nodes[0].counter--<1)
{
nodes[0].m_Vel.y = 0;
nodes[0].m_Vel.x = -2;
nodes[0].counter = 5;
}

}
// move the head to left
nodes[0].m_Pos-=nodes[0].m_Vel;

0

##### Share on other sites

Wouldn't it make more sense to only do movement for the head, and then through a series of previous positions update each node.

as such

for ( int i = 1 ; 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;
nodes[i].m_Pos.x = newX;
nodes[i].m_Pos.y = newX;
}
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;
}
}


However, I also saw you have a variable that seems to concern velocity and thus this wouldn't work. This is when you're obviously skipping around by say 32 pixels every step kind of like the old snake games, but with velocity your moving ever so slightly based on how long the gap between each time you run the code.

You could counter it by waiting for a complete step to occur IE if the head is moving at 8 pixels per second, you wait until the head has moved for that second and then update all the nodes to their correct spot, but your head would move effortlessly and the nodes would glitch along, updating theirselves once a second. In that case you could track the previous position up to say 30 steps behind and update the nodes 30 times in that second.

But then in the end... is this the better solution?

0

##### Share on other sites

Thanks for the reply,  I didn't get updating the head for 30times in a second and the others for 30 steps in a second, would you show me a pseduocode ?

In your code also, how would I set the offsets or the displacement between the nodes ?

Edited by AhmedCoeia
0

## Create an account

Register a new account