1

I need a function to animate a sine wave infinitely over time. The sine wave moves to the left.

My sine wave is built using the following equation:

A * sin(B * x + C) + D

Now to animate the sine wave as if it is moving to the left, I simply increase C by 1 everytime I refresh the screen. Now this is all fine and dandy for a few minutes but I need to have that animation run for hours. I can't just have an integer build up 60 times a second forever. How does someone deal with this? Do I just try to find a point where the sine wave crosses 0 and then restart the animation from 0?

I just need to have the logic of something like this explained.

EDIT #1 I forgot to mention that there's a randomized component to my sine. The sine is not continuously the same. A and D are sinusoidal functions tied to that integer at the moment. The sine needs to look random with varying periods and amplitudes.

EDIT #2

Edited see Edit 3

EDIT #3

@Potatoswatter I tried implementing your technique but I don't think I'm getting it. Here's what I got:

        static double i = 0;
        i = i + (MPI / 2); 
        if ( i >= 800 * (MPI / 2) ) i -= 800 * (MPI / 2);

                    for (k = 0; k < 800; ++k)
        {
            double A1 = 145 * sin((rand1 * (k - 400) + i) / 300) + rand3;       // Amplitude
            double A2 = 100 * sin((rand2 * (k - 400) + i) / 300) + rand2;       // Amplitude
            double A3 = 168 * sin((rand3 * (k - 400) + i) / 300) + rand1;       // Amplitude

            double B1 = 3 + rand1 + (sin((rand3 * k) * i) / (500 * rand1));     // Period
            double B2 = 3 + rand2 + (sin((rand2 * k) * i) / 500);           // Period
            double B3 = 3 + rand3 + (sin((rand1 * k) * i) / (500 * rand3));     // Period

            double x = k;                               // Current x

            double C1 = 10 * i;                         // X axis move
            double C2 = 11 * i;                         // X axis move
            double C3 = 12 * i;                         // X axis move

            double D1 = rand1 + sin(rand1 * x / 600) * 4;               // Y axis move
            double D2 = rand2 + sin(rand2 * x / 500) * 4;               // Y axis move
            double D3 = rand3 + cos(rand3 * x / 400) * 4;               // Y axis move


            sine1[k] = (double)A1 * sin((B1 * x + C1) / 400) + D1;
            sine2[k] = (double)A2 * sin((B2 * x + C2) / 300) + D2 + 100;
            sine3[k] = (double)A3 * cos((B3 * x + C3) / 500) + D3 + 50;

        }

How do I modify this to make it work?

Halp!

0

3 Answers 3

2

Sine has a period of 2 pi, meaning that sin(x) = sin(x + 2 * M_PI), for any x.

So, you could just increase C by, say, pi/n where n is any integer, as you refresh the screen, and after 2n refreshes, reset C (to 0, or whatever).

Edit for clarity: the integer n is not meant to change over time.

Instead, pick some n, for example, let's say n = 10. Now, every frame, increase x by pi / 10. After 20 frames, you have increased x by a total of 20 * pi / 10 = 2 * pi. Since sin(x + 2 * pi) = sin(x), you may as well just reset your sin(...) input to just x, and start the process over.

5
  • Thanks for your answer. Unfortunately my math skills are slightly lacking so I'm wondering if you could clarify your answer a bit. Wouldn't increasing C by pi / n where n is a continuously growing integer slow the animation down over time since the result returned would grow increasingly smaller?
    – ReX357
    Dec 23, 2013 at 8:19
  • Or you mean like n is a constant here? For instance 4800 and then reset to zero after 9600 frames?
    – ReX357
    Dec 23, 2013 at 8:26
  • @ReX357 yes, exactly. Dec 23, 2013 at 8:27
  • -1, Moving by exactly pi/n is unnecessarily restrictive. There's no need to require him to change the animation speed. Dec 23, 2013 at 8:31
  • Does it need to be 2n or it can be any even number of n? ie: 4n, 6n, 8n, etc.
    – ReX357
    Dec 23, 2013 at 8:32
0

sin is periodic, with a period of 2π. Therefore, if the argument is greater than 2π, you can subtract 2 * M_PI from it and get the same answer.

Instead of using a single variable k to compute all waves of various speeds, use three variables double k1, k2, k3, and keep them bound in the range from 0 to 2π.

if ( k2 >= 2 * M_PI ) k2 -= 2 * M_PI;

They may be individually updated by adding some value each frame. If the increment may be more than 2π then subtracting a single 2π won't bring them back into range, but you can use fmod() instead.

12
  • I see what you mean. However, if you notice, my C (x shift) is tied to variable i not k. All the sines have sinusoidal equations to define A and D so it looks very random over time. I tried the solution that said to increase i by pi / n and then resetting after a multiple of 2n but it simply skips to a totally different set of waves when I wrap back to 0. Does this still apply to a wave with varying amplitude and y shift over time?
    – ReX357
    Dec 23, 2013 at 9:06
  • To elaborate a little more on k, my view port is 800 pixels wide. I iterate through an array that's 801 values long (Extra value is cause I run a line_to function to draw the line) where each value is y. K is simply x position in my line basically. i is elapsed time basically. Gets updated 60 times a second.
    – ReX357
    Dec 23, 2013 at 9:10
  • @ReX357 I get it. My best-practices advice would be to keep everything clipped into [0, 2π) because as floating-point numbers grow they lose precision. So, keep three versions of i, and use them to initialize three versions of k, and constrain all the i's and k's in both the inner and outer loops. Dec 23, 2013 at 9:13
  • I'm sorry I keep quizzing you, I'm just trying to wrap my head around the whole thing here. I still don't understand how I need to initialize 3 versions of k? k is 0 to 800 no matter what. It represents 1 pixel no matter what. I use it in my equations to create some movement in the graph. Are you saying I should leave it out of the equations and simply use i where i is between 0 and 2pi?
    – ReX357
    Dec 23, 2013 at 9:22
  • @ReX357 You can increment k1, k2, k3 800 times, and each increment represents one pixel. I'm saying to calculate the increment, and add that much to each k rather than calculating everything from a single integer which tends to grow. But of course you can also have a separate int k if that's what the graphics library expects. Anyway… When things get confusing, I like to write down the equation in comments and simplify using basic algebra. Dec 23, 2013 at 9:30
0

I decided to change my course of action. I just drive i with the system's monotonic clock like so:

        struct timespec spec;
        int ms;
        time_t s;
        static unsigned long long etime = 0;

        clock_gettime(CLOCK_MONOTONIC, &spec);
        s = spec.tv_sec;
        ms = spec.tv_nsec / 10000000;
        etime = concatenate((long)s, ms);

Then I simply changed i to etime in my sine equations. Here's the concatenating function I used for this purpose:

unsigned concatenate(unsigned x, unsigned y) {
    x = x * 100;
        return x + y;        
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.