A Unity ID allows you to buy and/or subscribe to Unity products and services, shop in the Asset Store and participate
in the Unity community.
New Unity Live Help updates. Check them out here!
Discussion in 'Scripting' started by Nanior, Jul 12, 2019.
I don't really understand Vector3.Dot
No description further...
Do not try and bend Mathf.Dot... that's impossible... there is no Mathf.Dot
Dot product: https://learn.unity.com/tutorial/vector-maths#5c8a4304edbc2a0020d97735
Dot is short for dot product, which is a vector math function. It doesn't exist in Mathf tho, it's Vector3.Dot.
Also Quaternion.Dot iirc.
Sorry, its Vector.Dot
Explanations here: https://docs.unity3d.com/ScriptReference/Vector3.Dot.html
and here: https://learn.unity.com/tutorial/vector-maths#5c8a4304edbc2a0020d97735
It's something that won't help you make games.
The only reason to ever use it is if you find a formula somewhere that mentions Dot Product. When you translate that into Unity, hey -- Unity has dot product!
I know this is an older thread, but I came across it when searching to learn more about dot products and wanted to share in case anyone else was in the same position.
The use I've found for this is to detect when an object's center crosses a plane, for example. Essentially, using Vector3.Dot on two objects will return a positive value on one side of the plane and a negative value on the other.
I'm still getting the hang of it myself, but I was made aware of this technique by a portal tutorial from Brackeys though there are other implementations I can think of (progressive spawning triggers in a linear dungeon crawling game to save overhead on mobile apps, for example).
Dot Product is is just a way to get the angle. Most people would be better off using Vector3.Angle(v1, v2) instead.
Dot Product is actually the cosine of the angle. Going the same way (angle of 0) is 1, 45 degrees is 0.707, 90 degrees is 0, 180 degrees is -1. So, checking whether something is behind you is either dot(v1,v2)<0 or Angle(v1,v2)>90. If you want to check for an angle of <30 degrees with dot product, that's >0.5 in Dot product (the cosin of 30). Working with these funny numbers and flipped <'s is a pain.
With Dot Product both arrows need to be normalized. You won't get an error -- it will just be wrong. With Angle they don't. The one advantage for Dot Product is being faster than Angle. That's not worth it when you're getting the game to work the way you want, but pre-built standard formulas will use Dot instead of Angle.
Dot product is a mathematical operation on vectors that has a LOT of uses. As Owen-Reynolds mentioned last year... if you have a formula that uses dot product, yay, unity has that operation defined.
The definition of the operation is as follows in terms of algebra and geometry (taken from wikipedia):
The first sentence is just defining how it's algebraically defined. In symbolic form it's just saying:
f(v0, v1) = v0.x * v1.x + v0.y * v1.y + v2.x + v2.y
When reducing algebraic formulas you can use this information to simplify your algebra.
The geometric defintion is the practical one. It's basically saying that the dot product =
a dot b = ||a|| * ||b|| * cos(theta)
where theta is the angle between them, and ||a|| means "magnitude of a".
This is why you can get the angle between the vectors from the dot product (it's not technically the angle between them, though it's a significant value you can infer from it). All you do is divide by the magnitudes of both vectors and then take the acos of that. But you don't actually have to get the value, you can infer information about that angle instead.
To give you an example, this geometric relation means you can apply all sorts of geometric theory on vectors using the dot product to get a lot of different bits of information. For example when you said:
Well really the dot product is giving you the product of magnitude and the cos(theta). Thing is geometrically the cos(theta) is positive when the angle is acute, and negative when the angle is obtuse (<90 and >90 respectively).
(see how the red line is below the x-axis for values of pi/2 to 3pi/2, which is 90 to 270 in degrees)
Because magnitudes are always positive, the only sign that can result from this product must come from the cosine.
SO, when you take the dot product of some vector with the normal of some plane. The sign of that result is telling you if the angle between the normal and that vector is acute or obtuse. If it's acute we must be within 90 degrees of the normal and therefore on the same side of the plane as the normal. If it's negative we're more than 90 degrees from the normal, which means you must be on the opposite side of the plane.
And of course... if it was 0, that means your angle is a square angle (90 degrees), which means you're on the plane.
Thing is this isn't the only thing you may use it for. And angles aren't the only thing either. It's just geometry in general.
So lets say you know for certain that one of the vectors is a unit-vector. Such as if it's a vector defining an axis. Lets say that vector is "b" from our formula. That means we have:
a dot b = ||a||*1*cos(theta)
a dot b = ||a||*cos(theta)
b just falls out of our formula all together. Thing is what is ||a||*cos(theta) remind you of? Does it remind you of a right triangle?
||a|| * cos(theta) will give you the length of the near leg on a right triangle. Or effectively it's the "length in the direction of b" also known as "projection".
For example if you have the vector <2,4> and the axis <1,0>, the dot product is:
<2,4> dot <1,0> = 2 * 1 + 4 * 0 = 2
You got the x component of the vector.
Thing is if you dotted it on an arbitrary axis like so:
<2,4> dot <sqrt(2)/2, sqrt(2)/2> = 2 * sqrt(2)/2 + 4 * sqrt(2)/2 = sqrt(2) + 2*sqrt(2) = 3*sqrt(2)
(note <sqrt(2)/2,sqrt(2)/2> is just the normalized <1,1>)
We now have the distance down that arbitrary axis of <1,1>.
And the thing is... this is how things like "projection matrices" work (there's that word projection again). Note that matrix multiplication is really just a series of dot products of each component vector of the matrix.
Basically a vector is a set of component scalars... and a matrix is a set of component vectors.
So matrix multiplication is the dot product of each component vector:
And this is why you can use a matrix to transform between different "spaces".
Places this is used?
Using these matrices we can transform from inside a GameObject to the world, and vice versa.
OR using same a camera's projection matrix, you can project from a 3d-space to a 2d-surface like your monitor. And the value of that matrix can effect how it projects. You can orthographically do it so it just flattly projects onto 2d surface. Or you get a "perspective" projection that distorts it across a frustum.
Note how we built up our dot product to a matrix product. Just like how a power is just a built up product (a product of products).
Dot product is one of those "basic" operations in vector algebra (as opposed to scalar algebra which multiplication/division are part of).
Asking what "multiplication" is comes with all sorts of "well what do you mean?"
Multiplication is just an extension of addition, which is an extension of counting. If I defined multiplication as "the count of addition steps", that doesn't paint a good picture of what it's intended for.
And if you think about it as a single use case scenario... like getting the area of a square. That very much limits your understanding of its usefulness.
A dot product is as follows:
And there's a LOT to unpack there.
It has the 2 perspectives though for good reason...
The algebraic definition allows you to actually calculate it. Including inserting it into formula and reducing or expanding from there.
The geometric definition allows you to infer all sorts of information from it, which allows you to apply it in your use cases. May that be "to find the angle between something" or "if something intersects (separating axis theorem)" or "to find how far something intersects (think physics engines)" to "project a transformation into alternative coordinate systems" to much much more.