Skip to content

Commit c076164

Browse files
committed
Tweak Vector math page (style, links, more notes)
Also cleanup style issues in the docs image guidelines.
1 parent 5a0916e commit c076164

File tree

4 files changed

+76
-55
lines changed

4 files changed

+76
-55
lines changed

contributing/documentation/docs_image_guidelines.rst

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.. _docs_image_guidelines:
22

3-
Creating Documentation Images
3+
Creating documentation images
44
=============================
55

66
Throughout documentation images are often needed to make the explanation
@@ -36,10 +36,10 @@ on how to do this later on this page.
3636
Format conversion
3737
-----------------
3838

39-
The current format for images in Godot's documentation is WebP (.webp). While some
40-
Linux programs will support saving screenshots in this format, macOS and the
41-
Snip & Sketch program on Windows do not. For images that don't need editing,
42-
such as precise cropping or adding outlines, Squoosh can be used.
39+
The current format for images in Godot's documentation is WebP (``.webp``).
40+
While some Linux programs will support saving screenshots in this format, macOS
41+
and the Snip & Sketch program on Windows do not. For images that don't need
42+
editing, such as precise cropping or adding outlines, Squoosh can be used.
4343
`Squoosh <https://squoosh.app/>`_ is a converter developed by Google, is open
4444
source, and doesn't give Google any image rights by using it.
4545

@@ -87,9 +87,9 @@ Scaling down an image
8787
As explained earlier on this page, all images taken on a screen that is a higher resolution
8888
than 1080p should be scaled down. To do this in Krita click on **Image** on the top bar, and
8989
from the dropdown menu select **Scale Image To New Size**. This menu can also be opened by
90-
pressing :kbd: `ctrl + alt + I`. On this menu you want to adjust the pixel dimensions. For
90+
pressing :kbd:`Ctrl + Alt + I`. On this menu you want to adjust the pixel dimensions. For
9191
anything taken on a 4K monitor change the value of the width and height to half of its current
92-
value, for anything taken on a 1440p monitor multiply the width and height by .75. Make
92+
value, for anything taken on a 1440p monitor multiply the width and height by 0.75. Make
9393
sure the **Constrain Proportions** box at the bottom of the menu is checked so you only have
9494
to change 1 value.
9595

@@ -107,14 +107,14 @@ Outlines arrows and text
107107
Sometimes an image needs something extra to properly direct the readers
108108
attention, or make something clear. Outlines and arrows can be used
109109
for this purpose. For these types of edits Inkscape is the recommended open
110-
source program, it can be downloaded from the `official Inkscape website <https://inkscape.org/release/inkscape-1.2.1/>`_.
111-
Like Krita, if you're on linux you can also check your distributions repository
110+
source program, it can be downloaded from the `official Inkscape website <https://inkscape.org/>`_.
111+
Like Krita, if you're on Linux you can also check your distributions repository
112112
or get it from Flathub.
113113

114114
A full tutorial on creating outlines is not provided here, we recommend searching
115115
for various tutorials on how to use it online. However there are two standards
116116
for doc image outlines and arrows. First, the color should be yellow, specifically
117-
this hex color: fffb44 (fffb44ff if there is a transparency value like in inkscape).
117+
this hex color: ``fffb44`` (``fffb44ff`` if there is a transparency value like in Inkscape).
118118
This color was chosen specifically to make sure color blind people do not have
119119
issues reading the documentation, other colors can be used in addition to this yellow
120120
if multiple outlines on an image are needed, red should be avoided. The second standard
@@ -129,8 +129,15 @@ multiple outlines in multiple colors.
129129
Adding an image to a documentation page
130130
---------------------------------------
131131

132-
Once you've finished working on your image it can be added to the documentation.
133-
All images are stored in folders named `img`. To add your image add it to the img
134-
folder that's in the same folder as the rst file for the page. To add it in the
135-
text yourself you would type this `.. image:: img/documentation_image.webp` and
136-
`documentation_image.webp` would be whatever your image is named.
132+
Once you've finished working on your image, it can be added to the documentation.
133+
All images are stored in folders named ``img`` next to the page they are used in.
134+
135+
To add your image, add it to the ``img`` folder that's in the same folder as the
136+
``.rst`` file for the page (create it if it doesn't exist). In the ``.rst`` page,
137+
images should be included with the following code snippet::
138+
139+
.. image:: img/documentation_image.webp
140+
141+
Where ``documentation_image.webp`` would be changed to the name of the image you
142+
created. Name your images in a way that makes their meaning clear, possibly with
143+
a prefix that makes their relationship to a documentation page explicit.
-25.3 KB
Binary file not shown.
12.7 KB
Loading

tutorials/math/vector_math.rst

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ point:
4040

4141
This is a **vector**. A vector represents a lot of useful information. As well
4242
as 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

4647
A 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

7576
Godot 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

7982
Member 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

130134
A vector can be multiplied by a **scalar**:
131135

@@ -155,14 +159,21 @@ Movement
155159

156160
A vector can represent **any** quantity with a magnitude and direction. Typical
157161
examples 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
160164
can 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

167178
Pointing toward a target
168179
------------------------
@@ -171,9 +182,9 @@ In this scenario, you have a tank that wishes to point its turret at a robot.
171182
Subtracting the tank's position from the robot's position gives the vector
172183
pointing 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

178189
Unit vectors
179190
~~~~~~~~~~~~
@@ -187,8 +198,8 @@ Normalization
187198

188199
**Normalizing** a vector means reducing its length to ``1`` while preserving its
189200
direction. 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

209219
Reflection
210220
----------
@@ -220,25 +230,22 @@ other object:
220230

221231
The surface normal has a value of ``(0, -1)`` because this is a horizontal
222232
surface. 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

280290
The 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
282292
us 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

299309
The 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.
301311
For zombie ``A``, we find the direction vector ``AP`` pointing to the player
302312
using ``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

306317
In 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

372386
One common use of cross products is to find the surface normal of a plane or
373387
surface 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

377391
Here 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

Comments
 (0)