4040
4141This is a **vector **. A vector represents a lot of useful information. As well
4242as telling us that the point is at ``(4, 3) ``, we can also think of it as an
43- angle ``θ `` and a length (or magnitude) ``m ``. In this case, the arrow is a
44- **position vector ** - it denotes a position in space, relative to the origin.
43+ angle ``θ `` (theta) and a length (or magnitude) ``m ``. In this case, the arrow
44+ is a **position vector ** - it denotes a position in space, relative to the
45+ origin.
4546
4647A very important point to consider about vectors is that they only represent
4748**relative ** direction and magnitude. There is no concept of a vector's
@@ -74,7 +75,9 @@ pixels down, use the following code:
7475
7576Godot supports both :ref: `Vector2 <class_Vector2 >` and :ref: `Vector3
7677<class_Vector3>` for 2D and 3D usage, respectively. The same mathematical rules
77- discussed in this article apply to both types.
78+ discussed in this article apply to both types, and wherever we link to
79+ ``Vector2 `` methods in the class reference, you can also check out their
80+ ``Vector3 `` counterparts.
7881
7982Member access
8083-------------
@@ -84,18 +87,18 @@ The individual components of the vector can be accessed directly by name.
8487.. tabs ::
8588 .. code-tab :: gdscript GDScript
8689
87- # create a vector with coordinates (2, 5)
90+ # Create a vector with coordinates (2, 5).
8891 var a = Vector2(2, 5)
89- # create a vector and assign x and y manually
92+ # Create a vector and assign x and y manually.
9093 var b = Vector2()
9194 b.x = 3
9295 b.y = 1
9396
9497 .. code-tab :: csharp
9598
96- // create a vector with coordinates (2, 5)
99+ // Create a vector with coordinates (2, 5).
97100 var a = new Vector2(2, 5);
98- // create a vector and assign x and y manually
101+ // Create a vector and assign x and y manually.
99102 var b = new Vector2();
100103 b.X = 3;
101104 b.Y = 1;
@@ -125,7 +128,8 @@ Scalar multiplication
125128---------------------
126129
127130.. note :: Vectors represent both direction and magnitude. A value representing
128- only magnitude is called a **scalar **.
131+ only magnitude is called a **scalar **. Scalars use the
132+ :ref: `class_float ` type in Godot.
129133
130134A vector can be multiplied by a **scalar **:
131135
@@ -155,14 +159,21 @@ Movement
155159
156160A vector can represent **any ** quantity with a magnitude and direction. Typical
157161examples are: position, velocity, acceleration, and force. In this image, the
158- spaceship at step 1 has a position vector of ``(1,3) `` and a velocity vector of
159- ``(2,1) ``. The velocity vector represents how far the ship moves each step. We
162+ spaceship at step 1 has a position vector of ``(1, 3) `` and a velocity vector of
163+ ``(2, 1) ``. The velocity vector represents how far the ship moves each step. We
160164can find the position for step 2 by adding the velocity to the current position.
161165
162166.. image :: img/vector_movement1.png
163167
164168.. tip :: Velocity measures the **change** in position per unit of time. The new
165- position is found by adding velocity to the previous position.
169+ position is found by adding the velocity multiplied by the elapsed time
170+ (here assumed to be one unit, e.g. 1 s) to the previous position.
171+
172+ In a typical 2D game scenario, you would have a velocity in pixels per
173+ second, and multiply it by the ``delta `` parameter (time elapsed since
174+ the previous frame) from the :ref: `_process() <class_Node_method__process >`
175+ or :ref: `_physics_process() <class_Node_method__physics_process >`
176+ callbacks.
166177
167178Pointing toward a target
168179------------------------
@@ -171,9 +182,9 @@ In this scenario, you have a tank that wishes to point its turret at a robot.
171182Subtracting the tank's position from the robot's position gives the vector
172183pointing from the tank to the robot.
173184
174- .. image :: img/vector_subtract2.png
185+ .. image :: img/vector_subtract2.webp
175186
176- .. tip :: To find a vector pointing from ``A`` to ``B`` use ``B - A``.
187+ .. tip :: To find a vector pointing from ``A`` to ``B``, use ``B - A``.
177188
178189Unit vectors
179190~~~~~~~~~~~~
@@ -187,8 +198,8 @@ Normalization
187198
188199**Normalizing ** a vector means reducing its length to ``1 `` while preserving its
189200direction. This is done by dividing each of its components by its magnitude.
190- Because this is such a common operation, `` Vector2 `` and `` Vector3 `` provide a
191- method for normalizing :
201+ Because this is such a common operation, Godot provides a dedicated
202+ :ref: ` normalized() < class_Vector2_method_normalized >` method for this :
192203
193204.. tabs ::
194205 .. code-tab :: gdscript GDScript
@@ -199,12 +210,11 @@ method for normalizing:
199210
200211 a = a.Normalized();
201212
202-
203213.. warning :: Because normalization involves dividing by the vector's length, you
204214 cannot normalize a vector of length ``0 ``. Attempting to do so
205215 would normally result in an error. In GDScript though, trying to
206- call the ``normalized() `` method on a `` Vector2 `` or `` Vector3 `` of
207- length 0 leaves the value untouched and avoids the error for you.
216+ call the ``normalized() `` method on a vector of length 0 leaves the
217+ value untouched and avoids the error for you.
208218
209219Reflection
210220----------
@@ -220,25 +230,22 @@ other object:
220230
221231The surface normal has a value of ``(0, -1) `` because this is a horizontal
222232surface. When the ball collides, we take its remaining motion (the amount left
223- over when it hits the surface) and reflect it using the normal. In Godot, the
224- :ref: `Vector2 < class_Vector2 >` class has a `` bounce() ` ` method to handle this.
225- Here is a GDScript example of the diagram above using a :ref: `CharacterBody2D
233+ over when it hits the surface) and reflect it using the normal. In Godot, there
234+ is a :ref: `bounce() < class_Vector2_method_bounce > ` method to handle this.
235+ Here is a code example of the above diagram using a :ref: `CharacterBody2D
226236<class_CharacterBody2D>`:
227237
228-
229238.. tabs ::
230239 .. code-tab :: gdscript GDScript
231240
232- # object "collision" contains information about the collision
233- var collision = move_and_collide(velocity * delta)
241+ var collision: KinematicCollision2D = move_and_collide(velocity * delta)
234242 if collision:
235243 var reflect = collision.get_remainder().bounce(collision.get_normal())
236244 velocity = velocity.bounce(collision.get_normal())
237245 move_and_collide(reflect)
238246
239247 .. code-tab :: csharp
240248
241- // KinematicCollision2D contains information about the collision
242249 KinematicCollision2D collision = MoveAndCollide(_velocity * (float)delta);
243250 if (collision != null)
244251 {
@@ -263,22 +270,25 @@ and
263270
264271.. image :: img/vector_dot2.png
265272
266- However, in most cases it is easiest to use the built-in method. Note that the
267- order of the two vectors does not matter:
273+ The mathematical notation *||A|| * represents the magnitude of vector ``A ``, and
274+ *A *\ :sub: `x` means the ``x `` component of vector ``A ``.
275+
276+ However, in most cases it is easiest to use the built-in :ref: `dot()
277+ <class_Vector2_method_dot>` method. Note that the order of the two vectors does not matter:
268278
269279.. tabs ::
270280 .. code-tab :: gdscript GDScript
271281
272282 var c = a.dot(b)
273- var d = b.dot(a) # These are equivalent.
283+ var d = b.dot(a) # These are equivalent.
274284
275285 .. code-tab :: csharp
276286
277287 float c = a.Dot(b);
278- float d = b.Dot(a); // These are equivalent.
288+ float d = b.Dot(a); // These are equivalent.
279289
280290The dot product is most useful when used with unit vectors, making the first
281- formula reduce to just ``cosθ ``. This means we can use the dot product to tell
291+ formula reduce to just ``cos(θ) ``. This means we can use the dot product to tell
282292us something about the angle between two vectors:
283293
284294.. image :: img/vector_dot3.png
@@ -297,11 +307,12 @@ player?
297307.. image :: img/vector_facing2.png
298308
299309The green arrows ``fA `` and ``fB `` are **unit vectors ** representing the
300- zombies' facing directions and the blue semicircle represents its field of view.
310+ zombie's facing direction and the blue semicircle represents its field of view.
301311For zombie ``A ``, we find the direction vector ``AP `` pointing to the player
302312using ``P - A `` and normalize it, however, Godot has a helper method to do this
303- called ``direction_to ``. If the angle between this vector and the facing vector
304- is less than 90°, then the zombie can see the player.
313+ called :ref: `direction_to() <class_Vector2_method_direction_to >`. If the angle
314+ between this vector and the facing vector is less than 90°, then the zombie can
315+ see the player.
305316
306317In code it would look like this:
307318
@@ -349,9 +360,8 @@ The cross product is calculated like this:
349360 c.Y = (a.Z * b.X) - (a.X * b.Z);
350361 c.Z = (a.X * b.Y) - (a.Y * b.X);
351362
352-
353-
354- With Godot, you can use the built-in method:
363+ With Godot, you can use the built-in :ref: `Vector3.cross() <class_Vector3_method_cross >`
364+ method:
355365
356366.. tabs ::
357367 .. code-tab :: gdscript GDScript
@@ -362,6 +372,10 @@ With Godot, you can use the built-in method:
362372
363373 var c = a.Cross(b);
364374
375+ The cross product is not mathematically defined in 2D. The :ref: `Vector2.cross()
376+ <class_Vector2_method_cross>` method is a commonly used analog of the 3D cross
377+ product for 2D vectors.
378+
365379.. note :: In the cross product, order matters. ``a.cross(b)`` does not give the
366380 same result as ``b.cross(a) ``. The resulting vectors point in
367381 **opposite ** directions.
@@ -371,16 +385,16 @@ Calculating normals
371385
372386One common use of cross products is to find the surface normal of a plane or
373387surface in 3D space. If we have the triangle ``ABC `` we can use vector
374- subtraction to find two edges ``AB `` and ``AC ``. Using the cross product, `` AB x
375- AC `` produces a vector perpendicular to both: the surface normal.
388+ subtraction to find two edges ``AB `` and ``AC ``. Using the cross product,
389+ `` AB × AC `` produces a vector perpendicular to both: the surface normal.
376390
377391Here is a function to calculate a triangle's normal:
378392
379393.. tabs ::
380394 .. code-tab :: gdscript GDScript
381395
382396 func get_triangle_normal(a, b, c):
383- # find the surface normal given 3 vertices
397+ # Find the surface normal given 3 vertices.
384398 var side1 = b - a
385399 var side2 = c - a
386400 var normal = side1.cross(side2)
@@ -390,7 +404,7 @@ Here is a function to calculate a triangle's normal:
390404
391405 Vector3 GetTriangleNormal(Vector3 a, Vector3 b, Vector3 c)
392406 {
393- // find the surface normal given 3 vertices
407+ // Find the surface normal given 3 vertices.
394408 var side1 = b - a;
395409 var side2 = c - a;
396410 var normal = side1.Cross(side2);
0 commit comments