# Snake Implementation

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

## Recommended Posts

Hey, I would like to implement a snake. And to control it with the keys. More or less it should look like you can see here:
But I dont know how to start. Espacially how to get the smooth movement when it moves to the left or right. Do I need a bonestructure to get this smoothness? I think Vertex Blending is necessary as well. Could you give me some hints which tools to use and which method is the most simple one to achieve a similar result. I would be very grateful.... Thanks

##### Share on other sites
From what I saw in the video, all the sections in the snake follow the path taken by the snake's head.

To start really simple I would code for the head movement first.

Then you can keep track of the path the head took as a sucession of lines.

Then you can add segments (initially as parallel 2D shapes) after the head that are center-aligned to this lines, are always at the same distance one from each other (from their center) and are transformed (or facing) in the direction of the line they are currently passing by.

Last, you can connect the vertices of each segment to form the snake surface.

This is the simplest way I can think of.

##### Share on other sites
Thank you very much.

I have now implemented a primtive head. I calculate the normal of the nose of the snake:

Vector snake::PlaneNormal (Vector a, Vector b, Vector c)
{
Vector res;
Vector ab = minus(b,a);
Vector ac = minus(c,a);
res = cruz_producto(ab, ac);
NormalizarVector(res);
return res;
}

void snake::move()
{
Vector a;
a=PlaneNormal(punto_0,punto_1,punto_2);
glRotatef(3,0,1,0);
}

in the main:

void KeyPressed(unsigned char key, int x, int y)
{
switch (key)
{
case 27: //ESC
PostQuitMessage(0);
break;
case 'o':
snake1.move();
break;
.....
}

With the function move() I want to achieve that the head will turn to the right.
But nothings happening.
I thought that I have to move the NormalVector of the noseplane in order to achieve the correct headmovement.
Because the Normalvector is showing Opengl which of the planes is the front.

Isnt that so?
Im not getting it.

##### Share on other sites
Take a look at this tutorial for some ideas.

##### Share on other sites
I`m sorry but its not really helping.

My Problem is not so much the movement itself, but how to make sure, that the nose of the snake is always the front.
For that I am calculating the normal, but i´m not sure how to integrate it into the moving code.
Do i need to move only the normal and the whole head will move?
But that seems false.

Thanks anyway

[Edited by - hercule_poirot on August 18, 2009 3:02:22 PM]