Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;
}
share|improve this question
1  
Have a look at this answer to a previous question. –  brice Jun 23 '12 at 21:13

1 Answer 1

up vote 5 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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