GDKB

Vector Magnitude

Beginner20 Minutes readUpdated July 21, 20263 related concepts

A vector's magnitude is its length — a single number describing how far the arrow the vector represents actually stretches, independent of which direction it points. The magnitude of `(3, 4)` is `5`, regardless of whether that vector represents a position offset, a velocity, or anything else. Magnitude turns a vector's direction-plus-distance into just the distance part, which is exactly what's needed for questions like "how far away is the target" or "how fast is the object moving."

On this page

Vector Magnitude

Summary

A vector's magnitude is its length — a single number describing how far the arrow the vector represents actually stretches, independent of which direction it points. The magnitude of (3, 4) is 5, regardless of whether that vector represents a position offset, a velocity, or anything else. Magnitude turns a vector's direction-plus-distance into just the distance part, which is exactly what's needed for questions like "how far away is the target" or "how fast is the object moving."

Why This Matters

A vector on its own tells you direction and length combined, but many gameplay questions only care about the length — "is the enemy within attack range," "how fast is the projectile currently moving." Without a way to extract just the length, you'd need to reason about the whole vector every time, even when direction is irrelevant to the question being asked. Magnitude isolates exactly the piece of information those questions need, and — as covered in [[CON-0045]] — is also the first step in extracting a vector's direction on its own.

Mental Model

Magnitude is a tape measure laid along the arrow, not the arrow itself. An arrow drawn from one point to another shows both a direction and a distance, but if you only care about "how far," you can lay a tape measure along its length and read off a single number, ignoring which way it's pointing entirely. That single number — the distance — is the magnitude, and it's exactly what you get by measuring the arrow's length without regard for its direction.

Core Explanation

  • Magnitude is computed with the Pythagorean theorem, extended to however many dimensions the vector has. For a 2D vector (x, y), the magnitude is sqrt(x*x + y*y); for 3D (x, y, z), it's `sqrt(xx + yy
    • z*z)` — the same "square each component, sum them, take the square root" pattern extends naturally to 3D.
  • Magnitude is always a non-negative number. A vector's length can never be negative — a magnitude of 0 means the vector has no length at all (it points nowhere, representing "no offset" or "no movement").
  • The square root is the expensive part of the calculation. Comparing two magnitudes (e.g. "is A farther than B?") can often be done by comparing their squared magnitudes instead, skipping the square root entirely — since the relative order of squared values matches the relative order of their square roots for non-negative numbers.
  • Magnitude and normalization are directly related. Dividing a vector by its own magnitude produces a vector of length exactly 1 pointing the same direction — the basis of normalization (see [[CON-0045]]).

Syntax / Theory

Engine-neutral (formula)

text
2D: magnitude(x, y)    = sqrt(x*x + y*y)
3D: magnitude(x, y, z) = sqrt(x*x + y*y + z*z)

Squared magnitude (no square root — faster comparisons):
    sqrMagnitude(x, y, z) = x*x + y*y + z*z

C# (Unity)

csharp
Vector3 velocity = new Vector3(3, 0, 4);
float speed = velocity.magnitude;        // 5.0 — computed with a square root internally.
float speedSquared = velocity.sqrMagnitude; // 25.0 — no square root, cheaper to compute.

C++ (Unreal)

cpp
FVector Velocity(3.f, 0.f, 4.f);
float Speed = Velocity.Size();       // 5.0f — equivalent to Unity's .magnitude.
float SpeedSquared = Velocity.SizeSquared(); // 25.0f — equivalent to .sqrMagnitude.

Both engines expose a squared-magnitude alternative specifically because avoiding the square root matters for performance-sensitive comparisons done many times per frame (e.g. checking every enemy's distance to the player).

Practical Example

An "in range" check compares magnitude without ever needing the actual distance value.

csharp
Vector3 offset = targetPosition - playerPosition;

if (offset.sqrMagnitude <= attackRange * attackRange) // Avoids a square root.
{
    Attack();
}

Squaring attackRange to match sqrMagnitude's squared units gives the identical comparison result as computing the real distance and comparing it to attackRange directly — just without the unnecessary square root computation.

Common Mistakes

  • Computing full magnitude (with the square root) when only a comparison is needed. Calling .magnitude inside a loop checking dozens of enemies' distances every frame does far more work than necessary — .sqrMagnitude compared against a squared threshold gives the same comparison result more cheaply.
  • Forgetting to square the comparison threshold when switching to squared magnitude. Comparing offset.sqrMagnitude <= attackRange (without squaring attackRange) compares a squared value against an unsquared one, producing an incorrect result.
  • Assuming magnitude can be negative or treating a negative result as meaningful. Magnitude is a length — it's mathematically always zero or positive; a calculation that appears to produce a negative "distance" indicates a bug elsewhere, not a valid magnitude.
  • Using magnitude when direction was actually what was needed. Magnitude discards direction entirely — a system that needs both (How far, and which way?) needs both magnitude and the normalized vector, not magnitude alone.

Game Development Example

An area-of-effect explosion computes damage falloff based on each target's distance from the blast center.

csharp
public class Explosion : MonoBehaviour
{
    public float radius = 10f;
    public int maxDamage = 100;

    void DamageTarget(Health target, Vector3 targetPosition)
    {
        float distance = (targetPosition - transform.position).magnitude;
        float falloff = Mathf.Clamp01(1f - (distance / radius));
        target.TakeDamage(Mathf.RoundToInt(maxDamage * falloff));
    }
}

Here the actual distance value (not just a squared comparison) is needed, because it feeds directly into the falloff calculation — a case where the real square root is worth computing.

Best Practices

  • Use squared magnitude for range/distance comparisons; use full magnitude only when the actual distance value itself is needed for a calculation (like falloff).
  • Remember to square any threshold value being compared against a squared magnitude.
  • Treat magnitude as always non-negative — a negative result during debugging signals a bug upstream, not a valid magnitude.
  • Cache a magnitude value if it's used more than once in the same frame, rather than recomputing it repeatedly.

Exercises

  1. Compute the magnitude of (6, 8) by hand using the Pythagorean formula, then verify it in code.
  2. Take the Explosion example and add an early-exit check using sqrMagnitude before computing the full magnitude-based falloff, to skip targets clearly outside the radius without the more expensive calculation.
  3. You're checking, every frame, whether any of 200 enemies is within detection range of the player. Decide whether to use .magnitude or .sqrMagnitude for this check, and justify the choice in terms of how many times per second the comparison runs.
  4. Research floating-point precision limitations and explain why comparing two sqrMagnitude values for exact equality (rather than a range or threshold) can be unreliable.
  • Vectors — magnitude is a property computed from a vector's components; understanding what a vector is comes first.
  • Vector Normalization — dividing a vector by its own magnitude is exactly how normalization works, tying the two concepts directly together.
  • Distance Calculation — the distance between two points is simply the magnitude of the vector obtained by subtracting one position from the other.

Further Reading

Related Concepts