Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

What Is Vector3.Dot?

Discussion in 'Scripting' started by Nanior, Jul 12, 2019.

  1. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    I don't really understand Vector3.Dot
    No description further...
     
    Last edited: Jul 15, 2019
  2. Team2Studio

    Team2Studio

    Joined:
    Sep 23, 2015
    Posts:
    97
    Do not try and bend Mathf.Dot... that's impossible... there is no Mathf.Dot
     
  3. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    3,497
  4. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    290
    Dot is short for dot product, which is a vector math function. It doesn't exist in Mathf tho, it's Vector3.Dot.
     
  5. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,994
    Also Quaternion.Dot iirc.
     
  6. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    Sorry, its Vector.Dot
     
  7. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    3,497
  8. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,016
    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!
     
  9. victus_maestro

    victus_maestro

    Joined:
    Apr 21, 2019
    Posts:
    17
    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).
     
  10. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,016
    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.
     
    davidnibi likes this.
  11. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    7,153
    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:
    Code (csharp):
    1. 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?

    Well... Transform.localToWorldMatrix:
    https://docs.unity3d.com/ScriptReference/Transform-localToWorldMatrix.html

    Transform.worldToLocalMatrix:
    https://docs.unity3d.com/ScriptReference/Transform-worldToLocalMatrix.html

    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.
     
unityunity