From 8f9a99e1f44f8ad76d3c2c67338e5b9f9673b892 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 31 Dec 2013 12:13:06 -0800 Subject: [PATCH] add example scripts --- examples/collidingParticles.js | 149 +++++++++++++++++++++++++++++++++ examples/gun.js | 98 ++++++++++++++++++++++ 2 files changed, 247 insertions(+) create mode 100644 examples/collidingParticles.js create mode 100644 examples/gun.js diff --git a/examples/collidingParticles.js b/examples/collidingParticles.js new file mode 100644 index 0000000000..641dd1a4ec --- /dev/null +++ b/examples/collidingParticles.js @@ -0,0 +1,149 @@ +var currentIteration = 0; +var NUM_ITERATIONS_BEFORE_SEND = 15; // every 1/4th seconds send another + +var numberParticlesAdded = 0; +var MAX_PARTICLES = 1; + +var velocity = { + x: 1/TREE_SCALE, + y: 0/TREE_SCALE, + z: 1/TREE_SCALE }; + +var gravity = { + x: 0/TREE_SCALE, + y: 0/TREE_SCALE, + z: 0/TREE_SCALE }; + +var damping = 0.1; + +var scriptA = " " + + //" function update() { " + + //" print('update()\\n'); " + + //" var color = { red: 255, green: 127 + (Math.random() * 128), blue: 0 };" + + //" Particle.setColor(color); " + + //" } " + + " function collisionWithParticle(other) { " + + " print('collisionWithParticle(other.getID()=' + other.getID() + ')...'); " + + " print('myID=' + Particle.getID() + '\\n'); " + + " var colorBlack = { red: 0, green: 0, blue: 0 };" + + " var otherColor = other.getColor();" + + " print('otherColor=' + otherColor.red + ', ' + otherColor.green + ', ' + otherColor.blue + '\\n'); " + + " var myColor = Particle.getColor();" + + " print('myColor=' + myColor.red + ', ' + myColor.green + ', ' + myColor.blue + '\\n'); " + + " Particle.setColor(otherColor); " + + " other.setColor(myColor); " + + " } " + + " function collisionWithVoxel(voxel) { " + + " print('collisionWithVoxel(voxel)... '); " + + " print('myID=' + Particle.getID() + '\\n'); " + + " var voxelColor = voxel.getColor();" + + " print('voxelColor=' + voxelColor.red + ', ' + voxelColor.green + ', ' + voxelColor.blue + '\\n'); " + + " var myColor = Particle.getColor();" + + " print('myColor=' + myColor.red + ', ' + myColor.green + ', ' + myColor.blue + '\\n'); " + + " Particle.setColor(voxelColor); " + + " } " + + " Particle.collisionWithParticle.connect(collisionWithParticle); " + + " Particle.collisionWithVoxel.connect(collisionWithVoxel); " + + " "; + +var scriptB = " " + + " function collisionWithParticle(other) { " + + " print('collisionWithParticle(other.getID()=' + other.getID() + ')...'); " + + " print('myID=' + Particle.getID() + '\\n'); " + + " var myPosition = Particle.getPosition();" + + " var myRadius = Particle.getRadius();" + + " var myColor = Particle.getColor();" + + " Voxels.queueDestructiveVoxelAdd(myPosition.x, myPosition.y, myPosition.z, myRadius, myColor.red, myColor.green, myColor.blue); " + + " Particle.setScript('Particle.setShouldDie(true);'); " + + " } " + + " function collisionWithVoxel(voxel) { " + + " print('collisionWithVoxel(voxel)... '); " + + " print('myID=' + Particle.getID() + '\\n'); " + + " var voxelColor = voxel.getColor();" + + " print('voxelColor=' + voxelColor.red + ', ' + voxelColor.green + ', ' + voxelColor.blue + '\\n'); " + + " var myColor = Particle.getColor();" + + " print('myColor=' + myColor.red + ', ' + myColor.green + ', ' + myColor.blue + '\\n'); " + + " Particle.setColor(voxelColor); " + + " } " + + " Particle.collisionWithParticle.connect(collisionWithParticle); " + + " Particle.collisionWithVoxel.connect(collisionWithVoxel); " + + " "; + +var color = { + red: 255, + green: 255, + blue: 0 }; + +function draw() { + print("hello... draw()... currentIteration=" + currentIteration + "\n"); + + // on the first iteration, setup a single particle that's slowly moving + if (currentIteration == 0) { + var colorGreen = { red: 0, green: 255, blue: 0 }; + var startPosition = { + x: 2/TREE_SCALE, + y: 0/TREE_SCALE, + z: 2/TREE_SCALE }; + var largeRadius = 0.5/TREE_SCALE; + var verySlow = { + x: 0.01/TREE_SCALE, + y: 0/TREE_SCALE, + z: 0.01/TREE_SCALE }; + + Particles.queueParticleAdd(startPosition, largeRadius, colorGreen, verySlow, gravity, damping, false, scriptA); + print("hello... added particle... script=\n"); + print(scriptA); + numberParticlesAdded++; + } + + if (currentIteration++ % NUM_ITERATIONS_BEFORE_SEND === 0) { + print("draw()... sending another... currentIteration=" +currentIteration + "\n"); + + var center = { + x: 0/TREE_SCALE, + y: 0/TREE_SCALE, + z: 0/TREE_SCALE }; + + var particleSize = 0.1 / TREE_SCALE; + + print("number of particles=" + numberParticlesAdded +"\n"); + + var velocityStep = 0.1/TREE_SCALE; + if (velocity.x > 0) { + velocity.x -= velocityStep; + velocity.z += velocityStep; + color.blue = 0; + color.green = 255; + } else { + velocity.x += velocityStep; + velocity.z -= velocityStep; + color.blue = 255; + color.green = 0; + } + + if (numberParticlesAdded <= MAX_PARTICLES) { + Particles.queueParticleAdd(center, particleSize, color, velocity, gravity, damping, false, scriptB); + print("hello... added particle... script=\n"); + print(scriptB); + numberParticlesAdded++; + } else { + Agent.stop(); + } + + print("Particles Stats: " + Particles.getLifetimeInSeconds() + " seconds," + + " Queued packets:" + Particles.getLifetimePacketsQueued() + "," + + " PPS:" + Particles.getLifetimePPSQueued() + "," + + " BPS:" + Particles.getLifetimeBPSQueued() + "," + + " Sent packets:" + Particles.getLifetimePacketsSent() + "," + + " PPS:" + Particles.getLifetimePPS() + "," + + " BPS:" + Particles.getLifetimeBPS() + + "\n"); + } +} + + +// register the call back so it fires before each data send +print("here...\n"); +Particles.setPacketsPerSecond(40000); +Agent.willSendVisualDataCallback.connect(draw); +print("and here...\n"); diff --git a/examples/gun.js b/examples/gun.js new file mode 100644 index 0000000000..f4e6e04ea9 --- /dev/null +++ b/examples/gun.js @@ -0,0 +1,98 @@ +// initialize our triggers +var triggerPulled = new Array(); +var numberOfTriggers = Controller.getNumberOfTriggers(); +for (t = 0; t < numberOfTriggers; t++) { + triggerPulled[t] = false; +} + +function checkController() { + var numberOfTriggers = Controller.getNumberOfTriggers(); + var numberOfSpatialControls = Controller.getNumberOfSpatialControls(); + var controllersPerTrigger = numberOfSpatialControls / numberOfTriggers; + //print("numberOfTriggers:" + numberOfTriggers + "\n"); + //print("numberOfSpatialControls:" + numberOfSpatialControls + "\n"); + //print("controllersPerTrigger:" + controllersPerTrigger + "\n"); + + // this is expected for hydras + if (numberOfTriggers == 2 && controllersPerTrigger == 2) { + for (var t = 0; t < numberOfTriggers; t++) { + var shootABullet = false; + var triggerValue = Controller.getTriggerValue(t); + + if (triggerPulled[t]) { + // must release to at least 0.1 + if (triggerValue < 0.1) { + triggerPulled[t] = false; // unpulled + } + } else { + // must pull to at least 0.9 + if (triggerValue > 0.9) { + triggerPulled[t] = true; // pulled + shootABullet = true; + } + } + + if (shootABullet) { + var palmController = t * controllersPerTrigger; + var palmPosition = Controller.getSpatialControlPosition(palmController); + + var fingerTipController = palmController + 1; + var fingerTipPosition = Controller.getSpatialControlPosition(fingerTipController); + + var bulletSize = 0.25/TREE_SCALE; + + var palmInParticleSpace = + { x: palmPosition.x/TREE_SCALE, + y: palmPosition.y/TREE_SCALE, + z: palmPosition.z/TREE_SCALE }; + + var tipInParticleSpace = + { x: fingerTipPosition.x/TREE_SCALE, + y: fingerTipPosition.y/TREE_SCALE, + z: fingerTipPosition.z/TREE_SCALE }; + + var palmToFingerTipVector = + { x: (tipInParticleSpace.x - palmInParticleSpace.x), + y: (tipInParticleSpace.y - palmInParticleSpace.y), + z: (tipInParticleSpace.z - palmInParticleSpace.z) }; + + // just off the front of the finger tip + var position = { x: tipInParticleSpace.x + palmToFingerTipVector.x/2, + y: tipInParticleSpace.y + palmToFingerTipVector.y/2, + z: tipInParticleSpace.z + palmToFingerTipVector.z/2}; + + var linearVelocity = 50; // 50 meters per second + + var velocity = { x: palmToFingerTipVector.x * linearVelocity, + y: palmToFingerTipVector.y * linearVelocity, + z: palmToFingerTipVector.z * linearVelocity }; + + var gravity = { x: 0, y: -0.1/TREE_SCALE, z: 0 }; // gravity has no effect on these bullets + var color = { red: 128, green: 128, blue: 128 }; + var damping = 0; // no damping + var inHand = false; + var script = + " function collisionWithVoxel(voxel) { " + + " print('collisionWithVoxel(voxel)... '); " + + " print('myID=' + Particle.getID() + '\\n'); " + + " var voxelColor = voxel.getColor();" + + " print('voxelColor=' + voxelColor.red + ', ' + voxelColor.green + ', ' + voxelColor.blue + '\\n'); " + + " var myColor = Particle.getColor();" + + " print('myColor=' + myColor.red + ', ' + myColor.green + ', ' + myColor.blue + '\\n'); " + + " Particle.setColor(voxelColor); " + + " var voxelAt = voxel.getPosition();" + + " var voxelScale = voxel.getScale();" + + " Voxels.queueVoxelDelete(voxelAt.x, voxelAt.y, voxelAt.z, voxelScale); " + + " print('Voxels.queueVoxelDelete(' + voxelAt.x + ', ' + voxelAt.y + ', ' + voxelAt.z + ', ' + voxelScale + ')... \\n'); " + + " } " + + " Particle.collisionWithVoxel.connect(collisionWithVoxel); "; + + Particles.queueParticleAdd(position, bulletSize, color, velocity, gravity, damping, inHand, script); + } + } + } +} + + +// register the call back so it fires before each data send +Agent.willSendVisualDataCallback.connect(checkController);