+91 9591117915 info@pixtell.net

## Quick Distance comparison

Pixtell Labs gamedev, Tutorials Quick Distance comparison

# Quick Distance comparison

Posted By Kirti P

Quick Distance comparison:

In this tutorial, we will learn how to quickly find out the shortest distance comparison. We add a new character to the previous example Blinky (B). The goal is to find out If Inky(I) is closer to Pacman(P) or Blinky(B).

Using the vector length calculation, we are going to compare the length of the vector $\vec IP$ with the length of vector $\vec BP$

Applying Pythagoras theorem to calculate the length, we have

$\mid\vec BP\mid=\sqrt{\vec BPx^2 +\vec BPy^2}$   (compare$\mid\vec IP\mid=\sqrt{\vec IPx^2 +\vec IPy^2}$

where Compare means we are trying to find out if |BP| is bigger or |IP| is bigger.

Eliminating the square root on both sides by squaring, we have:

$\vec BPx^2 +\vec BPy^2$   (compare)  $\vec IPx^2 +\vec IPy^2$

Rewriting these in the original form:

$\mid\vec BP^2\mid$  (compare)   $\mid\vec IP^2\mid$

This factor is faster computationally to compare the lengths, rather than the vectors themselves.

Tagged