As — says the sine function returns a value between -1 and 1. The input to the function repeats every 2 Pi because of it's relation to Pi and a circle. However, if you tweak the input and output a little you can change them to the ranges you want
Firstly the input; if you wanted it to repeat every 20 seconds instead of every 6.28 ( 2 * Pi ) seconds what you need to do it "normalise" your input by dividing it by the desired period time / 20
this will give you a value between 0 and 1. Now you can multiply by the required period. (time / 20) * (2 * Pi)
or simplified (time / 10) * Pi
.
Secondly the output range. You only want a positive value, but there's actually 2 ways of doing this, depending on how you want it to behave. An easy option is to use absolute to convert it to a positive value like so abs(sin(time))
. But remember that sine is a curve, what you've effectually done is place a mirror along the x-axis, hence it will be rounded at the top and spiky at the bottom. In animation this gives you a bouncing ball effect. Accelerating as it falls, and decelerating as it climbs. The other option is to offset the curve so that it is always positive 1 + sin(time)
but this will change the range to between 0 and 2. We wanted an output of 0 to 500 anyway so we need half the output then scale it by 500 ((1+sin(time)) / 2) * 500
or if we simplify it (1+sin(time)) * 250
.
Let's combine those 2 parts together:
250 * (1 + sin((time / 10) * Pi))
When testing things like this it's useful to actually visualise the results on a graph. I recommend trying it out on a graphing tool like Desmos.
Here's some of the formulas for the expressions I mentioned, you should just be able to paste them into Desmos.
- \sin x
- \sin\left(\frac{x\ \cdot\ \pi}{10}\ \right)
- \left|\sin\left(x\right)\right|
- 1-\left|\sin\left(x\right)\right|
- \frac{\left(1\ +\ \sin\left(\frac{x\ \cdot\ \pi}{10}\ \right)\right)}{2}\
- \left(1\ +\ \sin\left(\frac{x\ \cdot\ \pi}{10}\ \right)\right)\ \cdot\ 5