• | **having a hard time understanding this expression.**
on Jul 9, 2018 at 6:19:48 pm Last Edited By Assaf Goldlust on Jul 9, 2018 at 7:54:31 pm |

the idear of this expressions

is to make a layer Y position bounce from 100 to 0 to -100

and make that bounce in a frequency of 5 times per second

but im having a hard time understanding the math behind that? is any around here can help me out?

var frequency=5;

var amplitude=100;

var x=transform.position[0];

var y=transform.position[1]+(amplitude*Math.sin(frequency*time*2*Math.PI));

[x,y]

• | **Re: having a hard time understanding this expression.** on Jul 10, 2018 at 7:05:07 am |

The main part is obviously this:

(amplitude*Math.sin(frequency*time*2*Math.PI))

The main thing to understand is the sine function (Math.sin), which gives you the y value from a point rotating on a unit circle (circle with the radius of one) at a given angle in relation to the positive x axis drawn from the center of the circle. Time is used to change the angle. Math.PI is pi, i.e. the relationship of the circumference of a circle to its diameter. Since pi means one full rotation, you are getting the y value of the rotating point based on time. When frequency is one, that's one rotation per second. Multiplied by frequency you get that many rotations. The multiplication by 2 in that formula is an unnecessary addition, it just doubles the frequency.

transform.position[0] retrieves the x position of the layer, [1] the y position. The [0] and [1] are array indices.

For a visual of the unit circle, see

https://en.wikipedia.org/wiki/Sine
Kalleheikki Kannisto

Senior Graphic Designer

• | **Re: having a hard time understanding this expression.** on Jul 11, 2018 at 8:16:14 am |

im a little confused? according to the book im working and also inside after it tell me that PI is half a circle...

• | **Re: having a hard time understanding this expression.** on Jul 11, 2018 at 9:31:51 am |

Pi is half a circle if you multiply the *radius* with it, full circle if you multiply the *diameter*. Diameter = 2 * radius.

Kalleheikki Kannisto

Senior Graphic Designer

• | **Re: having a hard time understanding this expression.** on Jul 11, 2018 at 9:46:53 am |

now its make sense. the Math.PI in this case is radius and not the diameter.

this the "*2" opertation is included

• | **Re: having a hard time understanding this expression.** on Jul 11, 2018 at 9:50:40 am Last Edited By Kalleheikki Kannisto on Jul 11, 2018 at 9:52:49 am |

Yes, sorry, I forgot that Javascript, which Expressions are based on, uses radians as units for this calculation, so you're right in that a full circle is indeed 2*PI when measured in radians.

Kalleheikki Kannisto

Senior Graphic Designer

• | **Re: having a hard time understanding this expression.** on Jul 11, 2018 at 10:30:35 am |

dont worry about it, you were still a great help. thank you!

• | **Re: having a hard time understanding this expression.** on Jul 11, 2018 at 9:32:54 pm |

here is something that gave me a bit of a head screather. im assiming time is changing the angle of the expression by rotating the point in at the center of the unit circle, but then im ising 2*Math.PI to tell him how many rotation to do per 1 second.

but if time is changing the angle by making the point rotate and the frequency make it go faster, dosn't have any kind of effect of how many complete rotation he does in a second?

• | **Re: having a hard time understanding this expression.** on Jul 11, 2018 at 10:13:31 pm |

The 2*Math.PI just converts the number of rotations to radians. The current number of rotations is determined by frequency*time. So for example, when frequency is 5, and time is 2, that's 10 complete rotations, so the total angle is 20*Math.PI radians.

Dan

• | **Re: having a hard time understanding this expression.** on Jul 11, 2018 at 10:26:43 pm |

i tought that Math.SIn treat already treat any number you feed him as a radian.

• | **Re: having a hard time understanding this expression.** on Jul 12, 2018 at 12:03:44 am |

That's correct.

• | **Re: having a hard time understanding this expression.** on Jul 12, 2018 at 12:51:53 am |

so... what is that you are converting if its already radians?

• | **Re: having a hard time understanding this expression.** on Jul 12, 2018 at 1:41:56 am |

Math.sin() expects radians, but you have to actually give it radians if you want the right answer.

Dan

• | **Re: having a hard time understanding this expression.** on Jul 12, 2018 at 1:52:15 am |

i dont understand the answer.

i will refraze my question:

you said that Math.PI is ment to convert the number of rotations to radians.

but if i feed Math.Sin are being treated as radians from the frist place what is there to convert, arent those numbers are already radians?

• | **Re: having a hard time understanding this expression.** on Jul 12, 2018 at 3:17:13 am |

Math.sin will assume whatever you feed it is expressed in radians. That doesn't imply that Math.sin converts it to radians. It will assume you have already done that. I'm not sure how else to explain it.

Dan

• | **Re: having a hard time understanding this expression.** on Jul 12, 2018 at 3:49:09 am |

that was good enough, thank you