Skip to main content
gamedev.net
🔒 Locked

Snake Implementation

Started by hercule_poirot 3 replies 850+ views
Original Post
hercule_poirot
hercule_poirot
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
owl
owl
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.
[size="2"]I like the Walrus best.
hercule_poirot
hercule_poirot
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?
I
m not getting it.

Thanks for your help....
owl
owl
Take a look at this tutorial for some ideas.
[size="2"]I like the Walrus best.
hercule_poirot
hercule_poirot
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]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.