Menu
CoddyTech

Vector2 Math

Part of the Nodes and Scenes section of Coddy's Godot journey. Lesson 16 of 28.

You can do math with a Vector2. Adding two vectors adds the xs and adds the ys:

Vector2(100, 50) + Vector2(10, 5)   # (110, 55)

Multiplying by a number multiplies both: Vector2(100, 50) * 2 is (200, 100). That is how velocity * delta works.

A vector's length is how far it reaches in a straight line. .length() works it out:

var step = Vector2(3, 4)
print(step.length())   # 5.0

For a velocity, the length is the real speed. A robot moving 120 right and 90 up each second covers 150 pixels a second.

challenge icon

Challenge

Easy

Print the robot's real speed, then fly it.

  1. In _ready(), print "Speed: " and the length of velocity, with .length().
  2. In _process(), move the robot by velocity per second, with delta.

Press Play: the Console shows Speed: 150.0 and the robot flies up and right. Then Check.

Try it yourself

extends Node2D

var velocity = Vector2(120, -90)

func _ready():
	pass

func _process(delta):
	pass

All lessons in Nodes and Scenes

Practice on your own: Godot playground