VECTORS
VECTORS
Vectors are an important concept in game development
Vectors are an important concept in game development
They are used to represent a position or direction in code.
They are used to represent a position or direction in code.
In 2D a vector would represent the X and Y axes
In 2D a vector would represent the X and Y axes
While in 3D, it would represent the X, Y and Z axes.
While in 3D, it would represent the X, Y and Z axes.
2D Vectors
2D Vectors
2D Vectors represent the X and Y axis values of something. An example of this is a node’s position property,
2D Vectors represent the X and Y axis values of something. An example of this is a node’s position property,
which can be modified in the Inspector with any 2D node selected.
which can be modified in the Inspector with any 2D node selected.
Create a New Scene
Create a New Scene
Call it TestVector2D
Call it TestVector2D
Add a Node2D
Add a Node2D
Add a Sprite Node
Add a Sprite Node
Set texture as Player.png
Set texture as Player.png
Add a Script to the Sprite
Add a Script to the Sprite
Save it as Player.gd
Save it as Player.gd
Moving the player with code
Moving the player with code
To modify a node’s position in code,
To modify a node’s position in code,
Use the global_position variable
Use the global_position variable
to get and set the node’s position.
to get and set the node’s position.
Now access the X and Y values individually
Now access the X and Y values individually
by placing a dot in between the name and the property.
by placing a dot in between the name and the property.
We can try this by setting the x position to 200 in the _ready function:
We can try this by setting the x position to 200 in the _ready function:
Running the scene now will start the game with the Player Sprite 200 pixels to the right.
Running the scene now will start the game with the Player Sprite 200 pixels to the right.
We can also create our own vectors instead
We can also create our own vectors instead
of setting the position vector individually.
of setting the position vector individually.
We can create our own vector variable
We can create our own vector variable
and then assign this vector to the node’s
and then assign this vector to the node’s
position to move our Player node to
position to move our Player node to
the desired location.
the desired location.
Player has moved diagonally down and to the right
Player has moved diagonally down and to the right
This knowledge can also be transferred onto 3D, with the only difference being the additional Z axis.
This knowledge can also be transferred onto 3D, with the only difference being the additional Z axis.
You also need to use Vector3 instead of Vector2 when declaring a 3D vector, so that you can use the
You also need to use Vector3 instead of Vector2 when declaring a 3D vector, so that you can use the
additional Z value when creating it.
additional Z value when creating it.
Moving the Player along a Vector
Moving the Player along a Vector
The _process function is a loop
The _process function is a loop
any code inside it runs continuously.
any code inside it runs continuously.
If we multiply our Vector by 30
If we multiply our Vector by 30
the Player moves at 30 pixels PER FRAME
the Player moves at 30 pixels PER FRAME
To make our Player move smoothly
To make our Player move smoothly
at 30 pixels PER SECOND
at 30 pixels PER SECOND
We multiply by Delta
We multiply by Delta