primitive

Vector normalization and limiting

Reduce a vector to a unit direction by dividing by its magnitude, then rescale or clamp it to a working length.

Also called: setMag · unit vector · vector clamping

What it is

Two related moves on the same vector. One reduces it to direction only: “to normalize a vector, divide each component by the vector’s magnitude.” What’s left points the same way but has length one. The other keeps direction and instead bounds how long the vector is allowed to get, described as an operation “which puts a cap on the magnitude of a vector.” Chained together, this is: normalize to strip out direction, then either scale that direction back up to a chosen length, or clamp an already-existing vector down to a maximum instead.

How it works

  1. Get the vector’s magnitude. The source ties this to Pythagoras — “a Greek mathematician named Pythagoras discovered a lovely formula that describes the relationship between the sides and hypotenuse of a right triangle” — applying that relationship to a vector’s own components.
  2. Divide each component by that magnitude. That’s normalizing, in full: “to normalize a vector, divide each component by the vector’s magnitude.”
  3. To land on a specific length rather than length one, multiply the normalized vector by that length.
  4. To cap a vector that might already be too long, instead of setting it outright, compare its magnitude to a maximum and rescale down only if it exceeds that maximum. (That comparison is the ordinary way to satisfy “puts a cap on the magnitude of a vector” — it’s offered here as this page’s own reading of the mechanism, not a step laid out directly.)

Parameters & tuning

Two numbers do the actual work: the length you scale up to once a vector is normalized, and the maximum you cap against instead of setting an exact value. Neither comes with a fixed setting — what belongs in either slot depends on what the vector stands for in whatever sketch is using it.

Go deeper


Revision 2 · 1 source · Something wrong? Tell us.