Dot Product: Difference between revisions

(Created page with "Calculates the dot product of the two input vectors, and outputs a number. /uploads/Dot Product1.png == Details == The Dot Product is a way of multiplying two vectors together, and is written as A · B. We can calculate it algebraically this way: A · B = Ax × Bx + Ay × By + Az × Bz We multiply the x's, multiply the y's, multiply the z's, and then sum them all together. For more advanced readers, it can also be calculated thi...")
 
m (Change some file extensions)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Calculates the dot product of the two input vectors, and outputs a [[number]].
{{Block
 
|image=Dot Product.png
[[width=336px,alt=Dot Product|/uploads/Dot Product1.png]]
|type=s
|folder=math
|input1={{Port|v}}
|input2={{Port|v}}
|output1={{Port|n|Dot Product}}
}}
Calculates the dot product of the two input [[vector]]s.


== Details ==
== Details ==


The Dot Product is a way of multiplying two vectors together, and is written as A · B.
The Dot Product is a way of multiplying two vectors together, and is written as <code>A · B.</code>


We can calculate it algebraically this way:
We can calculate it algebraically this way:


A · B = Ax × Bx + Ay × By + Az × Bz
<code>A · B = Ax × Bx + Ay × By + Az × Bz</code>


We multiply the x's, multiply the y's, multiply the z's, and then sum them all together.
We multiply the x's, multiply the y's, multiply the z's, and then sum them all together.
Line 15: Line 21:
For more advanced readers, it can also be calculated this way:
For more advanced readers, it can also be calculated this way:


A · B = \|A\| × \|B\| × cos(θ)
<code>A · B = |A| × |B| × cos(θ)</code>


Where \|A\| is the magnitude (length) of vector A, \|B\| is the magnitude (length) of vector B, and θ is the angle between A and B.
Where |A| is the magnitude (length) of vector A, |B| is the magnitude (length) of vector B, and θ is the angle between A and B.


So we multiply the length of A times the length of B, then multiply by the [[Cos|cosine]] of the angle between A and B.
So we multiply the length of A times the length of B, then multiply by the [[Cos|cosine]] of the angle between A and B.
Line 32: Line 38:


== Example ==
== Example ==
=== Finding its cosine angle ===


The dot product of two [[Normalize|unit vector]]s outputs the cosine of the angle between those two vectors. If our two inputs aren't unit vectors, we can find the cosine with two methods:
The dot product of two [[Normalize|unit vector]]s outputs the cosine of the angle between those two vectors. If our two inputs aren't unit vectors, we can find the cosine with two methods:
Line 37: Line 45:
* Get the output of the dot product and divide it by the product of the two input vectors' length.
* Get the output of the dot product and divide it by the product of the two input vectors' length.


[[File:Screenshot_2022_0628_220807.png]]
{{Image|Dot Product cosine angle a.png}}
 
To find the angle, we then use the [[Break Rotation#Example|inverse cosine]] function:


To find the angle, we then use the inverse cosine function:
{{Image|Dot Product cosine angle b.png}}


[[File:Screenshot_2022_0628_221027.png]]
=== Object on sight ===


Here is another example, there is a guard facing a direction with a FOV of 90° (highlighted in green) and a ninja sneaking up on him. How can we check if the ninja is on sight?
Here is another example, there is a guard facing a direction with a FOV of 90° (highlighted in green) and a ninja sneaking up on him. How can we check if the ninja is on sight?


[[File:Screenshot_20210106-131627_Fancade.jpg]]
{{Image|Dot product on sight a.jpg}}


The first input for the dot product is the direction that the guard is facing, the second input is the vector from the guard's position to the ninja's position. Then we compare the cosine of the angle formed by those two vector with the cosine of half the FOV:
The first input for the dot product is the direction that the guard is facing, the second input is the vector from the guard's position to the ninja's position. Then we compare the cosine of the angle formed by those two vector with the cosine of half the FOV:


[[File:Screenshot_2022_0628_221438.png]]
{{Image|Dot product on sight b.png}}


Let me explain the script:
Let me explain the script:
Line 56: Line 66:
We don't have to normalize because the vector we rotated (0, 0, 1) already has a length of one.
We don't have to normalize because the vector we rotated (0, 0, 1) already has a length of one.
* Finally after we get the dot product, check if its less than the cosine of half the FOV (half because the direction of the guard sits in the middle between the two edges of the FOV). If true, then the ninja is in sight, otherwise the ninja is still safe.
* Finally after we get the dot product, check if its less than the cosine of half the FOV (half because the direction of the guard sits in the middle between the two edges of the FOV). If true, then the ninja is in sight, otherwise the ninja is still safe.


[[Category:Blocks]]
[[Category:Blocks]]

Latest revision as of 09:03, 16 June 2025

Calculates the dot product of the two input vectors.

Dot Product
Dot Product.png
TypeScript block
ColliderNone
FolderMath
Ports
Inputs Vector
Vector
Outputs Dot Product

Details

The Dot Product is a way of multiplying two vectors together, and is written as A · B.

We can calculate it algebraically this way:

A · B = Ax × Bx + Ay × By + Az × Bz

We multiply the x's, multiply the y's, multiply the z's, and then sum them all together.

For more advanced readers, it can also be calculated this way:

A · B = |A| × |B| × cos(θ)

Where |A| is the magnitude (length) of vector A, |B| is the magnitude (length) of vector B, and θ is the angle between A and B.

So we multiply the length of A times the length of B, then multiply by the cosine of the angle between A and B.

The result is a number (called a "scalar" so we know it's not a vector).

Right Angles

When two vectors are at right angles to each other the dot product is zero. This can be a handy way of checking if two vectors are perpendicular.

If the angle formed by the two input vectors is acute, the dot product is positive.

If the angle is obtuse, then the dot product is negative.

Example

Finding its cosine angle

The dot product of two unit vectors outputs the cosine of the angle between those two vectors. If our two inputs aren't unit vectors, we can find the cosine with two methods:

  • Normalize the two input vectors first.
  • Get the output of the dot product and divide it by the product of the two input vectors' length.
 

To find the angle, we then use the inverse cosine function:

 

Object on sight

Here is another example, there is a guard facing a direction with a FOV of 90° (highlighted in green) and a ninja sneaking up on him. How can we check if the ninja is on sight?

 

The first input for the dot product is the direction that the guard is facing, the second input is the vector from the guard's position to the ninja's position. Then we compare the cosine of the angle formed by those two vector with the cosine of half the FOV:

 

Let me explain the script:

  • First we subtract the ninja's position by the guard's position. Then we normalize the vector before the dot product.
  • For the guard's direction, rotate the vector (0, 0, 1) by the guard's current rotation (because assuming that the guard faces up (0, 0, 1) if his current rotation is 0°).

We don't have to normalize because the vector we rotated (0, 0, 1) already has a length of one.

  • Finally after we get the dot product, check if its less than the cosine of half the FOV (half because the direction of the guard sits in the middle between the two edges of the FOV). If true, then the ninja is in sight, otherwise the ninja is still safe.