- Home /
Using Sine Wave to control time
hello! i have my object that is moving in a sin wave with mathf function, that is this
float t = currentLerpTime / lerpTime;
t = Mathf.Sin(t * Mathf.PI * 0.5f);
i want something that start fast and finish slower so i think that the sine wave is what i'm looking for but i don't really know how to manipulate it to get what i looking for.. any help?
what i exactly want is something like this but without using AnimationCurve
Answer by metalted · Jun 19, 2019 at 01:49 PM
You can change the amplitude and period of a sine wave. The following page shows you how to change the period: https://www.dummies.com/education/math/trigonometry/adjusting-the-period-of-a-sine-function/ The amplitude of the sine wave is simple a multiplication of the outcome of the Sin calculation.
So as you show with the curve in the picture, you are looking for a logarithmic curve. Using the following formula will get you there: Vc = V * (1- e^(-t / R*C)).
Vc = Value on the curve after t-seconds
V = Maximum value of the graph (so 1.0)
e = constant (2.71...)
t = time in seconds
R*C / TAU = not really important what it means but you can control the curve with it. If you enter 1 here the curve will take 5 seconds to go from 0% to 100%, if you enter 2 here it will take twice as long.
In C# this formula will be:
float maxValue = 1.0f;
float timer = 1.0f // This is a timer or counter or something
float tauValue = 1.0f;
float value = maxValue * (1 - Mathf.Pow( (float) Math.E, (-timer / tauValue));
//I couldn't find E in the Mathf class so I had to add the System namespace to have Math.E
//This will be the code making use of the Mathf.Exp() function suggested by Bunny83
float maxValue = 1.0f;
float timer = 1.0f // This is a timer or counter or something
float tauValue = 1.0f;
float value = maxValue * (1 - Mathf.Exp(-timer/tauValue));
Some examples:
R*C = 1
t=1 Vc=0.63
t=2 Vc=0.86
t=3 Vc=0.95
t=4 Vc=0.98
t=5 Vc=0.99
R*C = 2
t=1 Vc=0.39
t=2 Vc=0.63
t=3 Vc=0.77
t=4 Vc=0.86
t=5 Vc=0.91
t=6 Vc=0.95
t=7 Vc=0.96
t=8 Vc=0.98
t=9 Vc=0.98
t=10 Vc=0.99
Hello! thank's you for your help! i updated my question to be more clear
The curve you are showing here is a curve from a logarithm. I use this curve in my field to calculate the discharge and charge times of a capacitor. We use the following formula for that:
Vc = V * (1- e^(-t / R*C))
So little explanation: Vc is the value after t-seconds. V is de full value, so in your case that will be 1 (0.0 - 1.0). e is the natural log and is a constant (2.71....). t is the time in seconds and R*C is the value that deter$$anonymous$$es the curve. In electronics this is mostly replaced by TAU which is a charge characteristic. The curve is always the same when you just replace the TAU (R*C), only the time is different. So the curve is always (for us) counted in TAU's. 1TAU will be 63%, 2TAU will be 81%, 3TAU will be 86% .... 5TAU will be 99%. 5TAU is in most cases seen as 100%.
So if I got it right, if you replace R*C / TAU with a 1, the curve will be 5 seconds, but I'm not exactly sure about that anymore.
You know that the FPU inside your CPU has a specialized method for calculating e^x? Specifically
Thank's you i'll learn it...for now that's is working well... thank's you!