# Setting an offset between centipede nodes

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

## Recommended Posts

Hi All,

I'm trying to set an offset for a snake or centipede nodes. I don't know the math behind it.  The centipede nodes follow the head, hence the position is copied.

I tried the following idea, but it didnt work

for (var i:int = m_nodes.length - 1; i >= 0; --i)
{
if (i != 0)
{
/*
m_nodes[i].x = m_nodes[i - 1].x;
m_nodes[i].y = m_nodes[i - 1].y;*/
var currAngle:int = m_nodes[i].rotation + m_nodes[i-1].rotation;

trace("curr Angle"+currAngle);
m_nodes[i].x = m_nodes[i - 1].x + Math.cos(currAngle);
m_nodes[i].y = m_nodes[i - 1].y + Math.sin(currAngle);

}

else
{
m_nodes[0].x += m_nodes[0].vx * (100) * dt;
m_nodes[0].y += m_nodes[0].vy * (100) * dt;

}
}
Edited by Ahmed Egyptian

##### Share on other sites

The centipede in the old game was grid based so each node (segment) was simply assigned the values of the node in front of it. I'm not sure what you're trying to do with you're currAngle logic there.

Is node.rotation dependent or independent of the node in front of it? For example if you had 3 nodes facing left.

head.rotation == 180; // (pi/2)
neck.rotation == ?; // Either 180? or 0? gut reaction I'd say 180.


If you went the independent route, you'd loop through each node from the tail like you're doing, and simply assign the previous (closer to the head) node's values to the current node's values.

// have the tail follow the nodes in front of it.
node[i].x = node[i - 1].x;
node[i].y = node[i - 1].y;
node[i].rotation = node[i-1].rotation;


- Eck

##### Share on other sites

With the curreAngle, I just wanted to set a space or an offset between the nodes so that they are not overlapped when you just copy the positions.

Edited by AhmedCoeia

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634047
• Total Posts
3015231
×