Code viewer for World: Spaghett

// Cloned by Theo Delettre on 30 Sep 2021 from World "One Cube World (P5)" by Starter user 
// Please leave this clone trail here.

const universeSize = 700;       // size of the box all objects are restrained to

const anglechange = 0.002;     // how much the rotate angle changes each step 

var angle = 0;                  // rotate angle starts at 0  

var img;

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

const noboxes = 500;                 	// how many boxes to have 

var positions = new Array(noboxes);     // array of object positions
var dimensions = new Array(noboxes);    // array of object dimensions and characteristics    
var shapeTypes = new Array(noboxes);    // array of object shape types (box, sphere, or cone)
var velocities = new Array(noboxes);    // array of object velocities
var accelerations = new Array(noboxes); // array of object accelerations

 //const MUSICFILE = '/uploads/theonogo/MickGordon-BFGDivision1.mp3';
 //AB.backgroundMusic ( MUSICFILE );

for ( var i=0; i < noboxes; i++ )   	// set up the arrays
{
    positions[i] = [ AB.randomIntAtoB(-20,20), AB.randomIntAtoB(-20,20), AB.randomIntAtoB(-20,20)];
    dimensions[i] = [ AB.randomIntAtoB(2,40), AB.randomIntAtoB(2,40), AB.randomIntAtoB(2,40), AB.randomIntAtoB(6, 16), AB.randomIntAtoB(3,12)];
    shapeTypes[i] = AB.randomIntAtoB(0, 2);
    velocities[i] = [ AB.randomFloatAtoB(-0.5,0.5), AB.randomFloatAtoB(-0.5,0.5), AB.randomFloatAtoB(-0.5,0.5)]
    
}	


function setup()        // "setup" is called once at start of run 
{
  createCanvas ( ABWorld.fullwidth(), ABWorld.fullheight(),  WEBGL );
  img = loadImage('/uploads/theonogo/1632994832.png');
}

function draw()         // "draw" is called every timestep during run 
{
    background("LightSteelBlue");   // background color 
    texture(img);                   // set objects texture
           
    //rotateX(-angle/10);             // set each dimension rotation angle to "angle"
    rotateY(angle);
    //rotateZ(angle/10);
    
    for ( var i=0; i < noboxes; i++ )
    {
        accelerations = [ AB.randomFloatAtoB(-0.02,0.02), AB.randomFloatAtoB(-0.02,0.02), AB.randomFloatAtoB(-0.02,0.02) ]  //get random acceleration
        
        velocities[i] = [ accelerations[0] + velocities[i][0], accelerations[1] + velocities[i][1], accelerations[2] + velocities[i][2] ]; //velocity change = acceleration
        
        positions[i] = [ positions[i][0] + velocities[i][0], positions[i][1] + velocities[i][1], positions[i][2] + velocities[i][2] ]; //position change = velocity
        
        translate ( borderProtect(positions[i][0]), borderProtect(positions[i][1]), borderProtect(positions[i][2]) );	//move boxes to new positions
        
        switch(shapeTypes[i]) {     //pick randomly decided shape
            case 0:
                box(dimensions[i][0], dimensions[i][1], dimensions[i][2]);
                break;
            case 1:
                sphere(dimensions[i][0]/2, dimensions[i][3],dimensions[i][4] );
                break;
            default:
                cone(dimensions[i][0],dimensions[i][1], dimensions[i][3],dimensions[i][4] );   
        }
        
        
        translate ( -borderProtect(positions[i][0]), -borderProtect(positions[i][1]), -borderProtect(positions[i][2]) );    //reset placement
    }

    angle = angle + anglechange ;       // change angle each step to get rotate movement
}

function borderProtect(x){      //adjusts position to be within universeSize borders and "bounce" off universe edges
    m5 = (x/universeSize|0) %2;
    m10 = (x/(universeSize*2)|0) %2;
    res = x%universeSize;
    
    if(x>universeSize){
        if (m5 === 1){
            res = universeSize - res;
        }
        
        if(m10 === 1){
            res = -res;
        }
    } else if(x<-universeSize){
        if( abs(m5) === 1 ){
            res = -universeSize - res;
        }
        
        if(abs(m10) === 1){
            res = -res;
        }
    }
    
    return res;
}