# Explaining vector problem

I'm trying to simulate a centipede game, and I'm doing the snake movement. I have seen that piece of code, but can't get it, I know it gets the different between the current, previou's nodes positions, but what is the point of multiplying by the normal vector ?

for ( int i = 1 ; i < nodes.size(); i++ )
{

Vec2f diff =  nodes[i-1].m_Pos-nodes[i].m_Pos;

float length = diff.length();
Vec2f norm = diff.normalized();
nodes[i].m_Pos+=(length - 5)*norm;

}

• The normal just gets the direction of the difference. Then it just moves each piece by (length-5) in that direction. mathworld.wolfram.com/UnitVector.html – mobo Aug 25 '13 at 15:50
• is there a better way for doing that ? moving the snake body according to the head? the code actually is like a rope Why the normal should be multiplied? it should be added to the position? not to scale the different by the normal ? – Mahmoud Aug 25 '13 at 15:52
• nodes[i].m_Pos+=(length - 5)*norm; The normal is just the direction the next piece needs to move. The += means at that much to the position. So if head (piece 0) is at [20, 10], and next is at [10,10] (snake going right) then: diff is [10,0]; length is 10, and norm is [1,0]. It moves the piece (10-5)*[1,0] = [5,0] so, 5 pixels to the right. – mobo Aug 25 '13 at 16:01
• you can put it as an answer :) – Mahmoud Aug 25 '13 at 16:05
• I tried to let the first node move and at the screen edge, I give it a negative velocity downwards with x = 0. The problem that the following nodes doesn't move accordingly, I don't know why ? – Mahmoud Aug 25 '13 at 18:03

The fact that directly above the variable named norm is another variable named length and norm is declared as a vector rather than a scalar give enough context clues to indicate that norm is probably short-hand for normal, but this is poor naming.