|
@@ -188,25 +188,8 @@ Normalization
|
|
|
|
|
|
**Normalizing** a vector means reducing its length to ``1`` while
|
|
**Normalizing** a vector means reducing its length to ``1`` while
|
|
preserving its direction. This is done by dividing each of its components
|
|
preserving its direction. This is done by dividing each of its components
|
|
-by its magnitude:
|
|
|
|
-
|
|
|
|
-.. tabs::
|
|
|
|
- .. code-tab:: gdscript GDScript
|
|
|
|
-
|
|
|
|
- var a = Vector2(2, 4)
|
|
|
|
- var m = sqrt(a.x*a.x + a.y*a.y) # get magnitude "m" using the Pythagorean theorem
|
|
|
|
- a.x /= m
|
|
|
|
- a.y /= m
|
|
|
|
-
|
|
|
|
- .. code-tab:: csharp
|
|
|
|
-
|
|
|
|
- var a = new Vector2(2, 4);
|
|
|
|
- var m = Mathf.Sqrt(a.x*a.x + a.y*a.y); // get magnitude "m" using the Pythagorean theorem
|
|
|
|
- a.x /= m;
|
|
|
|
- a.y /= m;
|
|
|
|
-
|
|
|
|
-Because this is such a common operation, ``Vector2`` and ``Vector3`` provide
|
|
|
|
-a method for normalizing:
|
|
|
|
|
|
+by its magnitude. Because this is such a common operation,
|
|
|
|
+``Vector2`` and ``Vector3`` provide a method for normalizing:
|
|
|
|
|
|
.. tabs::
|
|
.. tabs::
|
|
.. code-tab:: gdscript GDScript
|
|
.. code-tab:: gdscript GDScript
|
|
@@ -291,12 +274,12 @@ the order of the two vectors does not matter:
|
|
.. code-tab:: gdscript GDScript
|
|
.. code-tab:: gdscript GDScript
|
|
|
|
|
|
var c = a.dot(b)
|
|
var c = a.dot(b)
|
|
- var d = b.dot(a) # these are equivalent
|
|
|
|
|
|
+ var d = b.dot(a) # These are equivalent.
|
|
|
|
|
|
.. code-tab:: csharp
|
|
.. code-tab:: csharp
|
|
|
|
|
|
float c = a.Dot(b);
|
|
float c = a.Dot(b);
|
|
- float d = b.Dot(a); // these are equivalent
|
|
|
|
|
|
+ float d = b.Dot(a); // These are equivalent.
|
|
|
|
|
|
The dot product is most useful when used with unit vectors, making the
|
|
The dot product is most useful when used with unit vectors, making the
|
|
first formula reduce to just ``cosθ``. This means we can use the dot
|
|
first formula reduce to just ``cosθ``. This means we can use the dot
|
|
@@ -319,7 +302,8 @@ zombies ``A`` and ``B``. Assuming a zombie's field of view is **180°**, can the
|
|
The green arrows ``fA`` and ``fB`` are **unit vectors** representing the
|
|
The green arrows ``fA`` and ``fB`` are **unit vectors** representing the
|
|
zombies' facing directions and the blue semicircle represents its field of
|
|
zombies' facing directions and the blue semicircle represents its field of
|
|
view. For zombie ``A``, we find the direction vector ``AP`` pointing to
|
|
view. For zombie ``A``, we find the direction vector ``AP`` pointing to
|
|
-the player using ``P - A`` and normalize it. If the angle between this
|
|
|
|
|
|
+the player using ``P - A`` and normalize it, however, Godot has a helper
|
|
|
|
+method to do this called ``direction_to``. If the angle between this
|
|
vector and the facing vector is less than 90°, then the zombie can see
|
|
vector and the facing vector is less than 90°, then the zombie can see
|
|
the player.
|
|
the player.
|
|
|
|
|
|
@@ -328,13 +312,13 @@ In code it would look like this:
|
|
.. tabs::
|
|
.. tabs::
|
|
.. code-tab:: gdscript GDScript
|
|
.. code-tab:: gdscript GDScript
|
|
|
|
|
|
- var AP = (P - A).normalized()
|
|
|
|
|
|
+ var AP = A.direction_to(P)
|
|
if AP.dot(fA) > 0:
|
|
if AP.dot(fA) > 0:
|
|
print("A sees P!")
|
|
print("A sees P!")
|
|
|
|
|
|
.. code-tab:: csharp
|
|
.. code-tab:: csharp
|
|
|
|
|
|
- var AP = (P - A).Normalized();
|
|
|
|
|
|
+ var AP = A.DirectionTo(P);
|
|
if (AP.Dot(fA) > 0)
|
|
if (AP.Dot(fA) > 0)
|
|
{
|
|
{
|
|
GD.Print("A sees P!");
|
|
GD.Print("A sees P!");
|