How does dot product work?

Godot Version

v 4.6.1

Question

I can`t manage to understand how the dot product works. I undestand that it`s an operation between 2 vectors which returns a scalar (a value representing only a magnitude).But I can`t understand what exactly happens in the if statement in the _physics_process function. (I`ve read the documentations about the simple vector math, yet i still can`t grasp what happens in this code very well)

Code:

The ball:

extends CharacterBody2D

var direction = Vector2.ZERO
var speed = 350

func _ready():
	global_position = Vector2(960, 600)
	#Each variable gets a random number for direction
	direction.x = [1, -1].pick_random()
	direction.y = [1, -1].pick_random()
	print("Inicial Direction: " + str(direction))
	velocity = speed * direction
	print("Inicial Velocity: " + str(velocity))

#Controls the movement of the ball
func _physics_process(delta):
	var colisionInfo := move_and_collide(velocity * delta)
	if colisionInfo:
		var normal := colisionInfo.get_normal()
		if normal.dot(velocity.normalized()) <= 0.0:
			velocity = velocity.bounce(normal)
			print("Normal: " + str(normal))
			print("Normalized Direction: " + str(direction.normalized()))
			print("Normalized Velocity: " + str(velocity.normalized()))
			print(normal.dot(velocity.normalized()))

The player:

extends CharacterBody2D

var speed: int = 400
var direction: Vector2

func _ready():
	global_position = Vector2(960, 1000)

func _process(delta):
	if Input.is_action_pressed("left"):
		direction.x = -1
	elif Input.is_action_pressed("right"):
		direction.x = 1
	else:
		direction.x = 0
	velocity = direction * speed
	move_and_collide(velocity * delta)

a dot product can be understood as the comparison between two vectors a and b.
in rendering, here we have the NORMAL vector and the VIEW vector. the normal points away from each face of the sphere while the VIEW comes from the camera.
the dot product results in the faces that are aligned being white (closer to 1) and the ones perpendicular being black (closer to 0):
NORMAL


VIEW

dot(NORMAL, VIEW)

1 Like

Dot product is in games used mostly to determine the inclination of a direction vector in respect to another direction vector, i.e. how ā€œsimilarā€ their directions are. It can be used to calculate the exact angle between them at the cost of an additional trigonometric function call, but the exact angle is not necessary in many cases.

By definition:

dot(a,b) = length(a) * length(b) * cosine_of_angle_between_a_and_b

If both vectors are normal vectors, i.e. their length is 1 then is boils down to simply:

dot(a,b) = cosine_of_angle_between_a_and_b

It’s directly equal to the cosine of the angle between two vectors. Since the dot product can be calculated simply by multiplying and adding the components, it lets us cheaply calculate the cosine of that angle.

What’s useful here is the fact that if the directions coincide, the dot product will be exactly 1. If they are perpendicular it will be exactly 0. For angles inbetween it will be some value between 0 and 1. If the vectors face in ā€œoppositeā€ directions the dot product will be a negative value between 0 and -1.

So to summarize; if vectors are facing in the same direction, the dot product it will be 1, if they are perpendicular it will be 0, and if they are facing away from each other, it will be -1.

This is very useful in many situations.

If you need an exact angle between the vectors, simply do an inverse cosine of the dot product, which is precisely what functions like Vector3::angle_to() do under the hood.

4 Likes

Thanks for explaination! I`ve got another question, what are cosines? I do not know them.

It’s part of a field of mathematics that binds geometry and algebra called trigonometry (the rules or measures of the triangle or trigon)
Plural of cosinus, which is part of a suit of ratios of specific triangle sides. The proof starts with a right angle triangle hypothenus and generalizes from there.

Trigonometry :

3 Likes

Take a basic trigonometry course, it’ll be immensely useful. You need to understand basic trigonometry concepts when doing any kind of graphics programming. No question about it.

4 Likes

This image from wolfire’s old blog does the trick for me

Similar normalized vectors will reach 1.0, while opposite normalized vectors reach -1.0, perpendicular results in 0.0, this ends up being a cosine value so you can get the exact angle from the inverse arc-cosine arcos(dot(a, b)).

If you use dot on non-normalized vectors you will get a square-magnitude of both vectors which is more complicated to get something useful out of.

https://www.wolfire.com/blog/2009/07/linear-algebra-for-game-developers-part-2/

3 Likes

Thank everyone!

The dot product takes two vectors and returns a scalar a real number.

All of the above is true, but it is also :

A = (a1, a2, a3) = a1 i + a2 j + a3 k

B = (b1, b2, b3) = b1 i + b2 j + b3 k

Where i = (1,0,0), j = (0,1,0), k = (0,0,1) are unit vectors pointing in the directions of the principal axis of the space or basis the vectors are defined in.

A.B = dot(A,B) = a1 b1 i.i + a2 b2 j.j + a3 b3 k.k

= a1 b1 + a2 b2 + a3 b3

since i.i == j.j ==k.k ==1