Reputation: 9566
I want to animate a sine wave as if it's generated from the center and moves outwards (to the left and right).
I started with this library: SISinusWaveView and made some adjustments. Currently I have this naive code for calculating the Y position of the curve depending on the X, phase and frequency:
float width = view.width; float mid = width / 2;
float adjustedX = x < mid ? x : width - x;
float y = maxAmplitude * sinf(2 * M_PI *(adjustedX / mid) * frequency + phase);
// phase increases every frame
Quite obviously, this causes a sharp angle at the middle of the sine wave, as seen below:
I would like to make it so the horizontal center of the animation is a smooth curve rather than a sharp angle, while keeping the animation horizontally symmetric. How would I approach this? Any mathematical insight for achieving this is appreciated.
EDIT
I tried to implement @TheBigH suggestion, but the parabola section is not seamlessly continuing the sine curve. Here's what I tried (implemented on Mathematica for quick visualization):
amp = 10;
freq = 1.5;
phase = 0.5;
Z = 1;
plotSine = Plot[amp*Sin[freq*x + phase], {x, Z, 2 Pi}];
aPara = amp*freq*Cos[phase]/(2 Z);
bPara = 0;
cPara = amp*Sin[c] - aPara*Z^2;
plotPara =
Plot[aPara*x^2 + bPara*x + cPara, {x, -Z, Z },
PlotRange -> {{-Z, Z}, {-20, 20}}];
Show[plotPara, plotSine, PlotRange -> {{-2 Pi, 2 Pi}, {-20, 20}}
Which results in this:
Changing the sign of the parabola didn't quite work either:
EDIT 2
I see now that the problem was assuming s(0) = p(Z)
and s'(0) = p'(Z)
; instead of s(z) = p(Z)
and s'(Z) = p'(Z)
. Moving the sine wave so it would start exactly at the end of the parabole would fix the problem, but it's more convenient solving the parabola such as s(z) = p(Z)
and s'(Z) = p'(Z)
as that would simplify the implementation. How to do this?
EDIT 3
See this math.stackexchange.com answer for the final solution.
Upvotes: 4
Views: 1183
Reputation: 554
Since OP has asked me to elaborate, here's my take as an answer.
Most generally, you're plotting the function s(x) = a sin(bx + c)
, where a
, b
and c
come from the original problem. Later we will shift the sine curve by some offset Z but I'll leave it out of the sine curve for now as it will complicate the mathematics.
The new parabolic section will have equation p(x) = Ax^2 + Bx + C
(A
, B
and C
are different variables than a
,b
and c
).
You want the two equations to join up cleanly, which means s(0) = p(Z)
. You also want the slopes to join up nicely so that there are no corners. That means you also need s'(0) = p'(Z)
. Also, since the parabola is centered about the origin, B = 0
.
Thus you have two simultaneous equations for A
, C
given that you already know a
, b
, c
and Z
a sin( c ) = A Z^2 + C
ab cos( c ) = 2AZ
or
A = ab cos( c ) / (2Z)
C = a sin (c) - A Z^2
This gives you the equation of the parabola, which you plot between -Z
and Z
. Then all you have to do is plot the sine curves, now adding in that offset. Let me know if any of this is unclear.
EDIT: I see there is a vertical shift of the sine wave as well. This does not pose any problems; just leave it out to begin with and add it to the parabola and sine waves at the end.
Upvotes: 2