https://um.mendelu.cz/...
in URL, click View on GitHub
to open this file on github.com.e
on GitHub). However, an advanced VS Code editor (press .
on GitHub) is better, since it provides preview how the Markdown code renders. Alternatively press pencil for simple editor or press triangle next to the pencil to get access to VS Code described as github.dev
.Ctrl+V
and K
. Keep the preview open as you work, or close using a mouse.Source control
(describe your changes, such as “Polish translation started”) and then press Commit&Push.is_finished: False
in header to is_finished: True
.https://um.mendelu.cz/...
, otevřete jej na serveru github.com.e
na GitHubu). Lepší je však pokročilý editor VS Code (stikněte .
na GitHubu), protože poskytuje náhled, jak se kód Markdown interpretuje. Případně stiskněte tužku pro jednoduchý editor nebo stiskněte trojúhelníček vedle tužky, abyste získali přístup k editoru VS Code popsaný jako github.dev
.Ctrl+V
. a K
. Během práce nechte náhled otevřený nebo jej zavřete pomocí myši.Zpráva
(popište své změny, např. “Zahájen překlad do polštiny”) a poté stiskněte tlačítko Commit&Push.is_finished: False
v záhlaví na is_finished: True
.Vektory jsou důležité nejen v matematice ale i fyzice nebo informatice. V matematice se jimi zabývá část s názvem lineární algebra.
Vektor je většinou definován jako prvek matematické abstraktní struktury, tzv. vektorového prostoru. Typickým zástupcem takovéhoto prostoru jsou třeba všechny uspořádné \(n\)-tice reálných čísel (tj. například dvojice nebo trojice) společně s pravidly pro jejich sčítání a násobení nějakým číslem. Na střední škole je tento vektor typicky prezentován jako množina orientovaných úseček, které mají daný směr a stejnou velikost.
Ve fyzice se pomocí vektorů popisují veličiny jako rychlost a zrychlení pohybujícího se objektu, síly na něj působící, či elektromagnetické pole. V informatice je vektorem často myšlen jen uspořádaný seznam položek (nemusí jít jen o čísla). Vektory jsou efektivní možností, jak organizovat a ukládat objekty například v aplikacích strojového učení.
V informatice ale existuje i oblast, kde se vektory používají tím způsobem, jakým jsou definovány ve středoškolské matematice či fyzice. Touto oblastí je prostředí počítačových her. Zvládnutí práce s vektory je dokonce jedním ze základních stavebních kamenů k tomu, aby člověk mohl být programátorem her.
V závislosti na tom, zda vytváříte 2D nebo 3D hru, mají vektory dva nebo tři souřadnice a obecně se používají k reprezentaci geometrických vlastností objektů v herním světě. Pro jednoduchost budeme pracovat pouze v dvourozměrném prostoru tedy v rovině a to v kartézské souřadné soustavě.
Poznámka: Takových základních stavebních kamenů je třeba samozřejmě mnohem více. Kromě příslušného programovacího nástroje je třeba také znát matice transformací, jako je posunutí, otočení atd. V následujících úlohách se chceme zaměřit jen na operace s vektory.
V následujících příkladech budeme rozlišovat zadání bodů (v hranatých závorkách) a vektorů (v kulatých závorkách). Současně ale budeme pamatovat na to, že bod \(A = [a_1;a_2]\) také můžeme chápat jako koncový bod vektoru \(\overrightarrow{a} = (a_1;a_2)\), jehož počáteční bod je počátek souřadného systému.
Bod má souřadnice, ale na rozdíl od vektoru není určen délkou a směrem. Bod \([0,0]\) nebo vektor \((0, 0)\) pro nás bude středem herního světa.
Běžným případem použití vektorů je výpočet vektoru, který popisuje vztah jednoho objektu vůči druhému. Vezměme si jednoduchý příklad bodů \(A = [a_1;a_2]\) a \(B = [b_1;b_2]\). Vektor \(\overrightarrow{u}=\overrightarrow{AB}= (b_1-a_1; b_2-a_2)\) obvykle nazýváme směrový vektor. Pokud budou body \(A\) a \(B\) ve hře reprezentovat postavy, pak vektor \(\overrightarrow{AB}\) určuje směr a jeho velikost pak vzdálenost, kterou postava \(A\) musí ujít, aby se dostala k postavě \(B\).
Úloha 1. Ve 2D hře máme programátora ovládajícího postavu \(A\) a hráče ovládajícího postavu \(B\). Postavy stojí na začátku na různých místech a jsou reprezentovány pro jednoduchost body \(A\), \(B\). Postava \(B\) postupně projde trasu ve směru vektorů \(\overrightarrow{u}\), \(\overrightarrow{v}\) a \(\overrightarrow{w}\). Vyjádřete vektor, který programátor potřebuje určit pro výstřel postavy \(A\) na postavu \(B\).
Řešení. Je jasné, že postava \(B\) celkem prošla trasu \(\overrightarrow{u}+\overrightarrow{v}+\overrightarrow{w}\). Postavy \(A\) a \(B\) ale na začátku stály na různých místech. Zbývá nám tedy určit vektor s počátečním bodem v bodě \(A\) a koncovým bodem v bodě \(B\), tedy směrový vektor \(\overrightarrow{AB}\). Víme že bod \(A\) můžeme brát jako koncový bod vektoru \(\overrightarrow{a}\) a bod \(B\) jako koncový bod vektoru \(\overrightarrow{b}\). Pak tedy vektor \(\overrightarrow{AB} = \overrightarrow{b}-\overrightarrow{a}\) (protože bod a vektor mají stejné souřadnice, používá se často i zápis \(\overrightarrow{AB} = B-A\)). Vektor určující výstřel postavy \(A\) na postavu \(B\) je tedy \(\overrightarrow{b}-\overrightarrow{a}+\overrightarrow{u}+\overrightarrow{v}+\overrightarrow{w}\).
U každé postavy v herním světě se používá i vektor ve smyslu seznamu položek. Jednou z položek je třeba jméno postavy, její úloha, poloha. Další vlastností každé postavy v herním světě je směr, kterým je tato postava natočena. K určení směru natočení se používá takzvaný normalizovaný směrový vektor, tedy směrový vektor délky \(1\).
Normalizované směrové vektory se používají i k uchovávání informace, jakým směrem se vyskytují ostatní postavy nebo objekty.
Poznámka: Důvod, proč jsou v herním světě používány normalizované verze směrových vektorů, vysvětlíme později.
Úloha 2. Mějme postavy \(A = [-5;2]\), \(B = [1;-2]\), \(C = [4;-1]\). Určete normalizované směrové vektory postav \(A\) a \(B\) směrem k ostatním postavám. Nakreslete odpovídající obrázek.
Řešení. Pro směrový vektor \(\overrightarrow{AB}\) platí \(\overrightarrow{AB}= (1-(-5); -2-2) = (6;-4)\). Pokud ho chceme normalizovat, stačí ho vydělit jeho délkou \(\left | \overrightarrow{AB} \right | = \sqrt{6^2+(-2)^2} = \sqrt{52}.\) Normalizovaný vektor k vektoru \(\overrightarrow{AB}\) značíme \(\widehat{AB}\) a platí
\[ \widehat{AB} = \frac{\overrightarrow{AB}}{\left | \overrightarrow{AB} \right | } = \frac{(6;-4)}{\sqrt{52}} = \left(\frac{3}{\sqrt{13}};-\frac{2}{\sqrt{13}}\right). \]
Obdobně
\[ \widehat{AC} = \frac{\overrightarrow{AC}}{\left | \overrightarrow{AC} \right | } = \frac{(9;-3)}{\sqrt{90}} = \left(\frac{3}{\sqrt{10}};-\frac{1}{\sqrt{10}}\right), \]
\[\widehat{BC} = \frac{\overrightarrow{BC}}{\left | \overrightarrow{BC} \right | } = \frac{(3;1)}{\sqrt{10}} = \left(\frac{3}{\sqrt{10}};\frac{1}{\sqrt{10}}\right),\]
\[\widehat{BA} = \frac{\overrightarrow{BA}}{\left | \overrightarrow{BA} \right | } = \frac{(-6;4)}{\sqrt{52}} = \left(-\frac{3}{\sqrt{13}};\frac{2}{\sqrt{13}}\right).\]
Vektor \(\widehat{BA}\) jsme nemuseli počítat, protože má stejnou velikost jako vektor \(\widehat{AB}\) a opačný směr. Souřadnice takových vektorů se liší jen v opačném znaménku.
Úloha 3. Mějme polohu postavy \(A = [a_1;a_2]\) a postavy \(B = [b_1;b_2]\) stojících na různých místech. Určete
normalizovaný směrový vektor \(\widehat{BA}\),
kde se bude postava \(B\) nacházet poté, co ujde tři jednotkové délky směrem k postavě \(A\)?
Řešení. a) To, co jsme počítali v předchozím příkladu s konkrétními souřadnicemi, nyní zapíšeme obecně. Tedy
\[\widehat{BA} = \frac{\overrightarrow{BA}}{\left| \overrightarrow{BA} \right| } = \frac{(a_1-b_1;a_2-b_2)}{\sqrt{(a_1-b_1)^2+(a_2-b_2)^2}}.\]
\[B+3\cdot\widehat{BA} = [b_1;b_2] +3\frac{(a_1-b_1;a_2-b_2)}{\sqrt{(a_1-b_1)^2+(a_2-b_2)^2}}.\]
Výsledkem skalárního součinu dvou vektorů je skalár, tedy reálné číslo. V programování her má důležité místo skalární součin normalizovaných vektorů.
Úloha 4. Určete skalární součiny normalizovaných směrových vektorů z řešení Úlohy 2.
Řešení.
\[ \widehat{AB} \cdot \widehat{AC} = \left(\frac{3}{\sqrt{13}};-\frac{2}{\sqrt{13}}\right) \cdot \left(\frac{3}{\sqrt{10}};-\frac{1}{\sqrt{10}}\right) = \frac{9}{\sqrt{130}}+\frac{2}{\sqrt{130}} = \frac{11}{\sqrt{130}} \dot=0{,}96 \]
\[ \widehat{BA} \cdot \widehat{BC} = \left(-\frac{3}{\sqrt{13}};\frac{2}{\sqrt{13}}\right) \cdot \left(\frac{3}{\sqrt{10}};\frac{1}{\sqrt{10}}\right) = -\frac{9}{\sqrt{130}}+\frac{2}{\sqrt{130}} = -\frac{7}{\sqrt{130}} \dot= -0{,}054 \]
Skalární součin dvou normalizovaných vektorů je velmi užitečný, protože určuje, do jaké míry dva vektory směřují stejným nebo podobným směrem! Skalární součin může totiž v tomto případě nabývat hodnot v rozmezí \(-1\) až \(1\), přičemž \(1\) znamená, že oba vektory směřují přesně stejným směrem, a \(-1\), že směřují opačným směrem, zatímco hodnota blízká \(0\) znamená, že svírají úhel blízký pravému úhlu. Důvodem rozsahu hodnot \(-1\) až \(1\) je to, že se pohybujeme v rozmezí funkčních hodnot funkce kosinus. Pro skalární součin dvou vektorů \(p\) \(q\), totiž také platí vztah
\[ \vec{p} \cdot \vec{q}=\left | \vec{p} \right |\left | \vec{q} \right |\cos\alpha, \]
kde \(\alpha\) je úhel který vektory \(p\) a \(q\) svírají.
Úloha 5. Pozorovatel v počátku se dívá na objekt \(A=[3;1]\), určete úhel \(\alpha\) o jaký se musí otočit, aby směr jeho pohledu mířil přímo na objekt \(B=[1;2]\).
Řešení. Body \(A\) a \(B\) budeme opět brát jako koncové body vektorů \(a=(3;1)\) a \(b=(1;2)\). Ze vztahu, který platí pro skalární součin dvou vektorů, vyjádříme \(\cos\alpha\):
\[ \cos\alpha =\frac{\vec{a} \cdot \vec{b}}{\left | \vec{a} \right |\left | \vec{b} \right |}\,. \]
Po dosazení získáváme
\[ \cos\alpha = \frac{(3;1) \cdot (1;2)}{\sqrt{3^2+1^2} \cdot \sqrt{1^2+2^2}} = \frac{3 \cdot 1 + 1 \cdot 2}{\sqrt{10} \cdot \sqrt{5}} = \frac{5}{\sqrt{50}} =\sqrt{\frac{25}{50}} = \frac{1}{\sqrt{2}}.\]
Víme, že \(\frac{1}{\sqrt{2}}=\frac{\sqrt{2}}{2}\) je základní hodnota goniometrické funkce, tedy \(\alpha=45^{\circ}\), resp. hodnotu úhlu \(\alpha\) vypočítáme jako \(\arccos \frac{1}{\sqrt{2}}\). Pozorovatel se tedy musí otočit o úhel \(45^{\circ}\).
Pokud by v zadání úlohy byly dané normalizované směrové vektory, jejich skalární součin by byl roven přímo \(\cos\alpha\).
\[ \cos\alpha =\frac{\vec{a}}{\left | \vec{a} \right |} \cdot \frac{\vec{b}}{\left | \vec{b} \right |} = \hat{a} \cdot \hat{b}\]
Toto je důvod proč bývají směry pohledů u postav a směrové vektory mezi postavami v seznamech položek uváděny v normalizovaném tvaru.
Skalární součin můžeme s výhodou použít i pro řešení následujícího problému. Řekněme, že vytvářím hru, ve které se hráč snaží schovat před nějakými strážemi. Bude nás tedy zajímat, zda strážný vidí či nevidí jednotlivé hráče.
Pro větši realističnost chceme, aby měl strážný zorné pole, ve kterém danou postavu vidí. U člověka se udává velikost zorného úhlu pro vidění oběma očima přibližně \(180^{\circ}\). To by pro našeho strážného bylo až příliš, takže řekněme že chceme aby jeho zorný úhel byl například \(170^{\circ}\).
Úloha 6. Zorný úhel strážného \(G\) je \(170^{\circ}\), jakých hodnot budou nabývat skalární součiny mezi jeho směrem pohledu \(\vec{d}\) a normalizovanými směrovými vektory k objektům, které strážný vidí?
Řešení. Od směru pohledu strážce k hranicím zorného pole (směrem doprava i doleva) máme \(85^{\circ}\). Stačí tedy vypočítat \(\cos 85^{\circ} \dot= 0,087\). Skalární součiny mezi směrem pohledu strážného a normalizovanými směrovými vektory k objektům, které vidí budou nabývat hodnot mezi \(0,087\) a \(1\).
Do seznamu položek příslušnému strážci tedy ke směru pohledu přidáme rozmezí, které bude určovat jeho zorné pole. Pomocí něj pak můžeme kontrolovat, zda strážný hráče vidí či nevidí. Pro jednoduchost předchozí výsledek zaokrouhlíme na jedno desetinné místo a zorné pole strážného tedy omezíme hodnotou \(0,1\).
Úloha 7. Určete zda strážný, umístěný v počátku, vidí hráče \(A=[3;-2]\), jestliže směr pohledu strážného je \(\left(\frac{1}{\sqrt{5}};\frac{2}{\sqrt{5}}\right)\) a hranice pro omezení zorného pole je dána hodnotou \(0,1\).
Řešení. Směr pohledu strážného je už normalizovaný vektor. Stačí tedy normalizovat směrový vektor od strážného k hráči \(A\). Díky tomu že strážný je v počátku, stačí normalizovat vektor \(\vec{a}=(3;-2)\). Platí
\[\hat{a}=\frac{\left(3;-2\right)}{3^2+(-2)^2} = \frac{\left(3;-2\right)}{15} = \left(\frac{3}{15};\frac{-2}{15}\right).\]
Poté už můžeme vypočítat příslušný skalární součin těchto normalizovaných vektorů, tedy
\[\left(\frac{1}{\sqrt{5}};\frac{2}{\sqrt{5}}\right) \cdot \left(\frac{3}{\sqrt{15}};\frac{-2}{\sqrt{15}}\right) = \frac{3}{\sqrt{75}} - \frac{4}{\sqrt{75}} = -\frac{1}{\sqrt{75}} \dot= -0,12.\]
Výsledek není v rozmezí od \(0,1\) do \(1\), strážný tedy hráče \(A\) nevidí. Z výsledku je navíc jasné že mezi vektory je větší než pravý úhel.
Můžete se zamyslete v čem se situace z předchozího příkladu změní, pokud strážce není v počátku.
Vectors are important not only in mathematics, but also in physics and computer science. In mathematics, they are studied within a branch called linear algebra.
A vector is usually defined as an element of an abstract mathematical structure called a vector space. A typical example of such a space is the set of all ordered n-tuples of real numbers (for instance, pairs or triples), together with the rules for adding them and multiplying them by a number. In high school mathematics, vectors are often introduced as a set of directed line segments that have the same magnitude and direction.
In physics, vectors are used to describe quantities such as the velocity and acceleration of a moving object, the forces acting on it, or electromagnetic fields. In computer science, a vector is often simply understood as an ordered list of items (not necessarily numbers). Vectors provide an efficient way of organizing and storing data, for example in machine learning applications.
However, there is also an area in computer science where vectors are used in the same way as they are defined in high school mathematics or physics. This area is the field of computer games. In fact, mastering vector operations is one of the fundamental building blocks for becoming a game programmer.
Depending on whether you are creating a 2D or 3D game, vectors have two or three coordinates and are generally used to represent geometric properties of objects within the game world. For simplicity, we will work only in two dimensions, that is, in a plane using the Cartesian coordinate system.
Note: Of course, understading vectors is just one of many fundamental building blocks. In addition to the relevant programming tools, one also needs to understand transformation matrices, such as translations, rotations, and so on. In the following tasks, we will focus only on vector operations.
In the following examples, we will distinguish between points (written in square brackets) and vectors (written in round brackets). At the same time, we should keep in mind that a point \(A = [a_1; a_2]\) can also be interpreted as the endpoint of the vector \(\overrightarrow{a} = (a_1; a_2)\), whose initial point is the origin of the coordinate system.
A point has coordinates, but unlike a vector, it is not defined by magnitude and direction. The point \([0, 0]\) (or the vector \((0, 0)\)) will serve as the center of the game world.
A common use of vectors is to find the vector that describes the relationship between two objects. Let us consider a simple example with two points \(A = [a_1; a_2]\) and \(B = [b_1; b_2]\). The vector \(\overrightarrow{u}=\overrightarrow{AB}= (b_1 - a_1; b_2 - a_2)\) is usually called the direction vector. If points \(A\) and \(B\) represent characters in a game, then the vector \(\overrightarrow{AB}\) indicates the direction from one character to the other, and its magnitude represents the distance character \(A\) would have to travel to reach character \(B\).
Exercise 1. In a 2D game, a programmer controls character \(A\) and a player controls character \(B\). The characters start at different locations, represented by points \(A\) and \(B\). Character \(B\) moves along a path made up of the vectors \(\overrightarrow{u}\), \(\overrightarrow{v}\), and \(\overrightarrow{w}\). Express the vector the programmer needs to determine in order for character \(A\) to shoot at character \(B\).
Solution. It is clear that character \(B\) has moved along the path \(\overrightarrow{u}+\overrightarrow{v}+\overrightarrow{w}\). However, characters \(A\) and \(B\) started at different positions. So, we still need to determine the vector that starts at point \(A\) and ends at point \(B\) — that is, the direction vector \(\overrightarrow{AB}\). We know that point \(A\) can be understood as the endpoint of the vector \(\overrightarrow{a}\), and point \(B\) as the endpoint of the vector \(\overrightarrow{b}\). Then the vector \(\overrightarrow{AB} = \overrightarrow{b}-\overrightarrow{a}\) (since points and corresponding vectors share the same coordinates, the notation \(\overrightarrow{AB} = B-A\) is also commonly used). Therefore, the vector needed for character \(A\) to shoot at character \(B\) is \(\overrightarrow{b}-\overrightarrow{a}+\overrightarrow{u}+\overrightarrow{v}+\overrightarrow{w}\).
Each character in the game world is also associated with a vector in the sense of a list of items — such as the character’s name, role, and position. Another important property of every character is the direction they are facing. This direction is represented by a so-called normalized direction vector, which is a direction vector with a magnitude of \(1\).
Normalized direction vectors are also used to store information about the directions in which other characters or objects are located.
Note: The reason why normalized versions of direction vectors are used in game worlds will be explained later.
Exercise 2. Let the characters be given by the points \(A = [-5; 2]\), \(B = [1; -2]\), and \(C = [4; -1]\). Determine the normalized direction vectors from characters \(A\) and \(B\) pointing toward the other characters. Draw a corresponding diagram.
Solution. For the direction vector \(\overrightarrow{AB}\), we have: \(\overrightarrow{AB}= (1-(-5); -2-2) = (6;-4)\). To normalize this vector, we simply divide it by its magnitude: \(\left | \overrightarrow{AB} \right | = \sqrt{6^2+(-2)^2} = \sqrt{52}.\) The normalized vector corresponding to \(\overrightarrow{AB}\) is denoted by \(\widehat{AB}\), and we get:
\[ \widehat{AB} = \frac{\overrightarrow{AB}}{\left | \overrightarrow{AB} \right | } = \frac{(6;-4)}{\sqrt{52}} = \left(\frac{3}{\sqrt{13}};-\frac{2}{\sqrt{13}}\right). \]
Similarly
\[ \widehat{AC} = \frac{\overrightarrow{AC}}{\left | \overrightarrow{AC} \right | } = \frac{(9;-3)}{\sqrt{90}} = \left(\frac{3}{\sqrt{10}};-\frac{1}{\sqrt{10}}\right), \]
\[\widehat{BC} = \frac{\overrightarrow{BC}}{\left | \overrightarrow{BC} \right | } = \frac{(3;1)}{\sqrt{10}} = \left(\frac{3}{\sqrt{10}};\frac{1}{\sqrt{10}}\right),\]
\[\widehat{BA} = \frac{\overrightarrow{BA}}{\left | \overrightarrow{BA} \right | } = \frac{(-6;4)}{\sqrt{52}} = \left(-\frac{3}{\sqrt{13}};\frac{2}{\sqrt{13}}\right).\]
There was no need to calculate the vector \(\widehat{BA}\), since it has the same magnitude as \(\widehat{AB}\) but points in the opposite direction. The coordinates of such vectors differ only in sign.
Exercise 3. Let the positions of characters \(A = [a_1;a_2]\) and \(B = [b_1;b_2]\) be given, with the characters standing at different locations. Determine:
the normalized direction vector \(\widehat{BA}\),
where character \(B\) will be located after walking three unit lengths in the direction of character \(A\)?
Solution. a) What we calculated in the previous example using specific coordinates can now be written in general form. That is,
\[\widehat{BA} = \frac{\overrightarrow{BA}}{\left| \overrightarrow{BA} \right| } = \frac{(a_1-b_1;a_2-b_2)}{\sqrt{(a_1-b_1)^2+(a_2-b_2)^2}}.\]
\[B+3\cdot\widehat{BA} = [b_1;b_2] +3\frac{(a_1-b_1;a_2-b_2)}{\sqrt{(a_1-b_1)^2+(a_2-b_2)^2}}.\]
The result of the dot product of two vectors is a scalar — that is, a real number. In game programming, the dot product of normalized vectors plays an important role.
Exercise 4. Determine the dot products of the normalized direction vectors from the solution to Exercise 2.
Solution.
\[ \widehat{AB} \cdot \widehat{AC} = \left(\frac{3}{\sqrt{13}};-\frac{2}{\sqrt{13}}\right) \cdot \left(\frac{3}{\sqrt{10}};-\frac{1}{\sqrt{10}}\right) = \frac{9}{\sqrt{130}}+\frac{2}{\sqrt{130}} = \frac{11}{\sqrt{130}} \dot=0{.}96 \]
\[ \widehat{BA} \cdot \widehat{BC} = \left(-\frac{3}{\sqrt{13}};\frac{2}{\sqrt{13}}\right) \cdot \left(\frac{3}{\sqrt{10}};\frac{1}{\sqrt{10}}\right) = -\frac{9}{\sqrt{130}}+\frac{2}{\sqrt{130}} = -\frac{7}{\sqrt{130}} \dot= -0{.}054 \]
The dot product of two normalized vectors is very useful, because it tells us to what extent the two vectors point in the same or similar direction! In this case, the value of the dot product ranges from \(-1\) to \(1\): a value of \(1\) means the vectors point in exactly the same direction, a value of \(-1\) means they point in exactly opposite directions, and a value close to \(0\) means the vectors are nearly perpendicular to each other. The reason for this range from \(-1\) to \(1\) is that we are working within the range of values of the cosine function. For any two vectors \(\vec{p}\), \(\vec{q}\) the dot product can also be written as:
\[ \vec{p} \cdot \vec{q}=\left | \vec{p} \right |\left | \vec{q} \right |\cos\alpha, \]
where \(\alpha\) is the angle between vectors and .
Exercise 5. An observer at the origin is looking at object \(A=[3;1]\). Determine the angle \(\alpha\) by which the observer must rotate so that they are facing directly toward object \(B=[1;2]\).
Solution. We again treat points \(A\) and \(B\) as the endpoints of vectors \(\vec{a}=(3;1)\) and \(\vec{b}=(1;2)\). From the formula for the dot product of two vectors, we can express \(\cos\alpha\):
\[ \cos\alpha =\frac{\vec{a} \cdot \vec{b}}{\left | \vec{a} \right |\left | \vec{b} \right |}\,. \]
After substituting the specific values, we obtain
\[ \cos\alpha = \frac{(3;1) \cdot (1;2)}{\sqrt{3^2+1^2} \cdot \sqrt{1^2+2^2}} = \frac{3 \cdot 1 + 1 \cdot 2}{\sqrt{10} \cdot \sqrt{5}} = \frac{5}{\sqrt{50}} =\sqrt{\frac{25}{50}} = \frac{1}{\sqrt{2}}.\]
We know that \(\frac{1}{\sqrt{2}}=\frac{\sqrt{2}}{2}\) is a well-known trigonometric value, which means that \(\alpha=45^{\circ}\). Alternatively, we can calculate the angle \(\alpha\) as \(\arccos \frac{1}{\sqrt{2}}\). Therefore, the observer must rotate by an angle of \(45^{\circ}\).
If the normalized direction vectors were explicitly given in the problem, then their dot product would be equal to \(\cos\alpha\).
\[ \cos\alpha =\frac{\vec{a}}{\left | \vec{a} \right |} \cdot \frac{\vec{b}}{\left | \vec{b} \right |} = \hat{a} \cdot \hat{b}\]
This is the reason why characters’ facing directions and the direction vectors between characters are typically stored in normalized form in attribute lists.
The dot product can also be conveniently used to solve the following problem. Let’s say I am designing a game where the player is trying to hide from guards. In that case, we are interested in whether or not a guard can see individual players.
To make the game more realistic, we want the guard to have a field of view within which a player is visible. For humans, the horizontal visual field covered by both eyes is approximately \(180^{\circ}\), but that would be too wide for our guard. So let’s say we want the guard’s field of view to be \(170^{\circ}\).
Exercise 6. The guard \(G\) has a field of view of \(170^{\circ}\). What will be the values of the dot products between the guard’s viewing direction \(\vec{d}\) and the normalized direction vectors to the objects that the guard can see?
Solution. From the guard’s viewing direction to the boundaries of their field of view — both to the left and right — the angle is \(85^{\circ}\). We just need to calculate \(\cos 85^{\circ} \dot= 0{.}087\). So, the dot products between the guard’s viewing direction and the normalized direction vectors to visible objects will fall within the range from approximately \(0{.}087\) to \(1\).
In the attribute list associated with the guard, we add not only their viewing direction, but also a threshold that defines their field of view. Using this threshold, we can check whether or not the guard sees a given player. For simplicity, we round the previous result to one decimal place and limit the guard’s field of view using a threshold value of \(0{.}1\).
Exercise 7. Determine whether a guard located at the origin can see player \(A=[3;-2]\), given that the guard’s viewing direction is \(\left(\frac{1}{\sqrt{5}};\frac{2}{\sqrt{5}}\right)\) and the threshold for the guard’s field of view is set to \(0{.}1\).
Solution. The guard’s viewing direction is already a normalized vector. So we just need to normalize the direction vector from the guard to player \(A\). Since the guard is located at the origin, this is equivalent to normalizing the vector \(\vec{a}=(3;-2)\). We get:
\[\hat{a}=\frac{\left(3;-2\right)}{3^2+(-2)^2} = \frac{\left(3;-2\right)}{15} = \left(\frac{3}{15};\frac{-2}{15}\right).\]
Now we calculate the dot product of the two normalized vectors:
\[\left(\frac{1}{\sqrt{5}};\frac{2}{\sqrt{5}}\right) \cdot \left(\frac{3}{\sqrt{15}};\frac{-2}{\sqrt{15}}\right) = \frac{3}{\sqrt{75}} - \frac{4}{\sqrt{75}} = -\frac{1}{\sqrt{75}} \dot= -0{.}12.\]
Since the result is not in the range between \(0,1\) and \(1\), the guard does not see player \(A\). Moreover, the negative result indicates that the angle between the vectors is greater than a right angle.
Think about how would the situation change if the guard was not positioned at the origin.