Advertisement Jump to content
Sign in to follow this  
Ahmed Egyptian

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.

If you intended to correct an error in the post then please contact us.

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);


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 this post

Link to post
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;

Your loop would have to adjust to i > 0 and outside your loop, you'd just move the head like you're doing.


- Eck


Share this post

Link to post
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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!