Difficulty understanding Sin()

Hello,

I am currently learning scripting using a third party course called “catlikecoding” - I have come across something which I am having difficulty understanding. The project involves creating a “graph” out of multiple prefab clones along an x axis and then forming different waveforms by using the x axis values to perform trigonomic functions and assigning each value to the Y for each prefab clone.

For creating a “moving ripple”, the course states that in order to animate the ripple, the input parameters for Sin() should be “float y = Sin(PI * (4f * x - Time. time));” I have tried writing it this way and it works, however writing it in the way “float y = Sin(PI * 4f * (x - Time.time));” creates the same shape but moving many times as fast.

The only reason I wrote it this way is because I was curious.

I am having difficulty understanding this, as I ran the calculations for both formulas manually and they both produce the exact same value, which I assume Sin() is reading in the same way. Am I missing something? I assume it has something to do with operational order maybe.

Here is a link to the course lesson I am studying - find the materials under the “how to create a ripple” part.

Thanks!

The two expressions are not the same. Just distribute and remove the brackets in both cases:

PI * (4f * x - Time. time) == PI * 4f * x - PI *Time. time

What you wrote expands to:

PI * 4f * (x - Time.time) == PI * 4f * x - PI * 4f *Time.time

So it seems you just have trouble with some basic maths here ^^

Just to clarify: the sine function takes in an angle given in radians. In radians 360° is equal to 2*PI. You don’t have to use PI in the equation at all. However it makes it easier to determine the “rotation speed” in terms of revolutions (or in the case of waves to determine the period)

Given the original equation PI * 4f * x - PI *Time. time we can see the angle changes “2 revolutions” per unit of x. He subtracts PI*Time. So we shift the waves by half a period per second. In your second case you multiply by an additional 4 so instead of half a period per second you will get 2 periods per second. So 4 times faster.

3 Likes

thanks a lot!

so it’s

Sin(PI * (4f * x - Time.time))

vs

Sin(PI * 4f * (x - Time.time))

if you just substitute the terms with something more familiar
(say PI is 9, x is 1, and Time.time is 5)

you get
9 * (4 * 1 - 5) = 9 * (4 - 5) = 9 * -1 = -9
vs
9 * 4 * (1 - 5) = 36 * (-4) = -144

pretty cheap way to confirm that it’s not the same thing, right?

you have to be mindful of both parentheses and ordinary operator precedence (multiplication takes precedence over addition/subtraction, just like in regular math)

Sin(PI * (4f * x - Time.time))

if you want to “pull out” that 4 out of parentheses, you have to make sure to maintain equilibrium
what messed it up is when you detached it from x, Time.time got subtracted from the wrong term

to make sure you maintain this balance, try looking at it from the math perspective

first you apply PI to both terms, then you extract the 4 * PI as a common factor (I am being verbose for clarity, obviously you can cut shortcuts)
the first term can be cleanly separated, but the other became PI * t, with no factors divisible by 4, so it has to be explicitly divided by 4 (simply put: (PI * t) / (4 * PI) = t / 4)

now let’s test this, admittedly odd-looking result :wink:

this is what we had
9 * (4 * 1 - 5) = 9 * (4 - 5) = 9 * -1 = -9

this is the final expression
4 * 9 * (1 - 5/4) = 36 * (-1/4) = (36 * -1) / 4 = -36 / 4 = -9

so this is what you wanted (though tbh there is nothing to be gained from this)
(in computation terms, it’s actually running a bit slower because of division)

Sin(PI * 4f * (x - Time.time / 4f))

aand we arrive to a conclusion already established by Bunny83