diff --git a/examples/collidingParticles.js b/examples/collidingParticles.js index f53933871f..cf1fce5660 100644 --- a/examples/collidingParticles.js +++ b/examples/collidingParticles.js @@ -18,14 +18,14 @@ var numberParticlesAdded = 0; var MAX_PARTICLES = 1; var velocity = { - x: 1/TREE_SCALE, - y: 0/TREE_SCALE, - z: 1/TREE_SCALE }; + x: 1, + y: 0, + z: 1 }; var gravity = { - x: 0/TREE_SCALE, - y: 0/TREE_SCALE, - z: 0/TREE_SCALE }; + x: 0, + y: 0, + z: 0 }; var damping = 0.1; @@ -65,16 +65,27 @@ function draw() { 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; + x: 2, + y: 0, + z: 2 }; + var largeRadius = 0.5; var verySlow = { - x: 0.01/TREE_SCALE, - y: 0/TREE_SCALE, - z: 0.01/TREE_SCALE }; + x: 0.01, + y: 0, + z: 0.01 }; + + var properties = { + position: startPosition, + radius: largeRadius, + color: colorGreen, + velocity: verySlow, + gravity: gravity, + damping: damping, + inHand: false, + script: scriptA + }; - Particles.queueParticleAdd(startPosition, largeRadius, colorGreen, verySlow, gravity, damping, false, scriptA); + Particles.addParticle(properties); print("hello... added particle... script=\n"); print(scriptA); numberParticlesAdded++; @@ -84,15 +95,15 @@ function draw() { print("draw()... sending another... currentIteration=" +currentIteration + "\n"); var center = { - x: 0/TREE_SCALE, - y: 0/TREE_SCALE, - z: 0/TREE_SCALE }; + x: 0, + y: 0, + z: 0 }; - var particleSize = 0.1 / TREE_SCALE; + var particleSize = 0.1; print("number of particles=" + numberParticlesAdded +"\n"); - var velocityStep = 0.1/TREE_SCALE; + var velocityStep = 0.1; if (velocity.x > 0) { velocity.x -= velocityStep; velocity.z += velocityStep; @@ -106,7 +117,17 @@ function draw() { } if (numberParticlesAdded <= MAX_PARTICLES) { - Particles.queueParticleAdd(center, particleSize, color, velocity, gravity, damping, false, scriptB); + var properties = { + position: center, + radius: particleSize, + color: color, + velocity: velocity, + gravity: gravity, + damping: damping, + inHand: false, + script: scriptB + }; + Particles.addParticle(properties); print("hello... added particle... script=\n"); print(scriptB); numberParticlesAdded++; diff --git a/examples/editParticleExample.js b/examples/editParticleExample.js index 24725ddf42..4eb5dfe907 100644 --- a/examples/editParticleExample.js +++ b/examples/editParticleExample.js @@ -11,9 +11,9 @@ var count = 0; var originalProperties = { - position: { x: 10/TREE_SCALE, - y: 0/TREE_SCALE, - z: 0/TREE_SCALE }, + position: { x: 10, + y: 0, + z: 0 }, velocity: { x: 0, y: 0, @@ -24,7 +24,7 @@ var originalProperties = { z: 0 }, - radius : 0.1/TREE_SCALE, + radius : 0.1, color: { red: 0, green: 255, @@ -32,23 +32,23 @@ var originalProperties = { }; -var positionDelta = { x: 0.1/TREE_SCALE, y: 0, z: 0 }; +var positionDelta = { x: 0.05, y: 0, z: 0 }; var particleID = Particles.addParticle(originalProperties); function moveParticle() { - if (count >= 10) { + if (count >= 100) { //Agent.stop(); // delete it... - if (count == 10) { + if (count == 100) { print("calling Particles.deleteParticle()"); Particles.deleteParticle(particleID); } // stop it... - if (count >= 100) { + if (count >= 200) { print("calling Agent.stop()"); Agent.stop(); } @@ -68,7 +68,7 @@ function moveParticle() { y: originalProperties.position.y + (count * positionDelta.y), z: originalProperties.position.z + (count * positionDelta.z) }, - radius : 0.25/TREE_SCALE, + radius : 0.25, }; @@ -79,11 +79,13 @@ function moveParticle() { Particles.editParticle(particleID, newProperties); // also check to see if we can "find" particles... - var searchAt = { x: 0, y: 0, z: 0}; + var searchAt = { x: 9, y: 0, z: 0}; var searchRadius = 2; var foundParticle = Particles.findClosestParticle(searchAt, searchRadius); if (foundParticle.isKnownID) { print("found particle:" + foundParticle.id); + } else { + print("could not find particle in or around x=9 to x=11:"); } } diff --git a/examples/fountain.js b/examples/fountain.js index b15557c8ee..86c125a834 100644 --- a/examples/fountain.js +++ b/examples/fountain.js @@ -37,25 +37,30 @@ function vInterpolate(a, b, fraction) { return rval; } -var position = { x: 5.0 / TREE_SCALE, y: 5.0 / TREE_SCALE, z: 5.0 / TREE_SCALE }; -Voxels.queueDestructiveVoxelAdd(position.x, position.y - (1.0 / TREE_SCALE), position.z, 0.5 / TREE_SCALE, 255, 255, 1); +var position = { x: 5.0, y: 0.6, z: 5.0 }; +Voxels.setVoxel(position.x, 0, position.z, 0.5, 0, 0, 255); +var totalParticles = 0; function makeFountain() { if (Math.random() < 0.06) { //print("Made particle!\n"); + var properties = { + position: position, + radius: (0.02 + (Math.random() * 0.05)), + color: { red: 0, green: 0, blue: 128 }, + velocity: { x: (Math.random() * 1.0 - 0.5), + y: (1.0 + (Math.random() * 2.0)), + z: (Math.random() * 1.0 - 0.5) }, + gravity: { x: 0, y: -0.5, z: 0 }, + damping: 0.25, + lifetime: 2 + } - var size = (0.02 + (Math.random() * 0.05)) / TREE_SCALE; - var velocity = { x: (Math.random() * 1.0 - 0.5) / TREE_SCALE, - y: (1.0 + (Math.random() * 2.0)) / TREE_SCALE, - z: (Math.random() * 1.0 - 0.5) / TREE_SCALE }; - - var gravity = { x: 0, y: -0.5 / TREE_SCALE, z: 0 }; // gravity has no effect on these bullets - var color = { red: 0, green: 0, blue: 128 }; - var damping = 0.25; // no damping - var inHand = false; - var script = ""; - - Particles.queueParticleAdd(position, size, color, velocity, gravity, damping, inHand, script); + Particles.addParticle(properties); + totalParticles++; + } + if (totalParticles > 100) { + Agent.stop(); } } // register the call back so it fires before each data send diff --git a/examples/gun.js b/examples/gun.js index b93131c0f7..30d2b41449 100644 --- a/examples/gun.js +++ b/examples/gun.js @@ -45,46 +45,30 @@ function checkController() { } if (shootABullet) { - var palmController = t * controllersPerTrigger; + var palmController = t * controllersPerTrigger; var palmPosition = Controller.getSpatialControlPosition(palmController); - var fingerTipController = palmController + 1; + var fingerTipController = palmController + 1; var fingerTipPosition = Controller.getSpatialControlPosition(fingerTipController); - var bulletSize = 0.05/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) }; - + + var palmToFingerTipVector = + { x: (fingerTipPosition.x - palmPosition.x), + y: (fingerTipPosition.y - palmPosition.y), + z: (fingerTipPosition.z - palmPosition.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 = 5; + var position = { x: fingerTipPosition.x + palmToFingerTipVector.x/2, + y: fingerTipPosition.y + palmToFingerTipVector.y/2, + z: fingerTipPosition.z + palmToFingerTipVector.z/2}; + var linearVelocity = 25; + 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; - // This is the script for the particles that this gun shoots. - var script = + var script = " function collisionWithVoxel(voxel) { " + " print('collisionWithVoxel(voxel)... '); " + " print('myID=' + Particle.getID() + '\\n'); " + @@ -92,16 +76,22 @@ function checkController() { " print('voxelColor=' + voxelColor.red + ', ' + voxelColor.green + ', ' + voxelColor.blue + '\\n'); " + " var myColor = Particle.getColor();" + " print('myColor=' + myColor.red + ', ' + myColor.green + ', ' + myColor.blue + '\\n'); " + - " var newProps = { color: voxelColor }; " + - " Particle.setProperties(newProps); " + + " Particle.setColor(voxelColor); " + " var voxelAt = voxel.getPosition();" + " var voxelScale = voxel.getScale();" + " Voxels.eraseVoxel(voxelAt.x, voxelAt.y, voxelAt.z, voxelScale); " + " print('Voxels.eraseVoxel(' + voxelAt.x + ', ' + voxelAt.y + ', ' + voxelAt.z + ', ' + voxelScale + ')... \\n'); " + " } " + " Particle.collisionWithVoxel.connect(collisionWithVoxel); "; - - Particles.queueParticleAdd(position, bulletSize, color, velocity, gravity, damping, inHand, script); + + Particles.addParticle( + { position: position, + radius: 0.05, + color: { red: 128, green: 128, blue: 128 }, + velocity: velocity, + gravity: { x: 0, y: -0.1, z: 0 }, + damping: 0, + script: script }); } } } diff --git a/examples/movingVoxel.js b/examples/movingVoxel.js index 44ab418aaf..0aadf7b30c 100644 --- a/examples/movingVoxel.js +++ b/examples/movingVoxel.js @@ -28,9 +28,9 @@ function moveVoxel() { thisColor = colorEdge; } // Create a new voxel - Voxels.queueDestructiveVoxelAdd(position.x / TREE_SCALE, position.y / TREE_SCALE, position.z / TREE_SCALE, size / TREE_SCALE, thisColor.r, thisColor.g, thisColor.b); + Voxels.setVoxel(position.x, position.y, position.z, size, thisColor.r, thisColor.g, thisColor.b); // delete old voxel - Voxels.queueVoxelDelete(oldPosition.x / TREE_SCALE, oldPosition.y / TREE_SCALE, oldPosition.z / TREE_SCALE, size / TREE_SCALE); + Voxels.eraseVoxel(oldPosition.x, oldPosition.y, oldPosition.z, size); // Copy old location to new oldPosition.x = position.x; oldPosition.y = position.y; diff --git a/examples/paintGun.js b/examples/paintGun.js index f75df58314..0cbe3ff366 100644 --- a/examples/paintGun.js +++ b/examples/paintGun.js @@ -1,17 +1,10 @@ // -// gun.js +// paintGun.js // hifi // // Created by Brad Hefta-Gaub on 12/31/13. // Copyright (c) 2013 HighFidelity, Inc. All rights reserved. // -// This is an example script that turns the hydra controllers into a particle gun. -// It reads the controller, watches for trigger pulls, and launches particles. -// The particles it creates have a script that when they collide with Voxels, the -// particle will change it's color to match the voxel it hits, and then delete the -// voxel. -// -// // initialize our triggers var triggerPulled = new Array(); @@ -51,27 +44,15 @@ function checkController() { var fingerTipController = palmController + 1; var fingerTipPosition = Controller.getSpatialControlPosition(fingerTipController); - var bulletSize = 0.01/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) }; + { x: (fingerTipPosition.x - palmPosition.x), + y: (fingerTipPosition.y - palmPosition.y), + z: (fingerTipPosition.z - palmPosition.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 position = { x: fingerTipPosition.x + palmToFingerTipVector.x/2, + y: fingerTipPosition.y + palmToFingerTipVector.y/2, + z: fingerTipPosition.z + palmToFingerTipVector.z/2}; var linearVelocity = 25; @@ -79,11 +60,6 @@ function checkController() { 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; - // This is the script for the particles that this gun shoots. var script = " function collisionWithVoxel(voxel) { " + @@ -96,12 +72,20 @@ function checkController() { " Particle.setColor(voxelColor); " + " var voxelAt = voxel.getPosition();" + " var voxelScale = voxel.getScale();" + - " Voxels.queueVoxelAdd(voxelAt.x, voxelAt.y, voxelAt.z, voxelScale, 255, 255, 0); " + - " print('Voxels.queueVoxelDelete(' + voxelAt.x + ', ' + voxelAt.y + ', ' + voxelAt.z + ', ' + voxelScale + ')... \\n'); " + + " Voxels.setVoxel(voxelAt.x, voxelAt.y, voxelAt.z, voxelScale, 255, 255, 0); " + + " print('Voxels.setVoxel(' + voxelAt.x + ', ' + voxelAt.y + ', ' + voxelAt.z + ', ' + voxelScale + ')... \\n'); " + " } " + " Particle.collisionWithVoxel.connect(collisionWithVoxel); "; - Particles.queueParticleAdd(position, bulletSize, color, velocity, gravity, damping, inHand, script); + Particles.addParticle( + { position: position, + radius: 0.01, + color: { red: 128, green: 128, blue: 128 }, + velocity: velocity, + gravity: { x: 0, y: -0.1, z: 0 }, + damping: 0, + script: script } + ); } } } diff --git a/examples/toyball.js b/examples/toyball.js index 4588c0397b..1682cbe3d4 100644 --- a/examples/toyball.js +++ b/examples/toyball.js @@ -103,9 +103,9 @@ function checkControllerSide(whichSide) { rightHandParticle = closestParticle; } var ballPosition = getBallHoldPosition(whichSide); - var properties = { position: { x: ballPosition.x / TREE_SCALE, - y: ballPosition.y / TREE_SCALE, - z: ballPosition.z / TREE_SCALE }, + var properties = { position: { x: ballPosition.x, + y: ballPosition.y, + z: ballPosition.z }, velocity : { x: 0, y: 0, z: 0}, inHand: true }; Particles.editParticle(closestParticle, properties); @@ -127,13 +127,13 @@ function checkControllerSide(whichSide) { // If '3' is pressed, and not holding a ball, make a new one if (Controller.isButtonPressed(BUTTON_3) && !ballAlreadyInHand) { var ballPosition = getBallHoldPosition(whichSide); - var properties = { position: { x: ballPosition.x / TREE_SCALE, - y: ballPosition.y / TREE_SCALE, - z: ballPosition.z / TREE_SCALE }, + var properties = { position: { x: ballPosition.x, + y: ballPosition.y, + z: ballPosition.z }, velocity: { x: 0, y: 0, z: 0}, gravity: { x: 0, y: 0, z: 0}, inHand: true, - radius: 0.05 / TREE_SCALE, + radius: 0.05, color: { red: 255, green: 0, blue: 0 }, lifetime: 10 // 10 seconds }; @@ -169,9 +169,9 @@ function checkControllerSide(whichSide) { if (grabButtonPressed) { debugPrint(">>>>> " + handMessage + "-BALL IN HAND, grabbing, hold and move"); var ballPosition = getBallHoldPosition(whichSide); - var properties = { position: { x: ballPosition.x / TREE_SCALE, - y: ballPosition.y / TREE_SCALE, - z: ballPosition.z / TREE_SCALE }, + var properties = { position: { x: ballPosition.x, + y: ballPosition.y, + z: ballPosition.z }, }; Particles.editParticle(handParticle, properties); } else { @@ -180,11 +180,11 @@ function checkControllerSide(whichSide) { var tipVelocity = Controller.getSpatialControlVelocity(whichTip); var THROWN_VELOCITY_SCALING = 1.5; var properties = { - velocity: { x: (tipVelocity.x * THROWN_VELOCITY_SCALING) / TREE_SCALE, - y: (tipVelocity.y * THROWN_VELOCITY_SCALING) / TREE_SCALE, - z: (tipVelocity.z * THROWN_VELOCITY_SCALING) / TREE_SCALE } , + velocity: { x: tipVelocity.x * THROWN_VELOCITY_SCALING, + y: tipVelocity.y * THROWN_VELOCITY_SCALING, + z: tipVelocity.z * THROWN_VELOCITY_SCALING } , inHand: false, - gravity: { x: 0, y: -2 / TREE_SCALE, z: 0}, + gravity: { x: 0, y: -2, z: 0}, }; Particles.editParticle(handParticle, properties); diff --git a/examples/voxelBird.js b/examples/voxelBird.js index bc4cac0247..54c0129045 100644 --- a/examples/voxelBird.js +++ b/examples/voxelBird.js @@ -110,17 +110,17 @@ function moveBird() { if (tweeting > 0) { // Change color of voxel to blinky red a bit while playing the sound var blinkColor = { r: Math.random() * 255, g: 0, b: 0 }; - Voxels.queueDestructiveVoxelAdd(position.x / TREE_SCALE, - position.y / TREE_SCALE, - position.z / TREE_SCALE, - size / TREE_SCALE, - blinkColor.r, blinkColor.g, blinkColor.b); + Voxels.setVoxel(position.x, + position.y, + position.z, + size, + blinkColor.r, blinkColor.g, blinkColor.b); } if (moved) { - Voxels.queueDestructiveVoxelAdd(position.x / TREE_SCALE, position.y / TREE_SCALE, position.z / TREE_SCALE, size / TREE_SCALE, thisColor.r, thisColor.g, thisColor.b); + Voxels.setVoxel(position.x, position.y, position.z, size, thisColor.r, thisColor.g, thisColor.b); // delete old voxel - Voxels.queueVoxelDelete(oldPosition.x / TREE_SCALE, oldPosition.y / TREE_SCALE, oldPosition.z / TREE_SCALE, size / TREE_SCALE); + Voxels.eraseVoxel(oldPosition.x, oldPosition.y, oldPosition.z, size); // Copy old location to new vCopy(oldPosition, position); moved = false; diff --git a/libraries/particles/src/Particle.cpp b/libraries/particles/src/Particle.cpp index 057ad07c36..5570797ab4 100644 --- a/libraries/particles/src/Particle.cpp +++ b/libraries/particles/src/Particle.cpp @@ -539,7 +539,7 @@ bool Particle::encodeParticleEditMessageDetails(PACKET_TYPE command, ParticleID // radius if (isNewParticle || ((packetContainsBits & PACKET_CONTAINS_RADIUS) == PACKET_CONTAINS_RADIUS)) { - float radius = properties.getRadius(); + float radius = properties.getRadius() / (float) TREE_SCALE; memcpy(copyAt, &radius, sizeof(radius)); copyAt += sizeof(radius); sizeOut += sizeof(radius); @@ -547,9 +547,10 @@ bool Particle::encodeParticleEditMessageDetails(PACKET_TYPE command, ParticleID // position if (isNewParticle || ((packetContainsBits & PACKET_CONTAINS_POSITION) == PACKET_CONTAINS_POSITION)) { - memcpy(copyAt, &properties.getPosition(), sizeof(properties.getPosition())); - copyAt += sizeof(properties.getPosition()); - sizeOut += sizeof(properties.getPosition()); + glm::vec3 position = properties.getPosition() / (float)TREE_SCALE; + memcpy(copyAt, &position, sizeof(position)); + copyAt += sizeof(position); + sizeOut += sizeof(position); } // color @@ -562,16 +563,18 @@ bool Particle::encodeParticleEditMessageDetails(PACKET_TYPE command, ParticleID // velocity if (isNewParticle || ((packetContainsBits & PACKET_CONTAINS_VELOCITY) == PACKET_CONTAINS_VELOCITY)) { - memcpy(copyAt, &properties.getVelocity(), sizeof(properties.getVelocity())); - copyAt += sizeof(properties.getVelocity()); - sizeOut += sizeof(properties.getVelocity()); + glm::vec3 velocity = properties.getVelocity() / (float)TREE_SCALE; + memcpy(copyAt, &velocity, sizeof(velocity)); + copyAt += sizeof(velocity); + sizeOut += sizeof(velocity); } // gravity if (isNewParticle || ((packetContainsBits & PACKET_CONTAINS_GRAVITY) == PACKET_CONTAINS_GRAVITY)) { - memcpy(copyAt, &properties.getGravity(), sizeof(properties.getGravity())); - copyAt += sizeof(properties.getGravity()); - sizeOut += sizeof(properties.getGravity()); + glm::vec3 gravity = properties.getGravity() / (float)TREE_SCALE; + memcpy(copyAt, &gravity, sizeof(gravity)); + copyAt += sizeof(gravity); + sizeOut += sizeof(gravity); } // damping @@ -1063,7 +1066,7 @@ void ParticleProperties::copyFromScriptValue(const QScriptValue &object) { void ParticleProperties::copyToParticle(Particle& particle) const { if (_positionChanged) { - particle.setPosition(_position); + particle.setPosition(_position / (float) TREE_SCALE); } if (_colorChanged) { @@ -1071,15 +1074,15 @@ void ParticleProperties::copyToParticle(Particle& particle) const { } if (_radiusChanged) { - particle.setRadius(_radius); + particle.setRadius(_radius / (float) TREE_SCALE); } if (_velocityChanged) { - particle.setVelocity(_velocity); + particle.setVelocity(_velocity / (float) TREE_SCALE); } if (_gravityChanged) { - particle.setGravity(_gravity); + particle.setGravity(_gravity / (float) TREE_SCALE); } if (_dampingChanged) { @@ -1104,11 +1107,11 @@ void ParticleProperties::copyToParticle(Particle& particle) const { } void ParticleProperties::copyFromParticle(const Particle& particle) { - _position = particle.getPosition(); + _position = particle.getPosition() * (float) TREE_SCALE; _color = particle.getXColor(); - _radius = particle.getRadius(); - _velocity = particle.getVelocity(); - _gravity = particle.getGravity(); + _radius = particle.getRadius() * (float) TREE_SCALE; + _velocity = particle.getVelocity() * (float) TREE_SCALE; + _gravity = particle.getGravity() * (float) TREE_SCALE; _damping = particle.getDamping(); _lifetime = particle.getLifetime(); _script = particle.getScript(); diff --git a/libraries/particles/src/Particle.h b/libraries/particles/src/Particle.h index da8cf35ac2..10a8d6868c 100644 --- a/libraries/particles/src/Particle.h +++ b/libraries/particles/src/Particle.h @@ -54,6 +54,7 @@ const bool NOT_IN_HAND = !IN_HAND; // it's not in a hand /// A collection of properties of a particle used in the scripting API. Translates between the actual properties of a particle /// and a JavaScript style hash/QScriptValue storing a set of properties. Used in scripting to set/get the complete set of /// particle properties via JavaScript hashes/QScriptValues +/// all units for position, velocity, gravity, radius, etc are in meter units class ParticleProperties { public: ParticleProperties(); @@ -78,10 +79,15 @@ public: uint64_t getLastEdited() const { return _lastEdited; } uint16_t getChangedBits() const; + /// set position in meter units void setPosition(const glm::vec3& value) { _position = value; _positionChanged = true; } + + /// set velocity in meter units void setVelocity(const glm::vec3& value) { _velocity = value; _velocityChanged = true; } void setColor(const xColor& value) { _color = value; _colorChanged = true; } void setRadius(float value) { _radius = value; _radiusChanged = true; } + + /// set gravity in meter units void setGravity(const glm::vec3& value) { _gravity = value; _gravityChanged = true; } void setInHand(bool inHand) { _inHand = inHand; _inHandChanged = true; } void setDamping(float value) { _damping = value; _dampingChanged = true; } @@ -146,6 +152,8 @@ class Particle { public: Particle(); + + /// all position, velocity, gravity, radius units are in domain units (0.0 to 1.0) Particle(glm::vec3 position, float radius, rgbColor color, glm::vec3 velocity, glm::vec3 gravity = DEFAULT_GRAVITY, float damping = DEFAULT_DAMPING, float lifetime = DEFAULT_LIFETIME, bool inHand = NOT_IN_HAND, QString updateScript = DEFAULT_SCRIPT, uint32_t id = NEW_PARTICLE); @@ -158,13 +166,22 @@ public: glm::vec3 gravity = DEFAULT_GRAVITY, float damping = DEFAULT_DAMPING, float lifetime = DEFAULT_LIFETIME, bool inHand = NOT_IN_HAND, QString updateScript = DEFAULT_SCRIPT, uint32_t id = NEW_PARTICLE); + /// get position in domain scale units (0.0 - 1.0) const glm::vec3& getPosition() const { return _position; } + const rgbColor& getColor() const { return _color; } xColor getXColor() const { xColor color = { _color[RED_INDEX], _color[GREEN_INDEX], _color[BLUE_INDEX] }; return color; } + + /// get radius in domain scale units (0.0 - 1.0) float getRadius() const { return _radius; } float getMass() const { return _mass; } + + /// get velocity in domain scale units (0.0 - 1.0) const glm::vec3& getVelocity() const { return _velocity; } + + /// get gravity in domain scale units (0.0 - 1.0) const glm::vec3& getGravity() const { return _gravity; } + bool getInHand() const { return _inHand; } float getDamping() const { return _damping; } float getLifetime() const { return _lifetime; } @@ -185,7 +202,10 @@ public: uint32_t getCreatorTokenID() const { return _creatorTokenID; } bool isNewlyCreated() const { return _newlyCreated; } + /// set position in domain scale units (0.0 - 1.0) void setPosition(const glm::vec3& value) { _position = value; } + + /// set velocity in domain scale units (0.0 - 1.0) void setVelocity(const glm::vec3& value) { _velocity = value; } void setColor(const rgbColor& value) { memcpy(_color, value, sizeof(_color)); } void setColor(const xColor& value) { @@ -193,8 +213,11 @@ public: _color[GREEN_INDEX] = value.green; _color[BLUE_INDEX] = value.blue; } + /// set radius in domain scale units (0.0 - 1.0) void setRadius(float value) { _radius = value; } void setMass(float value); + + /// set gravity in domain scale units (0.0 - 1.0) void setGravity(const glm::vec3& value) { _gravity = value; } void setInHand(bool inHand) { _inHand = inHand; } void setDamping(float value) { _damping = value; } @@ -291,23 +314,40 @@ public: public slots: unsigned int getID() const { return _particle->getID(); } - glm::vec3 getPosition() const { return _particle->getPosition(); } - glm::vec3 getVelocity() const { return _particle->getVelocity(); } + + /// get position in meter units + glm::vec3 getPosition() const { return _particle->getPosition() * (float)TREE_SCALE; } + + /// get velocity in meter units + glm::vec3 getVelocity() const { return _particle->getVelocity() * (float)TREE_SCALE; } xColor getColor() const { return _particle->getXColor(); } - glm::vec3 getGravity() const { return _particle->getGravity(); } + + /// get gravity in meter units + glm::vec3 getGravity() const { return _particle->getGravity() * (float)TREE_SCALE; } + float getDamping() const { return _particle->getDamping(); } - float getRadius() const { return _particle->getRadius(); } + + /// get radius in meter units + float getRadius() const { return _particle->getRadius() * (float)TREE_SCALE; } bool getShouldDie() { return _particle->getShouldDie(); } float getAge() const { return _particle->getAge(); } float getLifetime() const { return _particle->getLifetime(); } ParticleProperties getProperties() const { return _particle->getProperties(); } - void setPosition(glm::vec3 value) { _particle->setPosition(value); } - void setVelocity(glm::vec3 value) { _particle->setVelocity(value); } - void setGravity(glm::vec3 value) { _particle->setGravity(value); } + /// set position in meter units + void setPosition(glm::vec3 value) { _particle->setPosition(value / (float)TREE_SCALE); } + + /// set velocity in meter units + void setVelocity(glm::vec3 value) { _particle->setVelocity(value / (float)TREE_SCALE); } + + /// set gravity in meter units + void setGravity(glm::vec3 value) { _particle->setGravity(value / (float)TREE_SCALE); } + void setDamping(float value) { _particle->setDamping(value); } void setColor(xColor value) { _particle->setColor(value); } - void setRadius(float value) { _particle->setRadius(value); } + + /// set radius in meter units + void setRadius(float value) { _particle->setRadius(value / (float)TREE_SCALE); } void setShouldDie(bool value) { _particle->setShouldDie(value); } void setScript(const QString& script) { _particle->setScript(script); } void setLifetime(float value) const { return _particle->setLifetime(value); } diff --git a/libraries/voxels/src/VoxelsScriptingInterface.cpp b/libraries/voxels/src/VoxelsScriptingInterface.cpp index df5dad8231..3f4e19f60a 100644 --- a/libraries/voxels/src/VoxelsScriptingInterface.cpp +++ b/libraries/voxels/src/VoxelsScriptingInterface.cpp @@ -12,18 +12,21 @@ void VoxelsScriptingInterface::queueVoxelAdd(PACKET_TYPE addPacketType, VoxelDet getVoxelPacketSender()->queueVoxelEditMessages(addPacketType, 1, &addVoxelDetails); } -void VoxelsScriptingInterface::setVoxel(float x, float y, float z, float scale, uchar red, uchar green, uchar blue) { +void VoxelsScriptingInterface::setVoxelNonDestructive(float x, float y, float z, float scale, + uchar red, uchar green, uchar blue) { // setup a VoxelDetail struct with the data - VoxelDetail addVoxelDetail = {x, y, z, scale, red, green, blue}; + VoxelDetail addVoxelDetail = {x / (float)TREE_SCALE, y / (float)TREE_SCALE, z / (float)TREE_SCALE, + scale / (float)TREE_SCALE, red, green, blue}; // queue the packet queueVoxelAdd(PACKET_TYPE_VOXEL_SET, addVoxelDetail); } -void VoxelsScriptingInterface::destructiveSetVoxel(float x, float y, float z, float scale, +void VoxelsScriptingInterface::setVoxel(float x, float y, float z, float scale, uchar red, uchar green, uchar blue) { // setup a VoxelDetail struct with the data - VoxelDetail addVoxelDetail = {x, y, z, scale, red, green, blue}; + VoxelDetail addVoxelDetail = {x / (float)TREE_SCALE, y / (float)TREE_SCALE, z / (float)TREE_SCALE, + scale / (float)TREE_SCALE, red, green, blue}; // queue the destructive add queueVoxelAdd(PACKET_TYPE_VOXEL_SET_DESTRUCTIVE, addVoxelDetail); @@ -32,7 +35,8 @@ void VoxelsScriptingInterface::destructiveSetVoxel(float x, float y, float z, fl void VoxelsScriptingInterface::eraseVoxel(float x, float y, float z, float scale) { // setup a VoxelDetail struct with data - VoxelDetail deleteVoxelDetail = {x, y, z, scale, 0, 0, 0}; + VoxelDetail deleteVoxelDetail = {x / (float)TREE_SCALE, y / (float)TREE_SCALE, z / (float)TREE_SCALE, + scale / (float)TREE_SCALE, 0, 0, 0}; getVoxelPacketSender()->queueVoxelEditMessages(PACKET_TYPE_VOXEL_ERASE, 1, &deleteVoxelDetail); } diff --git a/libraries/voxels/src/VoxelsScriptingInterface.h b/libraries/voxels/src/VoxelsScriptingInterface.h index ea9f6a4205..6bbe21d601 100644 --- a/libraries/voxels/src/VoxelsScriptingInterface.h +++ b/libraries/voxels/src/VoxelsScriptingInterface.h @@ -13,6 +13,8 @@ #include #include + +#include "VoxelConstants.h" #include "VoxelEditPacketSender.h" /// handles scripting of voxel commands from JS passed to assigned clients @@ -26,30 +28,30 @@ public: public slots: /// queues the creation of a voxel which will be sent by calling process on the PacketSender - /// \param x the x-coordinate of the voxel (in VS space) - /// \param y the y-coordinate of the voxel (in VS space) - /// \param z the z-coordinate of the voxel (in VS space) - /// \param scale the scale of the voxel (in VS space) + /// \param x the x-coordinate of the voxel (in meter units) + /// \param y the y-coordinate of the voxel (in meter units) + /// \param z the z-coordinate of the voxel (in meter units) + /// \param scale the scale of the voxel (in meter units) + /// \param red the R value for RGB color of voxel + /// \param green the G value for RGB color of voxel + /// \param blue the B value for RGB color of voxel + void setVoxelNonDestructive(float x, float y, float z, float scale, uchar red, uchar green, uchar blue); + + /// queues the destructive creation of a voxel which will be sent by calling process on the PacketSender + /// \param x the x-coordinate of the voxel (in meter units) + /// \param y the y-coordinate of the voxel (in meter units) + /// \param z the z-coordinate of the voxel (in meter units) + /// \param scale the scale of the voxel (in meter units) /// \param red the R value for RGB color of voxel /// \param green the G value for RGB color of voxel /// \param blue the B value for RGB color of voxel void setVoxel(float x, float y, float z, float scale, uchar red, uchar green, uchar blue); - /// queues the destructive creation of a voxel which will be sent by calling process on the PacketSender - /// \param x the x-coordinate of the voxel (in VS space) - /// \param y the y-coordinate of the voxel (in VS space) - /// \param z the z-coordinate of the voxel (in VS space) - /// \param scale the scale of the voxel (in VS space) - /// \param red the R value for RGB color of voxel - /// \param green the G value for RGB color of voxel - /// \param blue the B value for RGB color of voxel - void destructiveSetVoxel(float x, float y, float z, float scale, uchar red, uchar green, uchar blue); - /// queues the deletion of a voxel, sent by calling process on the PacketSender - /// \param x the x-coordinate of the voxel (in VS space) - /// \param y the y-coordinate of the voxel (in VS space) - /// \param z the z-coordinate of the voxel (in VS space) - /// \param scale the scale of the voxel (in VS space) + /// \param x the x-coordinate of the voxel (in meter units) + /// \param y the y-coordinate of the voxel (in meter units) + /// \param z the z-coordinate of the voxel (in meter units) + /// \param scale the scale of the voxel (in meter units) void eraseVoxel(float x, float y, float z, float scale); private: @@ -62,9 +64,11 @@ public: VoxelDetailScriptObject(VoxelDetail* voxelDetail) { _voxelDetail = voxelDetail; } public slots: - glm::vec3 getPosition() const { return glm::vec3(_voxelDetail->x, _voxelDetail->y, _voxelDetail->z); } + /// position in meter units + glm::vec3 getPosition() const { return glm::vec3(_voxelDetail->x, _voxelDetail->y, _voxelDetail->z) * (float)TREE_SCALE; } xColor getColor() const { xColor color = { _voxelDetail->red, _voxelDetail->green, _voxelDetail->blue }; return color; } - float getScale() const { return _voxelDetail->s; } + /// scale in meter units + float getScale() const { return _voxelDetail->s * (float)TREE_SCALE; } private: VoxelDetail* _voxelDetail;