Richardus Bouncing Balls script


#1

Here is the Bouncing Ball script i have demostrated last meeting.

http://assets.sqmv.nl/scripts/BouncingBalls.js


#2

The script link is not valid anymore. Here is the script.

//
//  BouncingBalls.js
//
//  Created by Richardus Raymaker on 15-09-2014.

//  Distributed under the Apache License, Version 2.0.
//  See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
// Thanks to chris for the help.

var amount=50;
var chambersize = 20;
var i = 0;
var sphereSize;
var sphereColorR;
var sphereColorB;
var sphereColorG;
var sphereVelocity;
var spherePosX;
var spherePosY;
var spherePosZ;

var pitch = 90.0;
var yaw = 0.0;
var roll = 180.0;
var rotation = Quat.fromPitchYawRollDegrees(pitch, yaw, roll)
var positionDelta = { x: 0.000, y: 0.000, z: -0.05 };

var entityID;
var originalProperties;
var entityArray = [];

var avatarPos;

function Chamber(mode,sPosX,sPosY,sPosZ)
{
    var x,y,z;
    
    print('chamber '+mode);
    //draw floor and ceiling.
    z=0;
    do
    {
        x=0;
        do
        {    
            if (mode=='SET')
            {
                Voxels.setVoxel(sPosX+x,sPosY-5,sPosZ+z, 1.0, 100, 100, 100);
                Voxels.setVoxel(sPosX+x,sPosY+5,sPosZ+z, 1.0, 100, 100, 100);
            }
            if (mode=='ERASE')
            {        
                Voxels.eraseVoxel(sPosX+x,sPosY-5,sPosZ+z, 1.0);    
                Voxels.eraseVoxel(sPosX+x,sPosY+5,sPosZ+z, 1.0);    
            }
            
            x++;
        }
        while (x < chambersize)    
        z++;
    }
    while (z < chambersize)        
    
    //Set walls
    y=0;
    do
    {
        x=0;
        do
        {    
            if (mode=='SET')
            {
                Voxels.setVoxel(sPosX+x,(sPosY-5)+y,sPosZ, 1.0, 100, 100, 100);
                Voxels.setVoxel(sPosX+x,(sPosY-5)+y,sPosZ+chambersize, 1.0, 100, 100, 100);
                
                Voxels.setVoxel(sPosX,(sPosY-5)+y,sPosZ+x, 1.0, 100, 100, 100);                
                Voxels.setVoxel(sPosX+chambersize,(sPosY-5)+y,sPosZ+x, 1.0, 100, 100, 100);
            }
            if (mode=='ERASE')
            {        
                Voxels.eraseVoxel(sPosX+x,(sPosY-5)+y,sPosZ, 1.0);    
                Voxels.eraseVoxel(sPosX+x,(sPosY-5)+y,sPosZ+chambersize, 1.0);    
                
                Voxels.eraseVoxel(sPosX,(sPosY-5)+y,sPosZ+x, 1.0);                    
                Voxels.eraseVoxel(sPosX+chambersize,(sPosY-5)+y,sPosZ+x, 1.0);    
            }
            
            x++;
        }
        while (x < chambersize)    
        y++;
    }
    while (y < 10)        
}

function placeAtoms(sPosX,sPosY,sPosZ)
{
    do
    {
        sphereSize=Math.random() * (1.0 - 0.5) + 0.5;
        
        sphereColorR=Math.random() * (255 - 128) + 128;
        sphereColorB=Math.random() * (255 - 128) + 128;
        sphereColorG=Math.random() * (255 - 128) + 128;
        
        sphereVelocity=Math.random() * (2.0 - 0.5) + 0.5;
        
        spherePosX=Math.random() * (5.0 - 2.0) + 1.0;        
        spherePosY=Math.random() * (5.0 - 2.0) + 1.0;    
        spherePosZ=Math.random() * (5.0 - 2.0) + 1.0;    
    
        originalProperties = {
            type: "Sphere",
            position: { x: sPosX+spherePosX,
                            y: sPosY+spherePosY,
                            z: sPosZ+spherePosZ },

            dimensions: {     x: 1.0,
                                y: 1.0,
                                z: 1.0 },            

            color: { red: sphereColorR,
                        green: sphereColorG,
                        blue: sphereColorB },
             
            rotation: rotation,
             
            velocity: { x: sphereVelocity,
                            y: 0.2,
                            z: -0.2 },
                             
            damping: -0.2,
             
            angulardamping: 0,
             
            mass: 1.000,
            ignoreForCollisions: false,
            collisionsWillMove: true,     
        };
        
        print("<"+sPosX+spherePosX+" , "+sPosX+spherePosY+" , "+sPosX+spherePosZ+">  <"+sphereColorR+" , "+sphereColorG+" , "+sphereColorB+">  <"+sphereVelocity+">  "+i);
        
        entityID = Entities.addEntity(originalProperties);        
        entityArray[i] = entityID;
        i++;
    }
    while (i < amount)        
}

function scriptEnding() 
{ 
    i=0;
    do
    {
        Entities.deleteEntity(entityArray[i]);
        i++;
    }
    while (i < amount)    
    Chamber('ERASE',avatarPos.x-(chambersize/2),avatarPos.y,avatarPos.z-(chambersize/2));    
}

avatarPos = MyAvatar.position;
Chamber('SET',avatarPos.x-(chambersize/2),avatarPos.y,avatarPos.z-(chambersize/2));

// NOTE: I changed this code here to wait until the chamber is built before placing the atoms... otherwise the atoms will 
// fly off without having chamber walls to collide into.
var atomsPlaced = false;
Script.update.connect(function()
{
    if (Voxels.packetsToSendCount() == 0 && !atomsPlaced) 
     {
        atomsPlaced = true;
        placeAtoms(avatarPos.x,avatarPos.y,avatarPos.z);
    }
});

Script.scriptEnding.connect(scriptEnding);