Search Unity

Sine Waves

Discussion in 'Scripting' started by Clydey2Times, Aug 21, 2018.

  1. Clydey2Times

    Clydey2Times

    Joined:
    Oct 24, 2017
    Posts:
    242
    Hi all,

    I'm following a tutorial that is currently discussing sine waves. I'm not sure I fully understand it, though.

    Does a sine wave repeat after tau radians? I ask because I'm trying to understand how a sine wave works when the angle goes above tau radians. Does my question make sense? Apologies, I'm still trying to get to grips with the concept. It's been a long time since I've had to learn any kind of math.
     
  2. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    The sine wave does in fact repeat after tau radians.

    The sine function is the function that describes the vertical height if you took a radius and rotated it by x degrees on the unity circle. At sin(0) you are basically asking what is the y value of a unity circle who's radian has been rotated by 0 degrees (or perfectly flat, so it's 0.) At 45 degrees, that radian intersets the unity circle at exactly a diagonal, and lands on the y-axis at 0.5. Thats why when you rotate the radian 90 degrees and it's pointing straight up, sin(90) is exactly 1. If you think of the sinewave like that, it'll be obvious why it repeats after tau, because you can just keep rotating around that circle.
     
  3. Clydey2Times

    Clydey2Times

    Joined:
    Oct 24, 2017
    Posts:
    242
    Thanks for the response. So when a value goes above tau radians, it's equivalent to an angle going beyond 360? In essence, it can't happen and the angle resets?
     
  4. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Yes, tau radians is exactly 360 degrees. One radian is the angle created if you take the radius and paste it along the circumstance of the circle. That’s why the formula for the circumference of a circle is tau * radius.
     
  5. Clydey2Times

    Clydey2Times

    Joined:
    Oct 24, 2017
    Posts:
    242
    Thanks. Much p
    Thanks. Much appreciated.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
  7. Clydey2Times

    Clydey2Times

    Joined:
    Oct 24, 2017
    Posts:
    242
    Hey all. Just another question on this topic. I'm trying to rotate with sine.

    If use the following code, x rotates between 45 and -45:

    Code (CSharp):
    1.      public float speed = 2f;
    2.          public float maxRotation = 45f;
    3.    
    4.          void Update()
    5.          {
    6.              transform.rotation = Quaternion.Euler(maxRotation * Mathf.Sin(Time.time * speed), 0f, 0f);
    7.          }
    However, if I try to change it to rotate around z with the following code, it rotates between 1 and -1:

    Code (CSharp):
    1.      public float speed = 2f;
    2.          public float maxRotation = 45f;
    3.    
    4.          void Update()
    5.          {
    6.              transform.rotation = Quaternion.Euler(maxRotation * 0f, 0f, Mathf.Sin(Time.time * speed));
    7.          }
    Can anyone explain why in one case it returns the angle and in the other case it returns the sine?
     
  8. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Mathf.Sin always returns between 1 and -1, look at where your multiplier is in your code.
     
  9. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Simple cheat sheet:

    sin( t * frequency ) * amplitude


    You forgot to move maxRotation when you moved the Sin call.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    The expression "maxRotation * 0f" will always be zero.

    Move your maxRotation term along with your sine expression over to the Z argument.

    Better yet, get out of the awful habit of making ginormous compound math statements and ending up with this sort of bug. It's so easy to break it down so you can print each step out:

    Code (csharp):
    1. float effectiveTime = Time.time * speed;
    2.  
    3. float computedSine = Mathf.Sin( effectiveTime);
    4.  
    5. float scaledAngleOfRotation = computedSine * maxRotation;
    6.  
    7. transform.rotation = Quaternion.Euler( scaledAngleOfRotation, 0, 0);
    Just get in the habit of doing that, the compiler will generate almost precisely the same exact code, and now you will actually be able to reason about what you are doing in a stepwise function rather than cramming it all on one giant eyeball-scorching line and scratching your head for hours and hours. :)
     
  11. Clydey2Times

    Clydey2Times

    Joined:
    Oct 24, 2017
    Posts:
    242
    Ah, thank you. Silly oversight on my behalf.