Setting an offset between centipede nodes

Started by
1 comment, last by AhmedCoeia 10 years, 4 months ago

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;


}
}
Advertisement

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

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG

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.

This topic is closed to new replies.

Advertisement