4
\$\begingroup\$

Can someone explain the following code? I need to learn what each part means so I can turn it into enemy movement in a space shoot-em-up

Vec2d playerPos;
Vec2d direction; // always normalized
float velocity;

I get the above is naming two 2d Vector objects, and creating a variable called velocity. I'm not sure what the normalized comment is about, though.

update()
{
   direction = normalize(playerPos - enemyPos);
   playerPos = playerPos + direction * velocity;
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Have a look at this answer to a previous question. \$\endgroup\$ – brice Jun 23 '12 at 21:13
5
\$\begingroup\$

You should read this tutorial on vectors for game developers at Wolfire: Linear Algebra for Game Developers Part 1 / Part 2

A vector can be seen as pointing to a specific coordinate. It can also be seen as having a direction and a magnitude (or length).

A normalized vector is one that has a magnitude (or length) of exactly 1. Normalizing a vector produces a vector pointing in exactly the same direction, with a length of exactly 1.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.