   snake
  I'm trying to replicate the snake, but got confused on how to make each partof the snake body follow the next one. Any ideas?   
  I would just reposition the last piece of the snake (tail) with the next position of the snakes head which would depend on the direction of the snake. Either that, or you could loop through each piece and set its position to that of the piece infront.   ;Quote:I'm trying to replicate the snake, but got confused on how to make each partof the snake body follow the next one. Any ideas?Search the forum archives for 'snake game' and I'm sure you'll find some ideas (this topic has come up at least a few times before).A typical solution though would be to use a queue: each update, you push the new position of the head of the snake onto the cue, and pop the entry corresponding to the tail off of the cue (or not, if the snake is growing).Many languages include 'queue' containers as part of their standard library, so I recommend looking there first. (If you're programming in C++, check out std::queue; in C#, it would be System.Collections.Queue.)   ;Quote:Original post by jyk...in C#, it would be System.Collections.Queue.)Though that would certainly work, you'd probably be better off with System.Collections.Generic.Queue<T>.   ;Quote:Original post by benryvesQuote:Original post by jyk...in C#, it would be System.Collections.Queue.)Though that would certainly work, you'd probably be better off with System.Collections.Generic.Queue<T>.Oops, right (haven't used C# in a while).   ;  I was think of doing something like this :vector<int> vecSnake;bool draw = true;SNAKE snake; //classGLvoid draw() {    if(draw){glTranslatef(random,random,0.0f);drawObject(vecSnake); // Use glutSoliSphere to draw the number of tail according to vecSnake size;    }    if(right) snake.X += 0.01f;    if(left) snake.X -= 0.01f;    if(up) snake.Y += 0.01f;    if(down) snake.Y -= 0.01f;if( checkCollision.SnakeHitItem())   { vecSnake.resize(vecSanke.size()+1) draw = false;   }}I am just confused on how to make the tail follow the next ball.I think this is where OOP comes in. I need to give each tail its ownvariables.[Edited by - tnutty on April 22, 2009 5:56:45 PM];  Loop through each part, and adjust it so that it's as close to the ball in front of it as possible. That is, just take it's current position, and move it towards the one in front of it until it hits it.vecSnake[0].position += movement;for(size_t i=1;i<vecSnake.size();i++) { direction = vecSnake.position - vecSnake[i-1].position; normalize(direction);  vecSnake.position = vecSnake[i-1] + ballDiameter * direction;}And that's it. Just make sure when you place a new ball, don't put it exactly on the same position as the last one, or the normalize will get a divide by zero.   ;  Is the snake's motion discrete (i.e. grid-based), like in the 'classic' snake game? Or is it continuous? (Your most recent post suggests the latter...)   ;  Well for now its continuous, because I wan't to make it a little different. Maybe later on, I will try the grid Base. Or maybe I should start off withgrid base, it being easier ( I think ) .   ;Quote:Original post by tnuttyI am just confused on how to make the tail follow the next ball.I think this is where OOP comes in. I need to give each tail its ownvariables.Yes. Then you store the tails in the vector. (I have no idea what you were hoping to accomplish with a vector of ints; since the number of tails is a single int no matter how many tails there are :) ) Each time through, you iterate over the vector, and change each piece's position to move towards where the piece in front of it was. (Hint: this is easier if you iterate in reverse, so that the piece in front is still where it "was" when you update each piece. ;) )   