Search Unity

Resolved issues understanding dot product output

Discussion in 'Scripting' started by ADNCG, Jun 12, 2022.

  1. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    I have a sine wave that varies in amplitude. I am trying to make a snowboard game like those old arcade game where if you hold the screen while the slope is going downwards, you gain momentum, but if you hold it while the slope is going upwards, you lose some.

    I'm trying to use the dot product of a preset vector ((1, 1).normalized) and the normals of the slope to determine whether or not to accelerate.

    Since (1, 1).normalized is diagonal top right, I figured that since 0.5 is my threshold, it'd allow 45 degrees on both sides of the vector, so anything between (0, 1) and (1, 0), exclusively in the first quadrant
    Code (CSharp):
    1. float dot = Vector2.Dot(new Vector2(1f, 1f).normalized, normals);
    2. print(normals + "    " + dot);
    but I'm getting outputs like these in the console
    I'm a bit confused. Afaik the dot product will return a value > 0 as long as the tested vector points towards the same hemisphere. Meaning 0.5 and up should cover exclusively the inner half of the hemisphere, no?

    edit : here's an attempt to illustrate my understanding
    - In green is my preset vector
    - In pink is what I believe would yield a value > 0 when doing the dot product of any vector starting from the origin and the preset vector
    - In yellow is what I believe would yield a value > 0.5 when doing the dot product of any vector starting from the origin and the preset vector

    For some reason, I am getting dot products > 0.5 a bit further than I believe I should. Purple zone illustrates this.

    Untitled.png

    edit2 - SOLVED : Well I don't understand this thoroughly so I'll just say what I actually understand, in a very non methodical way, in case anybody as clueless as me ever reads this. If anybody wants to add more to the topic, please do so :)

    I just found out from this source that you can't extrapolate what angles are covered linearly. Turns out the threshold for 45 degrees is 0.707, an oddly familiar number

    Also turns out I have to go back and fix a bunch of shaders which I never knew were working wrong until today :rolleyes:
     
    Last edited: Jun 12, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Trig functions are certainly not straight lines... That would mean ...well, circles would be really useless, more like diamonds I suppose...

    Always remember your trig helpers: SOH CAH TOA

    EDIT - thanks to ADNCG for pointing out my typo!

    Sine of 45 degrees is 0.707, eg 1 / sqrt(2)

    Tangent of 45 degrees is 1

    Sine of angles doesn't get less than 0.5 until you go below 30 degrees
     
    Last edited: Jun 12, 2022
    ADNCG likes this.