Code viewer for World: Rainbow Cube
// Cloned by Niveditha Vudayagiri on 18 Sep 2024 from World "One Cube World (P5)" by Starter user 
// Please leave this clone trail here.
 
//Add Music Here
const MUSICFILE = '/uploads/lauracampbell26/gamemusic-6082.mp3';
AB.backgroundMusic ( MUSICFILE );

const objectsize    = 35;      // size of object   
const anglechange   = 0.01;     // how much the rotate angle changes each step 
var img;
var angle = 0;
let bg;
let y=0;

// make an array of random (x,y,z) positions 

const noboxes = 50;                 // number of cubes
var a = new Array(noboxes);                    // array for positions
var angles = new Array(noboxes);             // array for individual angles

// Set up arrays with random positions and angles for each cube
for (let i = 0; i < noboxes; i++) {
  a[i]=[AB.randomIntAtoB(-500, 500), AB.randomIntAtoB(-500, 500), AB.randomIntAtoB(-500, 500)];
  angles[i] = [AB.randomIntAtoB(0, 180),AB.randomIntAtoB(0, 180),AB.randomIntAtoB(0, 180)];
}	

function preload() 
{
   img = loadImage('/uploads/nivedithavudayagiri/rainbow.jpg');
}

function setup()        // "setup" is called once at start of run 
{
  createCanvas ( ABWorld.fullwidth(), ABWorld.fullheight(),  WEBGL );
}

function draw()         // "draw" is called every timestep during run 
{
   background('SkyBlue');    // background color 
      
    //fill("MediumPurple");     // paint box with this color 
    texture(img);
    
    for (let i = 0; i < noboxes; i++) {
    push();
    // Set the position for each cube
    translate(a[i][0], a[i][1], a[i][2]);

    // Apply independent rotation for each cube
    rotateX(angles[i][0]);
    rotateY(angles[i][0]);
    rotateZ(angles[i][0]);
    
    // Draw the cube
    box(objectsize);

    // Update angles for the next frame to create movement
    angles[i][0] += anglechange;
    angles[i][1] += anglechange;
    angles[i][2] += anglechange;
    
    pop();
  }
}