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;

  • \$\begingroup\$ 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 \$\endgroup\$
    – mobo
    Aug 25, 2013 at 15:50
  • \$\begingroup\$ 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 ? \$\endgroup\$
    – Mahmoud
    Aug 25, 2013 at 15:52
  • \$\begingroup\$ 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. \$\endgroup\$
    – mobo
    Aug 25, 2013 at 16:01
  • \$\begingroup\$ you can put it as an answer :) \$\endgroup\$
    – Mahmoud
    Aug 25, 2013 at 16:05
  • \$\begingroup\$ 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 ? \$\endgroup\$
    – Mahmoud
    Aug 25, 2013 at 18:03

1 Answer 1


In mathematics, a vector is said to be normal to a surface if it is perpendicular. In other words, normal vectors always point directly away from something.

Multiplying a scalar by a normal vector will get you a vector that represents displacement in the direction of the normal. Often normals have a handy property that they are unit-length, so if you want to move 5 units in the normal direction, nothing more than a multiplication is necessary. If it is not the case that the normal is unit-length, then an expensive normalization will be required.

Now, I am concerned by the naming in this code snippet. Norm actually has a different meaning from normal. In many contexts, the norm is actually the length, size or extent. A vector's norm, for instance, is a scalar that represents its length.

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.

  • \$\begingroup\$ Thanks, I have a problem right now. when the first node bounces and changes its velocity, the following nodes don't move at the way the first node moved.. the sample is here gamedev.net/… \$\endgroup\$
    – Mahmoud
    Aug 26, 2013 at 6:48
  • \$\begingroup\$ I would submit another question for this, there is not a lot I can do with an executable and no source code / problem description. \$\endgroup\$ Aug 26, 2013 at 19:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .