Search Unity

Cross Product

Discussion in 'Scripting' started by poolts, Oct 4, 2012.

  1. poolts

    poolts

    Joined:
    Aug 9, 2012
    Posts:
    113
    Hi all,

    I know that the cross product gives a vector perpendicular to two vectors (using the right or left hand rule). But what does the -1 or 1 returned by Unity actually mean? The direction or magnitude of the vector?

    This link gives some information, but I'm not really sure what the -1 or 1 indicates.

    http://docs.unity3d.com/Documentation/Manual/UnderstandingVectorArithmetic.html

    Also what is a useful example of the cross product? I'm sure there's 100's but the dot product seems to make a lot more sense (application wise) to me.

    So confused :confused:
     
    SamFernGamer4k likes this.
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,527
    What -1 or 1? Unity only returns a Vector3 when it performs a cross product.

    Dot products can potentially return a 1 or -1 (as well as several other values).

    And it's useless to say that dot product seems to make more sense application wise, over cross product. They both serve completely different functions.

    Dot products reduce vectors to a 1-dimensional values. These are used for projections (how much does something intersect, what's the angle of approach, what's the strength of something in a certain direction).

    Cross products create perpendiculars/orthogonals. They conserve the number of dimensions (3 in our case). These are used for creating new information.
    Cross products are useful for when you NEED that perpendicular, to calculate new data.

    I know forward and up, which way is right? I know two vectors on a plane, what is its normal? I have two vectors, and an angle between them (dot product), around what axis is that angle of rotation (cross product)?


    Dot product -
    read here: http://en.wikipedia.org/wiki/Dot_product

    Cross product -
    read here: http://en.wikipedia.org/wiki/Cross_product
     
  3. poolts

    poolts

    Joined:
    Aug 9, 2012
    Posts:
    113
    Thanks for the answer. I wasn't really saying that dot product makes more sense application wise, I was saying it made more sense to ME (that's why I wanted help filling in the blanks, so thanks for that).Thanks for the answer. I wasn't really saying that dot product makes more sense application wise, I was saying it made more sense to ME (that's why I wanted help filling in the blanks, so thanks for that).

    The 1 and -1 confusion was the diagram that Unity had at the bottom of that doc (see the link in the OP). Can you shed any light on that diagram? (Sorry not asking to be spoon fed, more so I want to get a full understanding)he 1 and -1 confusion was the diagram that Unity had at the bottom of that doc (see the link in the OP). Can you shed any light on that diagram? (Sorry not asking to be spoon fed, more so I want to get a fuller understanding).
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,527
    this image:



    It was the only one with a -1 in it.

    Yeah, that's to do with dot product, not cross product.

    And they're the cosine results. A dot product is equal the cosine of the angle between the to vectors.

    cos(theta) = a dot b

    for two unit vectors, whose dot product is -1, there is 180 degrees between them... because the cos(180degrees) = -1










    the 1 in this image is the sine. Sine of 90 degrees is 1, Sine of 180 degrees is 0.
     
    Last edited: Oct 4, 2012
  5. poolts

    poolts

    Joined:
    Aug 9, 2012
    Posts:
    113
    Sorry this image:



    It goes from 0 to 1. Is this just the dot product again, of how much the vector is projected onto the other?
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,527
    That I had in my post... but I guess the image didn't show up (bad text formatting).

    I said:

    The image is there because of the statement from your link:

    The magnitude of the resulting vector from a cross product. Is basically this:

    (a cross b).mag = a.mag * b.mag * sin(theta)

    where theta is the angle between the two vectors

    This is of use because usually when you cross two vectors you are using unit vectors (vectors whose mag is 1). Due to identity theorem this means you get: 1 * 1 * sin(theta)

    So really the cross of two UNIT vectors will result in a vector with magnitude sine(theta) (where theta is the angle between the first two).

    This means that if you've done a cross two things should come to mind real quick...

    1) you have to normalize it if you expect it to be a unit vector
    2) you don't have to do a dot product if you want the angle between them because the value is already there for you

    though this latter is inconsequential IMO, to get the magnitude you have to perform a square root. Where as a dot product is a sum of 3 products. The sum of 3 products will probably be faster... so from a practical point of view the magnitude sine(theta) doesn't help me much. BUT it's interested to know this.

    They then include that image there to show the relationships between sine results, and the corresponding angles. I guess whoever wrote that article thought that was a good graphic for describing the sine curve... I personally prefer this version of the graph of sine curve though:


    note - image is in radians, not degrees. 180 degrees == 2pi radians
     
  7. poolts

    poolts

    Joined:
    Aug 9, 2012
    Posts:
    113
    Awesome thanks a lot, you're definitely helped my understanding of the cross product!