Question about usage of Direction Vector

Why are we adding the user input movement to a direction input instead of directly modifying mapPos before drawing the map to the screen?

example: // if (IsKeyDown(KEY_A)) mapPos.x += moveSpeed;

This is going to make more sense in later lectures but there are two main reasons why we don’t want to immediately apply movement and instead use a direction vector.

  1. We may want to do further processing on the direction vector, such as normalizing it. This can resolve unintended consequences of our movement code, such as moving diagonally being faster than moving in a straight direction

  2. We may want to do a check to prevent this movement, maybe we’re about to leave the area or about to hit an object. Then we can adjust the direction vector in advance.

These are just a couple of examples, but the reasons can vary based on the type of game you’re wanting to make.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms