hlxldb

LED光立方制作全过程(三十八)

0
阅读(2096)

Step 59Software: Effect 3, sendvoxels random Z

 

This effect sends voxels up and down the Z axis, as the implies.

void sendvoxels_rand_z() takes three arguments. Iterations is the number of times a voxel is sent up or down. Delay is the speed of the movement (higher delay means lower speed). Wait is the delay between each voxel that is sent.

This is how it works:

1) The cube is cleared with fill(0x00);

2) Loop through all 64 positions along X/Y and randomly set a voxel at either Z=0 or Z=7.

3) Enter the main iteration loop

4) Select random coordinates for X and Y between 0 and 7. If the X and Y coordinates are identical to the previous coordinates, this iteration is skipped.

5) Check if the voxel at this X/Y coordinate is at Z=0 or Z=7, and send it to the opposite side using sendvoxel_z().

6) Delay for a while and save the coordinates of this iteration so we can check them against the random coordinates in the next iteration. It looked weird to move the same voxel twice in a row.


The actual movement of the voxels is done by another function, sendvoxel_z. The reason for this, is that a couple of other effects does the same thing only in different ways.


The function sendvoxel_z() takes four argument. X and Y coordinates. Z coordinate, this is the destination and can either be 0 or 7. Delay which controls the speed.

This is how it works:

1) For()-loop i from 0 to 7.

2) If the destination is 7, we set ii to 7-1, thus making ii the reverse of i. Clear the voxel at Z = ii+1. When moving down, ii+1 is the previous voxel.

3) If the destination is 0, let ii be equal to i. Clear ii-1. When moving upwards, -1 is the previous voxel.

4) Set the voxel at z=ii.

5) Wait for a while.