Skip to main content
added mathjax formulas to supplement code, fixed minor typo, added link to chess, converted comparison table from code to mathjax (though I'm not sure if that's an improvement or if the table needs refinement)
Source Link
Pikalek
  • 13.4k
  • 5
  • 49
  • 54

\$d(v)=\sqrt{v_x^2 + v_y^2}\$

Or as code:

The code you've described uses the Manhattan distance metric:

\$d_T(v)= \vert v_x\vert\ + \vert v_y \vert \$

Or as code:

Euclidean                              Manhattan

sqrt(v.x*v.x + v.y * v.y)              abs(v.x) + abs(v.y)
sqrt(1 * 1 + 1 * 1)                    abs(1) + abs(1)
sqrt(2)                                1 + 1
1.414...                               2

$$\begin{array}{cc} \mathrm{Euclidean} & \text{ }& \text{vs. } & \mathrm{Manhattan} \\ \sqrt{v_x^2 + v_y^2} & & & \vert v_x\vert\ + \vert v_y \vert \\ \sqrt{1^2 + 1^2} & & & \vert 1\vert\ + \vert 1 \vert \\ \sqrt{2} & & & 1 + 1\\ 1.414\dotsc & & & 2\\ \end{array}$$

Our familiar Euclidean metric is the red circle. This is the set of all points x,y\$x,y\$ such that x^2 + y^2 = 1\$x^2 + y^2 = 1\$. You can see that it's rotationally-symmetric, and that's why we like it: it neatly represents the idea that distance doesn't change with direction.

Finally, I threw in the Chebyshev metric for fun - it's the green square:

\$d_{Chebshev}(v) = \text{max} (\vert v_x\vert, \vert v_y \vert)\$

Or as code:

It's also good for tile-based games, where you're allowed to move on diagonals. A Kingking in Chesschess moves according to the Chebyshev metric.

The code you've described uses the Manhattan distance metric:

Euclidean                              Manhattan

sqrt(v.x*v.x + v.y * v.y)              abs(v.x) + abs(v.y)
sqrt(1 * 1 + 1 * 1)                    abs(1) + abs(1)
sqrt(2)                                1 + 1
1.414...                               2

Our familiar Euclidean metric is the red circle. This is the set of all points x,y such that x^2 + y^2 = 1. You can see that it's rotationally-symmetric, and that's why we like it: it neatly represents the idea that distance doesn't change with direction.

Finally, I threw in the Chebyshev metric for fun - it's the green square:

It's also good for tile-based games, where you're allowed to move on diagonals. A King in Chess moves according to the Chebyshev metric.

\$d(v)=\sqrt{v_x^2 + v_y^2}\$

Or as code:

The code you've described uses the Manhattan distance metric:

\$d_T(v)= \vert v_x\vert\ + \vert v_y \vert \$

Or as code:

$$\begin{array}{cc} \mathrm{Euclidean} & \text{ }& \text{vs. } & \mathrm{Manhattan} \\ \sqrt{v_x^2 + v_y^2} & & & \vert v_x\vert\ + \vert v_y \vert \\ \sqrt{1^2 + 1^2} & & & \vert 1\vert\ + \vert 1 \vert \\ \sqrt{2} & & & 1 + 1\\ 1.414\dotsc & & & 2\\ \end{array}$$

Our familiar Euclidean metric is the red circle. This is the set of all points \$x,y\$ such that \$x^2 + y^2 = 1\$. You can see that it's rotationally-symmetric, and that's why we like it: it neatly represents the idea that distance doesn't change with direction.

Finally, I threw in the Chebyshev metric for fun - it's the green square:

\$d_{Chebshev}(v) = \text{max} (\vert v_x\vert, \vert v_y \vert)\$

Or as code:

It's also good for tile-based games, where you're allowed to move on diagonals. A king in chess moves according to the Chebyshev metric.

replaced http://gamedev.stackexchange.com/ with https://gamedev.stackexchange.com/
Source Link

Normally in 3D games we model the world as a Euclidean space, and we use a Euclidean distance metric (also known as Pythagorean Theoremalso known as Pythagorean Theorem) to calculate the total length of a vector v with components v.x and v.y. Namely:

Normally in 3D games we model the world as a Euclidean space, and we use a Euclidean distance metric (also known as Pythagorean Theorem) to calculate the total length of a vector v with components v.x and v.y. Namely:

Normally in 3D games we model the world as a Euclidean space, and we use a Euclidean distance metric (also known as Pythagorean Theorem) to calculate the total length of a vector v with components v.x and v.y. Namely:

Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Your Pythagoras-free code doesn't compute a length as we normally think of it.

Normally in 3D games we model the world as a Euclidean space, and we use a Euclidean distance metric (also known as Pythagorean Theorem) to calculate the total length of a vector v with components v.x and v.y. Namely:

EuclideanLength(v) = sqrt(v.x * v.x + v.y * v.y)

(Note that this square root is missing in your sample code above, which is why the two approaches appear to give the same answer. More on that shortly...)

The code you've described uses the Manhattan distance metric:

ManhattanLength(v) = abs(v.x) + abs(v.y)

(Although you didn't include the absolute values, which may make it behave unexpectedly for negative numbers)

It's easy to see that these two distance functions match-up when v.x or v.y is zero, and we're only moving along one axis. How do they compare though when we move diagonally?

Let's say v.x = v.y = 1. How long is this vector (equivalently, how fast is the velocity it describes)?

Euclidean                              Manhattan

sqrt(v.x*v.x + v.y * v.y)              abs(v.x) + abs(v.y)
sqrt(1 * 1 + 1 * 1)                    abs(1) + abs(1)
sqrt(2)                                1 + 1
1.414...                               2

You can see these metrics don't actually agree for diagonal lines.

Let's plot on a graph the set of points that each metric says are a distance of 1 away from the origin:

Distance metrics

Our familiar Euclidean metric is the red circle. This is the set of all points x,y such that x^2 + y^2 = 1. You can see that it's rotationally-symmetric, and that's why we like it: it neatly represents the idea that distance doesn't change with direction.

The Manhattan metric is the blue diamond. Not a great match for our intuitive idea of distance - but that doesn't make it bad. In many tile-based games where you move in discrete steps in the four cardinal directions, the Manhattan metric gives the correct distance between points (in terms of "how many moves will it take to get there?")

Finally, I threw in the Chebyshev metric for fun - it's the green square:

ChebyshevLength(v) = max(abs(v.x), abs(v.y))

It's also good for tile-based games, where you're allowed to move on diagonals. A King in Chess moves according to the Chebyshev metric.

I hope that clears up what the difference is between typical Pythagorean-style code and the example you provided above.