• Create Account

# setting the displacement between snake segments

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1AhmedCoeia  Members   -  Reputation: 209

Like
0Likes
Like

Posted 29 August 2013 - 03:22 PM

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?

each frame

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

}

nodes[0].m_Pos.x-=(nodes[0].m_Vel.x);
nodes[0].m_Pos.y-=(nodes[0].m_Vel.y);

### #2rijvgt  Members   -  Reputation: 119

Like
-1Likes
Like

Posted 29 August 2013 - 05:00 PM

Could you explain how exactly the body of the snake gets distorted?

In case the snake is constantly moving, in each loop the only node that gets updated properly is the nodes[1] the rest will get older values(each loop will update nodes[i] with nodes[i-1]'s previous value instead of nodes[i-1]) because you loop from the bottom of the snake instead of it's head. (If your snake would stopped moving, after nodes.size() amount of frames the snake would get back together)

No, that's the way it is supposed to be  but you're wasting the first node.size()-1 repeats of the loop/frame

In your code you have setted c.vy = 0; so when you use circles[0].yPosition-=circles[0].vy*(2*5); it doesnt really do anything cause this is always circles[0].yPosition-=0.

So you are currently only moving horrizontally.

Moreover, the following loop wouldn't work with the way you update the circles[0]'s position: (The circles wouldnt stay chainned)

for ( var i:int =4; i>0; i--)

{

circles[i].xPosition = circles[i-1].xPosition;

circles[i].yPosition = circles[i-1].yPosition;

}

// update circles[0] position

To fix the current way you handle the snake's head movement, try doing something like this: (Haven't tested it and it IS missing cases)

//update circles[0] position anyhow.

circles[0].xPosition-=circles[0].vx*(2*5);
circles[0].yPosition-=circles[0].vy*(2*5);

// Submit the changed position properly to the rest of the circles.

for (int i=1; i<5; i++)

{

if (circles[i].yPosition != circles[i-1].yPosition)

{

if (circles[i].xPosition != circles[i-1].xPosition)

circles[i].xPosition = circles[i-1].xPosition;

circles[i].yPosition-=( circles[i].vy*(2*5) - (the diameter of the circle)*i);

}

else {

circles[i].xPosition-=circles[i].vx*(2*5); //apply the same you used on circles[0]

}

}

Incase I'm wrong and that doesnt work , I would maybe advise you to control your FPS value(ie. lower it to like 1-5 in some cases), I think it would be easier to spot these kind of problems.

Edited by rijvgt, 30 August 2013 - 09:49 AM.

### #3King Mir  Members   -  Reputation: 1328

Like
0Likes
Like

Posted 29 August 2013 - 10:09 PM

Remember that the x and y pos is usually the position of the top left corner, and the position may also hold the hight and width.

Can't say more without the declaration of documentation of the position structure or class.

### #4AhmedCoeia  Members   -  Reputation: 209

Like
0Likes
Like

Posted 30 August 2013 - 01:46 AM

My question is about the offset between the body of the segements. How to set it ?

The snake's head should move, change its velocity, and the body segements should follow the direction of the head's move.

I'm trying to implement the same snake as here with the same movements http://www.flashbynight.com/nanopedes/

Edited by AhmedCoeia, 30 August 2013 - 02:12 AM.

### #5rijvgt  Members   -  Reputation: 119

Like
0Likes
Like

Posted 30 August 2013 - 06:56 AM

My question is about the offset between the body of the segements. How to set it ?

in each loop the only node that gets updated properly is the nodes[1] the rest will get older values(each loop will update nodes[i] with nodes[i-1]'s previous value instead of nodes[i-1]) because you loop from the bottom of the snake instead of it's head. (If your snake would stopped moving, after nodes.size() amount of frames the snake would get back together)

In case you want support with anything above your posted code you should also include it in your post.

Edited by rijvgt, 30 August 2013 - 06:58 AM.

### #6AhmedCoeia  Members   -  Reputation: 209

Like
0Likes
Like

Posted 30 August 2013 - 08:02 AM

I have done all things, but still the offset is not correctly set. This is the whole code:

 public var circles:Vector.<circle> = new Vector.<circle>(5);
public function t()
{

var offset:int = 10;
for ( var i:int = 0; i<5; i++)
{
var c:circle = new circle();

c.xPosition= 120+offset;
c.yPosition = 120;
c.vx = 1;
c.vy = 0;
circles[i] = c;
offset+=40;

}

}

public function gameLoop(event:Event):void
{
for ( var i:int = 0; i<5; i++)
{
circles[i].drawCircle(circles[i].xPosition, circles[i].yPosition);
}

for ( var i:int = 0; i<5; i++)
{
if ( i != 0 )
{
circles[i].xPosition = circles[i-1].xPosition;
circles[i].yPosition = circles[i-1].yPosition;
}
else {

}
}

circles[0].xPosition-=circles[0].vx*(2*5);
circles[0].yPosition-=circles[0].vy*(2*5);

}

### #7rijvgt  Members   -  Reputation: 119

Like
0Likes
Like

Posted 30 August 2013 - 09:46 AM

I have done all things, but still the offset is not correctly set. This is the whole code:

I have updated my post. Gl

Edited by rijvgt, 30 August 2013 - 09:47 AM.

### #8AhmedCoeia  Members   -  Reputation: 209

Like
0Likes
Like

Posted 30 August 2013 - 10:25 AM

with the edited code, the snake is completely distored

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS