3D sine wave on an 8x8x8 led cube!

Hello everyone!
I recently made an 8x8x8 led cube with shift registers and am now making custom animations. I decided to start with trig functions like sine.
For the sin wave i used the following code:

int phase = 0

for(int x = 0; x < size; x++){
    for(int y = 0; y < size; y++){
        Z = sin(phase + sqrt(pow(map(x,0,size-1,-PI,PI),2) + pow(map(y,0,size-1,-PI,PI),2)));
        Z = round(map(Z,-1,1,0,size-1));
        setvoxel(x,y,Z);
    }
}

The line "setvoxel is a working function i used to set a point on my cube.

This code works fine for plotting one phase of the sine wave. However, I want to vary the phase in order to make the sine wave move up and down in a desirable pattern. I was instructed by the source below to vary the phase between 0 and 2PI but how do i do this in the code?

Source: HNTE - LED Cube

ANY help is appreciated!!! :slight_smile:

A simple linear interprelation with some step value over the range 2*M_PI

How would i do that? I have not learned much about trig---sorry

Wrap another loop around the X-Y loops you already have. Make it vary the variable phase over the range you want.

You probably also want to make phase a floating-point number. As an int, the phases 0, 1, 2 aren't very interesting.

const float FREQ = 1; // rotate phase one cycle per second

phase = millis() /1000.0 * PI * 2 * FREQ;

So the finished animation would look like this:?
(times is the # of times to repeat the animation)

int phase = -1.5

for(int v=0; v<times;v++)
{
    for(int i=0; i<6; i++)
    {
        phase=phase + 0.5;

        for(int x = 0; x < size; x++){
            for(int y = 0; y < size; y++){
                Z = sin(phase + sqrt(pow(map(x,0,size-1,-PI,PI),2) + pow(map(y,0,size-1,-PI,PI),2)));
                Z = round(map(Z,-1,1,0,size-1));
                setvoxel(x,y,Z);
            }
    }


    for(int i=0: i<6; i++)
    {
        phase=phase - 0.5;

        for(int x = 0; x < size; x++){
            for(int y = 0; y < size; y++){
                Z = sin(phase + sqrt(pow(map(x,0,size-1,-PI,PI),2) + pow(map(y,0,size-1,-PI,PI),2)));
                Z = round(map(Z,-1,1,0,size-1));
                setvoxel(x,y,Z);
            }
    }
}