From e27335fc5e1ec2ed35b2d8ec3c1779e1164c483b Mon Sep 17 00:00:00 2001 From: bwent Date: Tue, 4 Aug 2015 11:56:03 -0700 Subject: [PATCH 01/12] Refactoring to use Planet class, set up framework for UI --- examples/example/solarsystem.js | 471 ++++++++++++-------------------- 1 file changed, 172 insertions(+), 299 deletions(-) diff --git a/examples/example/solarsystem.js b/examples/example/solarsystem.js index 8d1f0c81e3..8711014335 100644 --- a/examples/example/solarsystem.js +++ b/examples/example/solarsystem.js @@ -18,39 +18,15 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -Script.include('../utilities/tools/cookies.js'); -Script.include('games/satellite.js'); +Script.include([ + '../utilities/tools/cookies.js'); + 'games/satellite.js' +]); var BASE_URL = "https://s3.amazonaws.com/hifi-public/marketplace/hificontent/Scripts/planets/planets/"; -var NUM_PLANETS = 8; - -var trailsEnabled = true; -var energyConserved = true; -var planetView = false; -var earthView = false; -var satelliteGame; - -var PANEL_X = 850; -var PANEL_Y = 600; -var BUTTON_SIZE = 20; -var PADDING = 20; - -var DAMPING = 0.0; -var LIFETIME = 6000; -var ERROR_THRESH = 2.0; -var TIME_STEP = 70.0; - -var MAX_POINTS_PER_LINE = 5; -var LINE_DIM = 10; -var LINE_WIDTH = 3.0; -var line; -var planetLines = []; -var trails = []; - var BOUNDS = 200; - // Alert user to move if they are too close to domain bounds if (MyAvatar.position.x < BOUNDS || MyAvatar.position.x > TREE_SCALE - BOUNDS || MyAvatar.position.y < BOUNDS || MyAvatar.position.y > TREE_SCALE - BOUNDS || MyAvatar.position.z < BOUNDS || MyAvatar.position.z > TREE_SCALE - BOUNDS) { Window.alert("Please move at least 200m away from domain bounds."); @@ -58,9 +34,21 @@ if (MyAvatar.position.x < BOUNDS || MyAvatar.position.x > TREE_SCALE - BOUNDS || } // Save intiial avatar and camera position -var startingPosition = MyAvatar.position; +var startingPosition = {x: 800, y: 800, z: 800}; +MyAvatar.position = startingPosition; var startFrame = Window.location.href; +/* + + +********************************** +TODO: create initial UI panel here + + + +*/ + + // Place the sun var MAX_RANGE = 80.0; var SUN_SIZE = 8.0; @@ -82,119 +70,124 @@ var theSun = Entities.addEntity({ collisionsWillMove: false }); -var planets = []; -var planet_properties = []; -// Reference values -var radius = 7.0; -var T_ref = 1.0; -var size = 1.0; -var M = 250.0; -var m = M * 0.000000333; -var G = (Math.pow(radius, 3.0) / Math.pow((T_ref / (2.0 * Math.PI)), 2.0)) / M; -var G_ref = G; -// Adjust size and distance as number of planets increases -var DELTA_RADIUS = 1.8; -var DELTA_SIZE = 0.2; +// Reference values for physical constants +var referenceRadius = 7.0; +var referencePeriod = 1.0; +var LARGE_BODY_MASS = 250.0; +var SMALL_BODY_MASS = LARGE_BODY_MASS * 0.000000333; +var GRAVITY = (Math.pow(referenceRadius, 3.0) / Math.pow((referencePeriod / (2.0 * Math.PI)), 2.0)) / LARGE_BODY_MASS; +var REFERENCE_GRAVITY = GRAVITY; -function initPlanets() { - for (var i = 0; i < NUM_PLANETS; ++i) { - var v0 = Math.sqrt((G * M) / radius); - var T = (2.0 * Math.PI) * Math.sqrt(Math.pow(radius, 3.0) / (G * M)); +var planetCount = 0; +var trails = []; - if (i == 0) { - var color = { - red: 255, - green: 255, - blue: 255 - }; - } else if (i == 1) { - var color = { - red: 255, - green: 160, - blue: 110 - }; - } else if (i == 2) { - var color = { - red: 10, - green: 150, - blue: 160 - }; - } else if (i == 3) { - var color = { - red: 180, - green: 70, - blue: 10 - }; - } else if (i == 4) { - var color = { - red: 250, - green: 140, - blue: 0 - }; - } else if (i == 5) { - var color = { - red: 235, - green: 215, - blue: 0 - }; - } else if (i == 6) { - var color = { - red: 135, - green: 205, - blue: 240 - }; - } else if (i == 7) { - var color = { - red: 30, - green: 140, - blue: 255 - }; - } +var Planet = function(name, trailColor, radius, size) { + this.index = 0; + this.name = name; + this.trailColor = trailColor; - var prop = { - radius: radius, - position: Vec3.sum(center, { - x: radius, - y: 0.0, - z: 0.0 - }), - lineColor: color, - period: T, - dimensions: size, - velocity: Vec3.multiply(v0, Vec3.normalize({ + this.radius = radius; + this.position = Vec3.sum(center, { x: this.radius, y: 0.0, z: 0.0 }); + this.period = (2.0 * Math.PI) * Math.sqrt(Math.pow(this.radius, 3.0) / (GRAVITY * LARGE_BODY_MASS)); + this.gravity = GRAVITY; + this.initialVelocity = Math.sqrt((GRAVITY * LARGE_BODY_MASS) / radius); + this.velocity = Vec3.multiply(this.initialVelocity, Vec3.normalize({ x: 0, y: -0.2, z: 0.9 - })) - }; - planet_properties.push(prop); + }); + this.dimensions = size; - planets.push(Entities.addEntity({ + this.planet = Entities.addEntity({ type: "Model", - modelURL: BASE_URL + (i + 1) + ".fbx", - position: prop.position, + modelURL: BASE_URL + name + ".fbx", + position: this.position, dimensions: { - x: prop.dimensions, - y: prop.dimensions, - z: prop.dimensions + x: this.dimensions, + y: this.dimensions, + z: this.dimensions }, - velocity: prop.velocity, + velocity: this.velocity, angularDamping: DAMPING, damping: DAMPING, ignoreCollisions: false, lifetime: LIFETIME, collisionsWillMove: true, - })); + })); - radius *= DELTA_RADIUS; - size += DELTA_SIZE; + this.label = new PlanetLabel(name, this.index); + + + this.computeAcceleration = function() { + var acc = -(this.gravity * LARGE_BODY_MASS) * Math.pow(this.radius, (-2.0)); + return acc; + }; + + this.update = function(deltaTime) { + var between = Vec3.subtract(this.position, center); + var speed = this.computeAcceleration(this.radius) * deltaTime; + var vel = Vec3.multiply(speed, Vec3.normalize(between)); + + // Update velocity and position + this.velocity = Vec3.sum(this.velocity, vel); + this.position = Vec3.sum(this.position, Vec3.multiply(this.velocity, deltaTime)); + Entities.editEntity(this.planet, { + velocity: this..velocity, + position: this..position + }); + }; + + this.resetTrails = function() { + elapsed = 0.0; + + this.trail = []; + this.lineStack = []; + //add the first line to both the line entity stack and the trail + trails.push(newLine(lineStack, this.position, this.period, this.lineColor)); + planetLines.push(lineStack); + + }; + + this.updateTrails = function() { + var point = this.position; + + var prop = Entities.getEntityProperties(this.lineStack[this.lineStack.length - 1]); + var linePos = prop.position; + + trails[index].push(computeLocalPoint(linePos, point)); + + Entities.editEntity(lineStack[lineStack.length - 1], { + linePoints: trails[i] + }); + if (trails[index].length === MAX_POINTS_PER_LINE) { + trails[index] = newLine(lineStack, point, this.period, this.lineColor); + } + }; + + this.adjustPeriod = function(alpha) { + // Update global G constant, period, poke velocity to new value + var ratio = this.last_alpha / alpha; + this.gravity = Math.pow(ratio, 2.0) * GRAVITY; + this.period = ratio * this.period; + this.velocity = Vec3.multiply(ratio, this.velocity); + + this.last_alpha = alpha; } + + this.index++; } -// Initialize planets -initPlanets(); +planets.push(new Planet("Mercury", {red: 255, green: 255, blue: 255}, 7.0, 1.0)); +planets.push(new Planet("Venus", {red: 255, green: 160, blue: 110}, 8.0, 1.2)); +planets.push(new Planet("Earth", {red: 10, green: 150, blue: 160}, 9.2, 1.6)); +planets.push(new Planet("Mars", {red: 180, green: 70, blue: 10}, 11.0, 2.0)); +planets.push(new Planet("Jupiter", {red: 250, green: 140, blue: 0}, 14.5, 4.3)); +planets.push(new Planet("Saturn", {red: 235, green: 215, blue: 0}, 21.0, 3.7)); +planets.push(new Planet("Uranus", {red: 135, green: 205, blue: 240}, 29.0, 4.0)); +planets.push(new Planet("Neptune", {red: 30, green: 140, blue: 255}, 35.0, 4.2)); +planets.push(new Planet("Pluto", {red: 255, green: 255, blue: 255}, 58.0, 3.2)); var labels = []; @@ -205,40 +198,13 @@ var LABEL_Y = 3.0; var LABEL_Z = 1.0; var LABEL_DIST = 8.0; var TEXT_HEIGHT = 1.0; -var sunLabel; -function showLabels() { - labelsShowing = true; - for (var i = 0; i < NUM_PLANETS; i++) { - var properties = planet_properties[i]; - var text; - if (i == 0) { - text = "Mercury"; - } else if (i == 1) { - text = "Venus"; - } else if (i == 2) { - text = "Earth"; - } else if (i == 3) { - text = "Mars"; - } else if (i == 4) { - text = "Jupiter"; - } else if (i == 5) { - text = "Saturn"; - } else if (i == 6) { - text = "Uranus"; - } else if (i == 7) { - text = "Neptune"; - } - - text = text + " Speed: " + Vec3.length(properties.velocity).toFixed(2); - - var labelPos = Vec3.sum(planet_properties[i].position, { - x: 0.0, - y: LABEL_DIST, - z: LABEL_DIST - }); - var linePos = planet_properties[i].position; - labelLines.push(Entities.addEntity({ +var PlanetLabel = function(name, index) { + var text = name + " Speed: " + Vec3.length(planets[index].velocity).toFixed(2); + var labelPos = Vec3.sum(planets[index].position, { x: 0.0, y: LABEL_DIST, z: LABEL_DIST }); + var linePos = planets[i].position; + + this.line = Entities.addEntity({ type: "Line", position: linePos, dimensions: { @@ -246,7 +212,7 @@ function showLabels() { y: 20, z: 20 }, - lineWidth: 3.0, + lineWidth: LINE_WIDTH, color: { red: 255, green: 255, @@ -256,10 +222,12 @@ function showLabels() { x: 0, y: 0, z: 0 - }, computeLocalPoint(linePos, labelPos)] - })); + }, + computeLocalPoint(linePos, labelPos)], + visible: false + }); - labels.push(Entities.addEntity({ + this.label = Entities.addEntity({ type: "Text", text: text, lineHeight: TEXT_HEIGHT, @@ -274,33 +242,33 @@ function showLabels() { green: 10, blue: 10 }, - textColor: { - red: 255, - green: 255, - blue: 255 - }, - faceCamera: true - })); + faceCamera: true, + visible: false + }); + labelLines.push(line); + labels.push(label); + + this.show = function() { + this.showing = true; + Entities.editEntity(this.line, {visible = true}); + Entities.editEntity(this.label, {visible = true}); } -} -function hideLabels() { - labelsShowing = false; - Entities.deleteEntity(sunLabel); - - for (var i = 0; i < NUM_PLANETS; ++i) { - Entities.deleteEntity(labelLines[i]); - Entities.deleteEntity(labels[i]); + this.hide = function() { + this.showing = false; + Entities.editEntity(this.line, {visible = false}); + Entities.editEntity(this.label, {visible = false}); } - labels = []; - labelLines = []; -} +} + var time = 0.0; var elapsed; -var counter = 0; var dt = 1.0 / TIME_STEP; +var planetLines = []; +var trails = []; + function update(deltaTime) { if (paused) { return; @@ -308,51 +276,15 @@ function update(deltaTime) { deltaTime = dt; time++; - for (var i = 0; i < NUM_PLANETS; ++i) { - var properties = planet_properties[i]; - var between = Vec3.subtract(properties.position, center); - var speed = getAcceleration(properties.radius) * deltaTime; - var vel = Vec3.multiply(speed, Vec3.normalize(between)); + for (var i = 0; i < planets.length; ++i) { + planets[i].update(); - // Update velocity and position - properties.velocity = Vec3.sum(properties.velocity, vel); - properties.position = Vec3.sum(properties.position, Vec3.multiply(properties.velocity, deltaTime)); - Entities.editEntity(planets[i], { - velocity: properties.velocity, - position: properties.position - }); - - - // Create new or update current trail if (trailsEnabled) { - var lineStack = planetLines[i]; - var point = properties.position; - var prop = Entities.getEntityProperties(lineStack[lineStack.length - 1]); - var linePos = prop.position; - - trails[i].push(computeLocalPoint(linePos, point)); - - Entities.editEntity(lineStack[lineStack.length - 1], { - linePoints: trails[i] - }); - if (trails[i].length === MAX_POINTS_PER_LINE) { - trails[i] = newLine(lineStack, point, properties.period, properties.lineColor); - } - } - - // Measure total energy every 10 updates, recalibrate velocity if necessary - if (energyConserved) { - if (counter % 10 === 0) { - var error = calcEnergyError(planets[i], properties.radius, properties.v0, properties.velocity, properties.position); - if (Math.abs(error) >= ERROR_THRESH) { - var speed = adjustVelocity(planets[i], properties.position); - properties.velocity = Vec3.multiply(speed, Vec3.normalize(properties.velocity)); - } - } + planets[i].updateTrails(); } + } - counter++; if (time % TIME_STEP == 0) { elapsed++; } @@ -363,23 +295,6 @@ function computeLocalPoint(linePos, worldPoint) { return localPoint; } -function getAcceleration(radius) { - var acc = -(G * M) * Math.pow(radius, (-2.0)); - return acc; -} - -// Create a new trail -function resetTrails(planetIndex) { - elapsed = 0.0; - var properties = planet_properties[planetIndex]; - - var trail = []; - var lineStack = []; - - //add the first line to both the line entity stack and the trail - trails.push(newLine(lineStack, properties.position, properties.period, properties.lineColor)); - planetLines.push(lineStack); -} // Create a new line function newLine(lineStack, point, period, color) { @@ -416,43 +331,6 @@ function newLine(lineStack, point, period, color) { return points; } -// Measure energy error, recalculate velocity to return to initial net energy -var totalEnergy; -var measuredEnergy; -var measuredPE; - -function calcEnergyError(planet, radius, v0, v, pos) { - totalEnergy = 0.5 * M * Math.pow(v0, 2.0) - ((G * M * m) / radius); - measuredEnergy = 0.5 * M * Math.pow(v, 2.0) - ((G * M * m) / Vec3.length(Vec3.subtract(center, pos))); - var error = ((measuredEnergy - totalEnergy) / totalEnergy) * 100; - return error; -} - -function adjustVelocity(planet, pos) { - var measuredPE = -(G * M * m) / Vec3.length(Vec3.subtract(center, pos)); - return Math.sqrt(2 * (totalEnergy - measuredPE) / M); -} - - -// Allow user to toggle pausing the model, switch to planet view -var pauseButton = Overlays.addOverlay("text", { - backgroundColor: { - red: 200, - green: 200, - blue: 255 - }, - text: "Pause", - x: PANEL_X, - y: PANEL_Y - 30, - width: 70, - height: 20, - alpha: 1.0, - backgroundAlpha: 0.5, - visible: true -}); - -var paused = false; - function mousePressEvent(event) { var clickedOverlay = Overlays.getOverlayAtPoint({ x: event.x, @@ -506,6 +384,7 @@ function keyPressEvent(event) { } } +//switch to planet view function mouseDoublePressEvent(event) { if (earthView) { return; @@ -543,14 +422,20 @@ function mouseDoublePressEvent(event) { } } +// UI ELEMENTS + + +// USE FLOATING UI PANEL TO IMPROVE UI EXPERIENCE + +var paused = false; + // Create UI panel var panel = new Panel(PANEL_X, PANEL_Y); -var panelItems = []; var g_multiplier = 1.0; -panelItems.push(panel.newSlider("Adjust Gravitational Force: ", 0.1, 5.0, +panel.newSlider("Adjust Gravitational Force: ", 0.1, 5.0, function(value) { g_multiplier = value; G = G_ref * g_multiplier; @@ -565,7 +450,17 @@ panelItems.push(panel.newSlider("Adjust Gravitational Force: ", 0.1, 5.0, var period_multiplier = 1.0; var last_alpha = period_multiplier; -panelItems.push(panel.newSlider("Adjust Orbital Period: ", 0.1, 3.0, + +panel.newSubPanel("Adjust Orbital Periods"); +/* + + TODO: Loop through all planets, creating new sliders and adjusting their respective orbital periods + + +*/ + +for (var i = 0; i < planets.length; ++i) +panel.newSlider("Adjust Orbital Period: ", 0.1, 3.0, function(value) { period_multiplier = value; changePeriod(period_multiplier); @@ -577,7 +472,7 @@ panelItems.push(panel.newSlider("Adjust Orbital Period: ", 0.1, 3.0, return (value).toFixed(2) + "x"; })); -panelItems.push(panel.newCheckbox("Leave Trails: ", +panel.newCheckbox("Leave Trails: ", function(value) { trailsEnabled = value; if (trailsEnabled) { @@ -588,7 +483,7 @@ panelItems.push(panel.newCheckbox("Leave Trails: ", } else if (planetLines.length != 0) { for (var i = 0; i < NUM_PLANETS; ++i) { for (var j = 0; j < planetLines[i].length; ++j) { - Entities.deleteEntity(planetLines[i][j]); + Entities.editEntity(planetLines[i][j], {visible: false}); } planetLines[i] = []; } @@ -601,40 +496,18 @@ panelItems.push(panel.newCheckbox("Leave Trails: ", return value; })); -panelItems.push(panel.newCheckbox("Energy Error Calculations: ", - function(value) { - energyConserved = value; - }, - function() { - return energyConserved; - }, - function(value) { - return value; - })); -// Update global G constant, period, poke velocity to new value -function changePeriod(alpha) { - var ratio = last_alpha / alpha; - G = Math.pow(ratio, 2.0) * G; - for (var i = 0; i < NUM_PLANETS; ++i) { - var properties = planet_properties[i]; - properties.period = ratio * properties.period; - properties.velocity = Vec3.multiply(ratio, properties.velocity); - - } - last_alpha = alpha; -} // Clean up models, UI panels, lines, and button overlays function scriptEnding() { - satelliteGame.endGame(); Entities.deleteEntity(theSun); for (var i = 0; i < NUM_PLANETS; ++i) { Entities.deleteEntity(planets[i]); } + Menu.removeMenu("Developer > Scene"); panel.destroy(); Overlays.deleteOverlay(pauseButton); From 861c9f0ca0f5f38ef3246bde71f045e733f4439e Mon Sep 17 00:00:00 2001 From: bwent Date: Wed, 5 Aug 2015 17:00:15 -0700 Subject: [PATCH 02/12] more refactoring --- examples/example/solarsystem.js | 426 ++++++++++++++++---------------- 1 file changed, 218 insertions(+), 208 deletions(-) diff --git a/examples/example/solarsystem.js b/examples/example/solarsystem.js index 8711014335..861a805a84 100644 --- a/examples/example/solarsystem.js +++ b/examples/example/solarsystem.js @@ -19,36 +19,40 @@ // Script.include([ - '../utilities/tools/cookies.js'); + '../utilities/tools/cookies.js', 'games/satellite.js' ]); +var DAMPING = 0.0; +var LIFETIME = 6000; var BASE_URL = "https://s3.amazonaws.com/hifi-public/marketplace/hificontent/Scripts/planets/planets/"; -var BOUNDS = 200; - -// Alert user to move if they are too close to domain bounds -if (MyAvatar.position.x < BOUNDS || MyAvatar.position.x > TREE_SCALE - BOUNDS || MyAvatar.position.y < BOUNDS || MyAvatar.position.y > TREE_SCALE - BOUNDS || MyAvatar.position.z < BOUNDS || MyAvatar.position.z > TREE_SCALE - BOUNDS) { - Window.alert("Please move at least 200m away from domain bounds."); - return; -} - // Save intiial avatar and camera position var startingPosition = {x: 800, y: 800, z: 800}; MyAvatar.position = startingPosition; var startFrame = Window.location.href; -/* +var panelPosition = {x: 300, y: 300}; +var panelWidth = 100; +var panelHeight = 300; -********************************** -TODO: create initial UI panel here +var panelBackground = Overlays.addOverlay("text", { + backgroundColor: { + red: 200, + green: 200, + blue: 255 + }, + x: panelPosition.x, + y: panelPosition.y, + width: panelWidth, + height: panelHeight, + alpha: 1.0, + backgroundAlpha: 0.5, visible: true +}); -*/ - - // Place the sun var MAX_RANGE = 80.0; var SUN_SIZE = 8.0; @@ -63,6 +67,11 @@ var theSun = Entities.addEntity({ y: SUN_SIZE, z: SUN_SIZE }, + angularVelocity: { + x: 0.0, + y: 0.1, + z: 0.0 + }, angularDamping: DAMPING, damping: DAMPING, ignoreCollisions: false, @@ -80,13 +89,19 @@ var SMALL_BODY_MASS = LARGE_BODY_MASS * 0.000000333; var GRAVITY = (Math.pow(referenceRadius, 3.0) / Math.pow((referencePeriod / (2.0 * Math.PI)), 2.0)) / LARGE_BODY_MASS; var REFERENCE_GRAVITY = GRAVITY; +var planets = []; var planetCount = 0; -var trails = []; + +var TRAILS_ENABLED = true; +var MAX_POINTS_PER_LINE = 20; var Planet = function(name, trailColor, radius, size) { this.index = 0; this.name = name; this.trailColor = trailColor; + + this.trail = []; + this.lineStack = []; this.radius = radius; this.position = Vec3.sum(center, { x: this.radius, y: 0.0, z: 0.0 }); @@ -97,7 +112,7 @@ var Planet = function(name, trailColor, radius, size) { x: 0, y: -0.2, z: 0.9 - }); + })); this.dimensions = size; this.planet = Entities.addEntity({ @@ -115,28 +130,29 @@ var Planet = function(name, trailColor, radius, size) { ignoreCollisions: false, lifetime: LIFETIME, collisionsWillMove: true, - })); - - this.label = new PlanetLabel(name, this.index); + }); - this.computeAcceleration = function() { var acc = -(this.gravity * LARGE_BODY_MASS) * Math.pow(this.radius, (-2.0)); return acc; }; - this.update = function(deltaTime) { + this.update = function(timeStep) { var between = Vec3.subtract(this.position, center); - var speed = this.computeAcceleration(this.radius) * deltaTime; + var speed = this.computeAcceleration(this.radius) * timeStep; var vel = Vec3.multiply(speed, Vec3.normalize(between)); // Update velocity and position this.velocity = Vec3.sum(this.velocity, vel); - this.position = Vec3.sum(this.position, Vec3.multiply(this.velocity, deltaTime)); + this.position = Vec3.sum(this.position, Vec3.multiply(timeStep, this.velocity)); Entities.editEntity(this.planet, { - velocity: this..velocity, - position: this..position + velocity: this.velocity, + position: this.position }); + + if (TRAILS_ENABLED) { + this.updateTrail(); + } }; this.resetTrails = function() { @@ -145,24 +161,23 @@ var Planet = function(name, trailColor, radius, size) { this.trail = []; this.lineStack = []; //add the first line to both the line entity stack and the trail - trails.push(newLine(lineStack, this.position, this.period, this.lineColor)); - planetLines.push(lineStack); + this.trail.push(newLine(this.lineStack, this.position, this.period, this.lineColor)); }; - this.updateTrails = function() { + this.updateTrail = function() { var point = this.position; var prop = Entities.getEntityProperties(this.lineStack[this.lineStack.length - 1]); var linePos = prop.position; - trails[index].push(computeLocalPoint(linePos, point)); + this.trail.push(computeLocalPoint(linePos, point)); - Entities.editEntity(lineStack[lineStack.length - 1], { - linePoints: trails[i] + Entities.editEntity(this.lineStack[this.lineStack.length - 1], { + linePoints: this.trail }); - if (trails[index].length === MAX_POINTS_PER_LINE) { - trails[index] = newLine(lineStack, point, this.period, this.lineColor); + if (this.trail.length === MAX_POINTS_PER_LINE) { + this.trail = newLine(this.lineStack, point, this.period, this.lineColor); } }; @@ -250,41 +265,37 @@ var PlanetLabel = function(name, index) { this.show = function() { this.showing = true; - Entities.editEntity(this.line, {visible = true}); - Entities.editEntity(this.label, {visible = true}); + Entities.editEntity(this.line, {visible: true}); + Entities.editEntity(this.label, {visible: true}); } this.hide = function() { this.showing = false; - Entities.editEntity(this.line, {visible = false}); - Entities.editEntity(this.label, {visible = false}); + Entities.editEntity(this.line, {visible: false}); + Entities.editEntity(this.label, {visible: false}); } } var time = 0.0; var elapsed; +var TIME_STEP = 60.0; var dt = 1.0 / TIME_STEP; var planetLines = []; var trails = []; + function update(deltaTime) { - if (paused) { - return; - } + // if (paused) { + // return; + // } deltaTime = dt; time++; for (var i = 0; i < planets.length; ++i) { - planets[i].update(); - - if (trailsEnabled) { - planets[i].updateTrails(); - } - + planets[i].update(deltaTime); } - if (time % TIME_STEP == 0) { elapsed++; } @@ -295,7 +306,6 @@ function computeLocalPoint(linePos, worldPoint) { return localPoint; } - // Create a new line function newLine(lineStack, point, period, color) { if (elapsed < period) { @@ -331,170 +341,170 @@ function newLine(lineStack, point, period, color) { return points; } -function mousePressEvent(event) { - var clickedOverlay = Overlays.getOverlayAtPoint({ - x: event.x, - y: event.y - }); - if (clickedOverlay == pauseButton) { - paused = !paused; - for (var i = 0; i < NUM_PLANETS; ++i) { - Entities.editEntity(planets[i], { - velocity: { - x: 0.0, - y: 0.0, - z: 0.0 - } - }); - } - if (paused && !labelsShowing) { - Overlays.editOverlay(pauseButton, { - text: "Paused", - backgroundColor: { - red: 255, - green: 50, - blue: 50 - } - }); - showLabels(); - } - if (paused == false && labelsShowing) { - Overlays.editOverlay(pauseButton, { - text: "Pause", - backgroundColor: { - red: 200, - green: 200, - blue: 255 - } - }); - hideLabels(); - } - planetView = false; - } -} +// function mousePressEvent(event) { +// var clickedOverlay = Overlays.getOverlayAtPoint({ +// x: event.x, +// y: event.y +// }); +// if (clickedOverlay == pauseButton) { +// paused = !paused; +// for (var i = 0; i < NUM_PLANETS; ++i) { +// Entities.editEntity(planets[i], { +// velocity: { +// x: 0.0, +// y: 0.0, +// z: 0.0 +// } +// }); +// } +// if (paused && !labelsShowing) { +// Overlays.editOverlay(pauseButton, { +// text: "Paused", +// backgroundColor: { +// red: 255, +// green: 50, +// blue: 50 +// } +// }); +// showLabels(); +// } +// if (paused == false && labelsShowing) { +// Overlays.editOverlay(pauseButton, { +// text: "Pause", +// backgroundColor: { +// red: 200, +// green: 200, +// blue: 255 +// } +// }); +// hideLabels(); +// } +// planetView = false; +// } +// } -function keyPressEvent(event) { - // Jump back to solar system view - if (event.text == "TAB" && planetView) { - if (earthView) { - satelliteGame.endGame(); - earthView = false; - } - MyAvatar.position = startingPosition; - } -} +// function keyPressEvent(event) { +// // Jump back to solar system view +// if (event.text == "TAB" && planetView) { +// if (earthView) { +// satelliteGame.endGame(); +// earthView = false; +// } +// MyAvatar.position = startingPosition; +// } +// } -//switch to planet view -function mouseDoublePressEvent(event) { - if (earthView) { - return; - } - var pickRay = Camera.computePickRay(event.x, event.y) - var rayPickResult = Entities.findRayIntersection(pickRay, true); +// //switch to planet view +// function mouseDoublePressEvent(event) { +// if (earthView) { +// return; +// } +// var pickRay = Camera.computePickRay(event.x, event.y) +// var rayPickResult = Entities.findRayIntersection(pickRay, true); - for (var i = 0; i < NUM_PLANETS; ++i) { - if (rayPickResult.entityID === labels[i]) { - planetView = true; - if (i == 2) { - MyAvatar.position = Vec3.sum(center, { - x: 200, - y: 200, - z: 200 - }); - Camera.setPosition(Vec3.sum(center, { - x: 200, - y: 200, - z: 200 - })); - earthView = true; - satelliteGame = new SatelliteGame(); +// for (var i = 0; i < NUM_PLANETS; ++i) { +// if (rayPickResult.entityID === labels[i]) { +// planetView = true; +// if (i == 2) { +// MyAvatar.position = Vec3.sum(center, { +// x: 200, +// y: 200, +// z: 200 +// }); +// Camera.setPosition(Vec3.sum(center, { +// x: 200, +// y: 200, +// z: 200 +// })); +// earthView = true; +// satelliteGame = new SatelliteGame(); - } else { - MyAvatar.position = Vec3.sum({ - x: 0.0, - y: 0.0, - z: 3.0 - }, planet_properties[i].position); - Camera.lookAt(planet_properties[i].position); - } - break; - } - } -} +// } else { +// MyAvatar.position = Vec3.sum({ +// x: 0.0, +// y: 0.0, +// z: 3.0 +// }, planet_properties[i].position); +// Camera.lookAt(planet_properties[i].position); +// } +// break; +// } +// } +// } -// UI ELEMENTS +// // UI ELEMENTS -// USE FLOATING UI PANEL TO IMPROVE UI EXPERIENCE +// // USE FLOATING UI PANEL TO IMPROVE UI EXPERIENCE -var paused = false; +// var paused = false; -// Create UI panel -var panel = new Panel(PANEL_X, PANEL_Y); +// // Create UI panel +// var panel = new Panel(PANEL_X, PANEL_Y); -var g_multiplier = 1.0; -panel.newSlider("Adjust Gravitational Force: ", 0.1, 5.0, - function(value) { - g_multiplier = value; - G = G_ref * g_multiplier; - }, +// var g_multiplier = 1.0; +// panel.newSlider("Adjust Gravitational Force: ", 0.1, 5.0, +// function(value) { +// g_multiplier = value; +// G = G_ref * g_multiplier; +// }, - function() { - return g_multiplier; - }, - function(value) { - return value.toFixed(1) + "x"; - })); +// function() { +// return g_multiplier; +// }, +// function(value) { +// return value.toFixed(1) + "x"; +// }); -var period_multiplier = 1.0; -var last_alpha = period_multiplier; +// var period_multiplier = 1.0; +// var last_alpha = period_multiplier; -panel.newSubPanel("Adjust Orbital Periods"); -/* +// panel.newSubPanel("Adjust Orbital Periods"); +// /* - TODO: Loop through all planets, creating new sliders and adjusting their respective orbital periods +// TODO: Loop through all planets, creating new sliders and adjusting their respective orbital periods -*/ +// */ -for (var i = 0; i < planets.length; ++i) -panel.newSlider("Adjust Orbital Period: ", 0.1, 3.0, - function(value) { - period_multiplier = value; - changePeriod(period_multiplier); - }, - function() { - return period_multiplier; - }, - function(value) { - return (value).toFixed(2) + "x"; - })); +// for (var i = 0; i < planets.length; ++i) +// panel.newSlider("Adjust Orbital Period: ", 0.1, 3.0, +// function(value) { +// period_multiplier = value; +// changePeriod(period_multiplier); +// }, +// function() { +// return period_multiplier; +// }, +// function(value) { +// return (value).toFixed(2) + "x"; +// })); -panel.newCheckbox("Leave Trails: ", - function(value) { - trailsEnabled = value; - if (trailsEnabled) { - for (var i = 0; i < NUM_PLANETS; ++i) { - resetTrails(i); - } - //if trails are off and we've already created trails, remove existing trails - } else if (planetLines.length != 0) { - for (var i = 0; i < NUM_PLANETS; ++i) { - for (var j = 0; j < planetLines[i].length; ++j) { - Entities.editEntity(planetLines[i][j], {visible: false}); - } - planetLines[i] = []; - } - } - }, - function() { - return trailsEnabled; - }, - function(value) { - return value; - })); +// panel.newCheckbox("Leave Trails: ", +// function(value) { +// trailsEnabled = value; +// if (trailsEnabled) { +// for (var i = 0; i < NUM_PLANETS; ++i) { +// resetTrails(i); +// } +// //if trails are off and we've already created trails, remove existing trails +// } else if (planetLines.length != 0) { +// for (var i = 0; i < NUM_PLANETS; ++i) { +// for (var j = 0; j < planetLines[i].length; ++j) { +// Entities.editEntity(planetLines[i][j], {visible: false}); +// } +// planetLines[i] = []; +// } +// } +// }, +// function() { +// return trailsEnabled; +// }, +// function(value) { +// return value; +// })); @@ -522,21 +532,21 @@ function scriptEnding() { }; -Controller.mouseMoveEvent.connect(function panelMouseMoveEvent(event) { - return panel.mouseMoveEvent(event); -}); -Controller.mousePressEvent.connect(function panelMousePressEvent(event) { - return panel.mousePressEvent(event); -}); -Controller.mouseDoublePressEvent.connect(function panelMouseDoublePressEvent(event) { - return panel.mouseDoublePressEvent(event); -}); -Controller.mouseReleaseEvent.connect(function(event) { - return panel.mouseReleaseEvent(event); -}); -Controller.mousePressEvent.connect(mousePressEvent); -Controller.mouseDoublePressEvent.connect(mouseDoublePressEvent); -Controller.keyPressEvent.connect(keyPressEvent); +// Controller.mouseMoveEvent.connect(function panelMouseMoveEvent(event) { +// return panel.mouseMoveEvent(event); +// }); +// Controller.mousePressEvent.connect(function panelMousePressEvent(event) { +// return panel.mousePressEvent(event); +// }); +// Controller.mouseDoublePressEvent.connect(function panelMouseDoublePressEvent(event) { +// return panel.mouseDoublePressEvent(event); +// }); +// Controller.mouseReleaseEvent.connect(function(event) { +// return panel.mouseReleaseEvent(event); +// }); +// Controller.mousePressEvent.connect(mousePressEvent); +// Controller.mouseDoublePressEvent.connect(mouseDoublePressEvent); +// Controller.keyPressEvent.connect(keyPressEvent); Script.scriptEnding.connect(scriptEnding); Script.update.connect(update); \ No newline at end of file From e61ba997e5e9fc41a03ec3ca6b69a3c8e38f1ae5 Mon Sep 17 00:00:00 2001 From: bwent Date: Thu, 6 Aug 2015 10:04:30 -0700 Subject: [PATCH 03/12] Fix trail functionality with new classes --- examples/example/solarsystem.js | 73 +++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 27 deletions(-) diff --git a/examples/example/solarsystem.js b/examples/example/solarsystem.js index 861a805a84..e873383295 100644 --- a/examples/example/solarsystem.js +++ b/examples/example/solarsystem.js @@ -54,7 +54,7 @@ var panelBackground = Overlays.addOverlay("text", { // Place the sun -var MAX_RANGE = 80.0; +var MAX_RANGE = 50.0; var SUN_SIZE = 8.0; var center = Vec3.sum(startingPosition, Vec3.multiply(MAX_RANGE, Quat.getFront(Camera.getOrientation()))); @@ -93,10 +93,17 @@ var planets = []; var planetCount = 0; var TRAILS_ENABLED = true; -var MAX_POINTS_PER_LINE = 20; +var MAX_POINTS_PER_LINE = 50; +var LINE_DIM = 200; +var LINE_WIDTH = 20; + +var VELOCITY_OFFSET_Y = -0.3; +var VELOCITY_OFFSET_Z = 0.9; + +var index = 0; var Planet = function(name, trailColor, radius, size) { - this.index = 0; + this.name = name; this.trailColor = trailColor; @@ -110,8 +117,8 @@ var Planet = function(name, trailColor, radius, size) { this.initialVelocity = Math.sqrt((GRAVITY * LARGE_BODY_MASS) / radius); this.velocity = Vec3.multiply(this.initialVelocity, Vec3.normalize({ x: 0, - y: -0.2, - z: 0.9 + y: VELOCITY_OFFSET_Y, + z: VELOCITY_OFFSET_Z })); this.dimensions = size; @@ -131,7 +138,7 @@ var Planet = function(name, trailColor, radius, size) { lifetime: LIFETIME, collisionsWillMove: true, }); - + this.computeAcceleration = function() { var acc = -(this.gravity * LARGE_BODY_MASS) * Math.pow(this.radius, (-2.0)); return acc; @@ -161,23 +168,23 @@ var Planet = function(name, trailColor, radius, size) { this.trail = []; this.lineStack = []; //add the first line to both the line entity stack and the trail - this.trail.push(newLine(this.lineStack, this.position, this.period, this.lineColor)); + this.trail.push(newLine(this.lineStack, this.position, this.period, this.trailColor)); }; this.updateTrail = function() { + var point = this.position; - - var prop = Entities.getEntityProperties(this.lineStack[this.lineStack.length - 1]); - var linePos = prop.position; + var linePos = Entities.getEntityProperties(this.lineStack[this.lineStack.length - 1]).position; this.trail.push(computeLocalPoint(linePos, point)); Entities.editEntity(this.lineStack[this.lineStack.length - 1], { - linePoints: this.trail + linePoints: this.trail }); - if (this.trail.length === MAX_POINTS_PER_LINE) { - this.trail = newLine(this.lineStack, point, this.period, this.lineColor); + + if (this.trail.length == MAX_POINTS_PER_LINE) { + this.trail = newLine(this.lineStack, point, this.period, this.trailColor); } }; @@ -191,19 +198,31 @@ var Planet = function(name, trailColor, radius, size) { this.last_alpha = alpha; } - this.index++; + index++; + this.resetTrails(); + } -planets.push(new Planet("Mercury", {red: 255, green: 255, blue: 255}, 7.0, 1.0)); -planets.push(new Planet("Venus", {red: 255, green: 160, blue: 110}, 8.0, 1.2)); -planets.push(new Planet("Earth", {red: 10, green: 150, blue: 160}, 9.2, 1.6)); -planets.push(new Planet("Mars", {red: 180, green: 70, blue: 10}, 11.0, 2.0)); -planets.push(new Planet("Jupiter", {red: 250, green: 140, blue: 0}, 14.5, 4.3)); -planets.push(new Planet("Saturn", {red: 235, green: 215, blue: 0}, 21.0, 3.7)); -planets.push(new Planet("Uranus", {red: 135, green: 205, blue: 240}, 29.0, 4.0)); -planets.push(new Planet("Neptune", {red: 30, green: 140, blue: 255}, 35.0, 4.2)); -planets.push(new Planet("Pluto", {red: 255, green: 255, blue: 255}, 58.0, 3.2)); +var MERCURY_LINE_COLOR = {red: 255, green: 255, blue: 255}; +var VENUS_LINE_COLOR = {red: 255, green: 160, blue: 110}; +var EARTH_LINE_COLOR = {red: 10, green: 150, blue: 160}; +var MARS_LINE_COLOR = {red: 180, green: 70, blue: 10}; +var JUPITER_LINE_COLOR = {red: 250, green: 140, blue: 0}; +var SATURN_LINE_COLOR = {red: 235, green: 215, blue: 0}; +var URANUS_LINE_COLOR = {red: 135, green: 205, blue: 240}; +var NEPTUNE_LINE_COLOR = {red: 30, green: 140, blue: 255}; +var PLUTO_LINE_COLOR = {red: 255, green: 255, blue: 255}; + +planets.push(new Planet("mercury", MERCURY_LINE_COLOR, 7.0, 1.0)); +planets.push(new Planet("venus", VENUS_LINE_COLOR, 8.5, 1.2)); +planets.push(new Planet("earth", EARTH_LINE_COLOR, 10.2, 1.6)); +planets.push(new Planet("mars", MARS_LINE_COLOR, 16.0, 2.0)); +planets.push(new Planet("jupiter", JUPITER_LINE_COLOR, 19.5, 4.3)); +planets.push(new Planet("saturn", SATURN_LINE_COLOR, 29.0, 3.7)); +planets.push(new Planet("uranus", URANUS_LINE_COLOR, 37.0, 4.0)); +planets.push(new Planet("neptune", NEPTUNE_LINE_COLOR, 55.0, 4.2)); +planets.push(new Planet("pluto", PLUTO_LINE_COLOR, 80.0, 3.2)); var labels = []; var labelLines = []; @@ -285,20 +304,20 @@ var dt = 1.0 / TIME_STEP; var planetLines = []; var trails = []; - function update(deltaTime) { // if (paused) { // return; // } deltaTime = dt; time++; + if (time % TIME_STEP == 0) { + elapsed++; + } for (var i = 0; i < planets.length; ++i) { planets[i].update(deltaTime); } - if (time % TIME_STEP == 0) { - elapsed++; - } + } function computeLocalPoint(linePos, worldPoint) { From c585f24e27b41123fccd074e02e5a4ca62d2f690 Mon Sep 17 00:00:00 2001 From: bwent Date: Tue, 11 Aug 2015 17:07:27 -0700 Subject: [PATCH 04/12] Refining UI, introduce tween functionality for zooming into planets --- examples/example/games/satellite.js | 4 +- examples/example/solarsystem.js | 873 ++++++++++++++++++---------- examples/utilities/tools/cookies.js | 2 +- 3 files changed, 562 insertions(+), 317 deletions(-) diff --git a/examples/example/games/satellite.js b/examples/example/games/satellite.js index f362c0c1e4..fd1243fbe0 100644 --- a/examples/example/games/satellite.js +++ b/examples/example/games/satellite.js @@ -20,6 +20,8 @@ Script.include('../utilities/tools/vector.js'); var URL = "https://s3.amazonaws.com/hifi-public/marketplace/hificontent/Scripts/planets/"; SatelliteGame = function() { + print("initializing satellite game"); + var MAX_RANGE = 50.0; var Y_AXIS = { x: 0, @@ -68,7 +70,7 @@ SatelliteGame = function() { this.clouds = Entities.addEntity({ type: "Model", shapeType: 'sphere', - modelURL: URL + "clouds.fbx?i=2", + modelURL: URL + "clouds.fbx", position: position, dimensions: { x: size + CLOUDS_OFFSET, diff --git a/examples/example/solarsystem.js b/examples/example/solarsystem.js index e873383295..196ad57f86 100644 --- a/examples/example/solarsystem.js +++ b/examples/example/solarsystem.js @@ -19,7 +19,6 @@ // Script.include([ - '../utilities/tools/cookies.js', 'games/satellite.js' ]); @@ -28,35 +27,19 @@ var LIFETIME = 6000; var BASE_URL = "https://s3.amazonaws.com/hifi-public/marketplace/hificontent/Scripts/planets/planets/"; // Save intiial avatar and camera position -var startingPosition = {x: 800, y: 800, z: 800}; +var startingPosition = { + x: 8000, + y: 8000, + z: 8000 +}; MyAvatar.position = startingPosition; -var startFrame = Window.location.href; - - -var panelPosition = {x: 300, y: 300}; -var panelWidth = 100; -var panelHeight = 300; - -var panelBackground = Overlays.addOverlay("text", { - backgroundColor: { - red: 200, - green: 200, - blue: 255 - }, - x: panelPosition.x, - y: panelPosition.y, - width: panelWidth, - height: panelHeight, - alpha: 1.0, - backgroundAlpha: 0.5, visible: true -}); - +var cameraStart = Camera.getOrientation(); // Place the sun -var MAX_RANGE = 50.0; -var SUN_SIZE = 8.0; -var center = Vec3.sum(startingPosition, Vec3.multiply(MAX_RANGE, Quat.getFront(Camera.getOrientation()))); +var MAX_RANGE = 80.0; +var center = Vec3.sum(startingPosition, Vec3.multiply(MAX_RANGE, Vec3.normalize(Quat.getFront(Camera.getOrientation())))); +var SUN_SIZE = 7.0; var theSun = Entities.addEntity({ type: "Model", @@ -82,8 +65,9 @@ var theSun = Entities.addEntity({ // Reference values for physical constants -var referenceRadius = 7.0; -var referencePeriod = 1.0; +var referenceRadius = 15.0; +var referenceDiameter = 0.6; +var referencePeriod = 3.0; var LARGE_BODY_MASS = 250.0; var SMALL_BODY_MASS = LARGE_BODY_MASS * 0.000000333; var GRAVITY = (Math.pow(referenceRadius, 3.0) / Math.pow((referencePeriod / (2.0 * Math.PI)), 2.0)) / LARGE_BODY_MASS; @@ -92,8 +76,8 @@ var REFERENCE_GRAVITY = GRAVITY; var planets = []; var planetCount = 0; -var TRAILS_ENABLED = true; -var MAX_POINTS_PER_LINE = 50; +var trailsEnabled = true; +var MAX_POINTS_PER_LINE = 60; var LINE_DIM = 200; var LINE_WIDTH = 20; @@ -102,43 +86,48 @@ var VELOCITY_OFFSET_Z = 0.9; var index = 0; -var Planet = function(name, trailColor, radius, size) { - +var Planet = function(name, trailColor, radiusScale, sizeScale) { + this.name = name; this.trailColor = trailColor; - + this.trail = []; this.lineStack = []; - this.radius = radius; - this.position = Vec3.sum(center, { x: this.radius, y: 0.0, z: 0.0 }); + this.radius = radiusScale * referenceRadius; + this.position = Vec3.sum(center, { + x: this.radius, + y: 0.0, + z: 0.0 + }); this.period = (2.0 * Math.PI) * Math.sqrt(Math.pow(this.radius, 3.0) / (GRAVITY * LARGE_BODY_MASS)); this.gravity = GRAVITY; - this.initialVelocity = Math.sqrt((GRAVITY * LARGE_BODY_MASS) / radius); + this.initialVelocity = Math.sqrt((GRAVITY * LARGE_BODY_MASS) / this.radius); this.velocity = Vec3.multiply(this.initialVelocity, Vec3.normalize({ - x: 0, - y: VELOCITY_OFFSET_Y, - z: VELOCITY_OFFSET_Z - })); - this.dimensions = size; + x: 0, + y: VELOCITY_OFFSET_Y, + z: VELOCITY_OFFSET_Z + })); + this.dimensions = sizeScale * referenceDiameter; + this.sizeScale = sizeScale; this.planet = Entities.addEntity({ - type: "Model", - modelURL: BASE_URL + name + ".fbx", - position: this.position, - dimensions: { - x: this.dimensions, - y: this.dimensions, - z: this.dimensions - }, - velocity: this.velocity, - angularDamping: DAMPING, - damping: DAMPING, - ignoreCollisions: false, - lifetime: LIFETIME, - collisionsWillMove: true, + type: "Model", + modelURL: BASE_URL + name + ".fbx", + position: this.position, + dimensions: { + x: this.dimensions, + y: this.dimensions, + z: this.dimensions + }, + velocity: this.velocity, + angularDamping: DAMPING, + damping: DAMPING, + ignoreCollisions: false, + lifetime: LIFETIME, + collisionsWillMove: true, }); - + this.computeAcceleration = function() { var acc = -(this.gravity * LARGE_BODY_MASS) * Math.pow(this.radius, (-2.0)); return acc; @@ -157,9 +146,9 @@ var Planet = function(name, trailColor, radius, size) { position: this.position }); - if (TRAILS_ENABLED) { + if (trailsEnabled) { this.updateTrail(); - } + } }; this.resetTrails = function() { @@ -173,7 +162,6 @@ var Planet = function(name, trailColor, radius, size) { }; this.updateTrail = function() { - var point = this.position; var linePos = Entities.getEntityProperties(this.lineStack[this.lineStack.length - 1]).position; @@ -188,45 +176,104 @@ var Planet = function(name, trailColor, radius, size) { } }; + this.zoom = function() { + Script.include('file:///Users/bridget/Desktop/tween.js'); + init(this); + // var viewingRange = sizeScale * 2.0; + // var direction = Vec3.subtract(this.position, MyAvatar.position); + // var dist = Vec3.length(direction); + // var timer = 0; + // while (dist > viewingRange && timer < 8000000) { + // timer++; + // if (timer % 10000 == 0) { + // MyAvatar.position = Vec3.sum(MyAvatar.position, Vec3.normalize(direction)); + // direction = Vec3.subtract(this.position, MyAvatar.position); + // dist = Vec3.length(direction); + // } + // } + }; + + this.adjustPeriod = function(alpha) { // Update global G constant, period, poke velocity to new value var ratio = this.last_alpha / alpha; this.gravity = Math.pow(ratio, 2.0) * GRAVITY; this.period = ratio * this.period; this.velocity = Vec3.multiply(ratio, this.velocity); - - this.last_alpha = alpha; - } - index++; - this.resetTrails(); + this.last_alpha = alpha; + }; + + + index++; + this.resetTrails(); } +// rideOrbit = function() { -var MERCURY_LINE_COLOR = {red: 255, green: 255, blue: 255}; -var VENUS_LINE_COLOR = {red: 255, green: 160, blue: 110}; -var EARTH_LINE_COLOR = {red: 10, green: 150, blue: 160}; -var MARS_LINE_COLOR = {red: 180, green: 70, blue: 10}; -var JUPITER_LINE_COLOR = {red: 250, green: 140, blue: 0}; -var SATURN_LINE_COLOR = {red: 235, green: 215, blue: 0}; -var URANUS_LINE_COLOR = {red: 135, green: 205, blue: 240}; -var NEPTUNE_LINE_COLOR = {red: 30, green: 140, blue: 255}; -var PLUTO_LINE_COLOR = {red: 255, green: 255, blue: 255}; +// Script.include('file:///Users/bridget/Desktop/tween.js'); +// init(); + +// } -planets.push(new Planet("mercury", MERCURY_LINE_COLOR, 7.0, 1.0)); -planets.push(new Planet("venus", VENUS_LINE_COLOR, 8.5, 1.2)); -planets.push(new Planet("earth", EARTH_LINE_COLOR, 10.2, 1.6)); -planets.push(new Planet("mars", MARS_LINE_COLOR, 16.0, 2.0)); -planets.push(new Planet("jupiter", JUPITER_LINE_COLOR, 19.5, 4.3)); -planets.push(new Planet("saturn", SATURN_LINE_COLOR, 29.0, 3.7)); -planets.push(new Planet("uranus", URANUS_LINE_COLOR, 37.0, 4.0)); -planets.push(new Planet("neptune", NEPTUNE_LINE_COLOR, 55.0, 4.2)); -planets.push(new Planet("pluto", PLUTO_LINE_COLOR, 80.0, 3.2)); -var labels = []; -var labelLines = []; -var labelsShowing = false; +var MERCURY_LINE_COLOR = { + red: 255, + green: 255, + blue: 255 +}; +var VENUS_LINE_COLOR = { + red: 255, + green: 160, + blue: 110 +}; +var EARTH_LINE_COLOR = { + red: 10, + green: 150, + blue: 160 +}; +var MARS_LINE_COLOR = { + red: 180, + green: 70, + blue: 10 +}; +var JUPITER_LINE_COLOR = { + red: 250, + green: 140, + blue: 0 +}; +var SATURN_LINE_COLOR = { + red: 235, + green: 215, + blue: 0 +}; +var URANUS_LINE_COLOR = { + red: 135, + green: 205, + blue: 240 +}; +var NEPTUNE_LINE_COLOR = { + red: 30, + green: 140, + blue: 255 +}; +var PLUTO_LINE_COLOR = { + red: 255, + green: 255, + blue: 255 +}; + +planets.push(new Planet("mercury", MERCURY_LINE_COLOR, 0.387, 0.383)); +planets.push(new Planet("venus", VENUS_LINE_COLOR, 0.723, 0.949)); +planets.push(new Planet("earth", EARTH_LINE_COLOR, 1.0, 1.0)); +planets.push(new Planet("mars", MARS_LINE_COLOR, 1.52, 0.532)); +planets.push(new Planet("jupiter", JUPITER_LINE_COLOR, 5.20, 11.21)); +planets.push(new Planet("saturn", SATURN_LINE_COLOR, 9.58, 9.45)); +planets.push(new Planet("uranus", URANUS_LINE_COLOR, 19.20, 4.01)); +planets.push(new Planet("neptune", NEPTUNE_LINE_COLOR, 30.05, 3.88)); +planets.push(new Planet("pluto", PLUTO_LINE_COLOR, 39.48, 0.186)); + var LABEL_X = 8.0; var LABEL_Y = 3.0; var LABEL_Z = 1.0; @@ -235,67 +282,78 @@ var TEXT_HEIGHT = 1.0; var PlanetLabel = function(name, index) { var text = name + " Speed: " + Vec3.length(planets[index].velocity).toFixed(2); - var labelPos = Vec3.sum(planets[index].position, { x: 0.0, y: LABEL_DIST, z: LABEL_DIST }); - var linePos = planets[i].position; - + this.labelPos = Vec3.sum(planets[index].position, { + x: 0.0, + y: LABEL_DIST, + z: LABEL_DIST + }); + this.linePos = planets[index].position; + this.line = Entities.addEntity({ - type: "Line", - position: linePos, - dimensions: { - x: 20, - y: 20, - z: 20 - }, - lineWidth: LINE_WIDTH, - color: { - red: 255, - green: 255, - blue: 255 - }, - linePoints: [{ + type: "Line", + position: this.linePos, + dimensions: { + x: 20, + y: 20, + z: 20 + }, + lineWidth: LINE_WIDTH, + color: { + red: 255, + green: 255, + blue: 255 + }, + linePoints: [{ x: 0, y: 0, z: 0 - }, - computeLocalPoint(linePos, labelPos)], - visible: false - }); + }, + computeLocalPoint(this.linePos, this.labelPos) + ], + visible: false + }); this.label = Entities.addEntity({ - type: "Text", - text: text, - lineHeight: TEXT_HEIGHT, - dimensions: { - x: LABEL_X, - y: LABEL_Y, - z: LABEL_Z - }, - position: labelPos, - backgroundColor: { - red: 10, - green: 10, - blue: 10 - }, - faceCamera: true, - visible: false - }); - labelLines.push(line); - labels.push(label); + type: "Text", + text: text, + lineHeight: TEXT_HEIGHT, + dimensions: { + x: LABEL_X, + y: LABEL_Y, + z: LABEL_Z + }, + position: this.labelPos, + backgroundColor: { + red: 10, + green: 10, + blue: 10 + }, + faceCamera: true, + visible: false + }); this.show = function() { this.showing = true; - Entities.editEntity(this.line, {visible: true}); - Entities.editEntity(this.label, {visible: true}); + Entities.editEntity(this.line, { + visible: true + }); + Entities.editEntity(this.label, { + visible: true + }); } this.hide = function() { this.showing = false; - Entities.editEntity(this.line, {visible: false}); - Entities.editEntity(this.label, {visible: false}); + Entities.editEntity(this.line, { + visible: false + }); + Entities.editEntity(this.label, { + visible: false + }); } - } - + + var time = 0.0; var elapsed; var TIME_STEP = 60.0; @@ -303,12 +361,13 @@ var dt = 1.0 / TIME_STEP; var planetLines = []; var trails = []; +var paused = false; function update(deltaTime) { - // if (paused) { - // return; - // } - deltaTime = dt; + if (paused) { + return; + } + //deltaTime = dt; time++; if (time % TIME_STEP == 0) { elapsed++; @@ -317,7 +376,28 @@ function update(deltaTime) { for (var i = 0; i < planets.length; ++i) { planets[i].update(deltaTime); } - +} + +function pause() { + for (var i = 0; i < planets.length; ++i) { + Entities.editEntity(planets[i].planet, { + velocity: { + x: 0.0, + y: 0.0, + z: 0.0 + } + }); + planets[i].label = new PlanetLabel(planets[i].name, i); + planets[i].label.show(); + } + paused = true; +} + +function resume() { + for (var i = 0; i < planets.length; ++i) { + planets[i].label.hide(); + } + paused = false; } function computeLocalPoint(linePos, worldPoint) { @@ -360,187 +440,345 @@ function newLine(lineStack, point, period, color) { return points; } -// function mousePressEvent(event) { -// var clickedOverlay = Overlays.getOverlayAtPoint({ -// x: event.x, -// y: event.y -// }); -// if (clickedOverlay == pauseButton) { -// paused = !paused; -// for (var i = 0; i < NUM_PLANETS; ++i) { -// Entities.editEntity(planets[i], { -// velocity: { -// x: 0.0, -// y: 0.0, -// z: 0.0 -// } -// }); -// } -// if (paused && !labelsShowing) { -// Overlays.editOverlay(pauseButton, { -// text: "Paused", -// backgroundColor: { -// red: 255, -// green: 50, -// blue: 50 -// } -// }); -// showLabels(); -// } -// if (paused == false && labelsShowing) { -// Overlays.editOverlay(pauseButton, { -// text: "Pause", -// backgroundColor: { -// red: 200, -// green: 200, -// blue: 255 -// } -// }); -// hideLabels(); -// } -// planetView = false; -// } -// } -// function keyPressEvent(event) { -// // Jump back to solar system view -// if (event.text == "TAB" && planetView) { -// if (earthView) { -// satelliteGame.endGame(); -// earthView = false; -// } -// MyAvatar.position = startingPosition; -// } -// } -// //switch to planet view -// function mouseDoublePressEvent(event) { -// if (earthView) { -// return; -// } -// var pickRay = Camera.computePickRay(event.x, event.y) -// var rayPickResult = Entities.findRayIntersection(pickRay, true); +var spacing = 8; +var textWidth = 70; +var buttonWidth = 30; +var buttonHeight = 30; -// for (var i = 0; i < NUM_PLANETS; ++i) { -// if (rayPickResult.entityID === labels[i]) { -// planetView = true; -// if (i == 2) { -// MyAvatar.position = Vec3.sum(center, { -// x: 200, -// y: 200, -// z: 200 -// }); -// Camera.setPosition(Vec3.sum(center, { -// x: 200, -// y: 200, -// z: 200 -// })); -// earthView = true; -// satelliteGame = new SatelliteGame(); +var ICONS_URL = 'https://s3.amazonaws.com/hifi-public/marketplace/hificontent/Scripts/planets/images/'; -// } else { -// MyAvatar.position = Vec3.sum({ -// x: 0.0, -// y: 0.0, -// z: 3.0 -// }, planet_properties[i].position); -// Camera.lookAt(planet_properties[i].position); -// } -// break; -// } -// } -// } +var UIPanel = function(x, y, orientation) { + this.visible = false; + this.buttons = []; + this.x = x; + this.y = y; + this.nextX = x + spacing; + this.nextY = y + spacing; + this.width = spacing * 2.0; + this.height = spacing * 2.0; -// // UI ELEMENTS + this.background = Overlays.addOverlay("text", { + backgroundColor: { + red: 240, + green: 240, + blue: 255 + }, + x: this.x, + y: this.y, + width: this.width, + height: this.height, + alpha: 1.0, + backgroundAlpha: 0.7, + visible: false + }); + + this.addIcon = function(iconID) { + var icon = Overlays.addOverlay("image", { + color: { + red: 255, + green: 255, + blue: 255 + }, + imageURL: ICONS_URL + iconID + '.svg', + x: this.nextX, + y: this.nextY, + width: buttonWidth, + height: buttonHeight, + alpha: 1.0, + visible: false + }); + + + if (orientation === 'horizontal') { + this.nextX += buttonWidth + spacing; + this.width += buttonWidth; + + } else if (orientation === 'vertical') { + this.nextY += buttonHeight + spacing; + this.height += buttonHeight; + } + + Overlays.editOverlay(this.background, { + width: buttonWidth + this.width, + height: buttonHeight + this.height + }); + + this.buttons.push(icon); + return icon; + }; + + this.addText = function(text) { + var icon = Overlays.addOverlay("text", { + color: { + red: 240, + green: 240, + blue: 255 + }, + text: text, + x: this.nextX, + y: this.nextY, + width: textWidth, + height: buttonHeight, + visible: false + }); + + if (orientation === 'horizontal') { + this.nextX += textWidth + spacing; + this.width += textWidth; + + } else if (orientation === 'vertical') { + this.nextY += buttonHeight + spacing; + this.height += buttonHeight; + } + + Overlays.editOverlay(this.background, { + width: textWidth + this.width, + height: buttonHeight + this.height + }); + + this.buttons.push(icon); + return icon; + }; + + + this.show = function() { + Overlays.editOverlay(this.background, { + visible: true + }); + for (var i in this.buttons) { + Overlays.editOverlay(this.buttons[i], { + visible: true + }); + } + this.visible = true; + }; + + this.hide = function() { + Overlays.editOverlay(this.background, { + visible: false + }); + for (var i in this.buttons) { + Overlays.editOverlay(this.buttons[i], { + visible: false + }); + } + this.visible = false; + }; + + this.remove = function() { + Overlays.deleteOverlay(this.background); + for (var i in this.buttons) { + Overlays.deleteOverlay(this.buttons[i]); + } + }; + +} + +var panelX = 1250; +var panelY = 500; +var panelWidth = 50; +var panelHeight = 210; + +var mainPanel = new UIPanel(panelX, panelY, 'vertical'); + +var systemViewButton = mainPanel.addIcon('solarsystems'); +var systemViewPanel = new UIPanel(panelX - 140, panelY, 'horizontal'); +var reverseButton = systemViewPanel.addIcon('reverse'); +var pauseButton = systemViewPanel.addIcon('playpause'); +var forwardButton = systemViewPanel.addIcon('forward'); + +var zoomButton = mainPanel.addIcon('magnifier'); +var zoomPanel = new UIPanel(panelX - 900, panelY + (buttonHeight + spacing) * 4.0, 'horizontal'); +for (var i = 0; i < planets.length; ++i) { + zoomPanel.addText(planets[i].name); +} + +var satelliteButton = mainPanel.addIcon('satellite'); +var settingsButton = mainPanel.addIcon('settings'); +var stopButton = mainPanel.addIcon('close'); + +mainPanel.show(); + +Script.include('../utilities/tools/cookies.js'); + + +var satelliteView; +var satelliteGame; + var settings = new Panel(panelX - 610, panelY - 80); + + var g_multiplier = 1.0; + settings.newSlider("Gravitational Force ", 0.1, 5.0, + function(value) { + g_multiplier = value; + GRAVITY = REFERENCE_GRAVITY * g_multiplier; + }, + + function() { + return g_multiplier; + }, + function(value) { + return value.toFixed(1) + "x"; + }); + + + var subPanel = settings.newSubPanel("Orbital Periods"); + + for (var i = 0; i < planets.length; ++i) { + planets[i].period_multiplier = 1.0; + planets[i].last_alpha = planets[i].period_multiplier; + + subPanel.newSlider(planets[i].name, 0.1, 3.0, + function(value) { + planets[i].period_multiplier = value; + planets[i].adjustPeriod(value); + }, + function() { + return planets[i].period_multiplier; + }, + function(value) { + return (value).toFixed(2) + "x"; + }); + } + settings.newCheckbox("Leave Trails: ", + function(value) { + trailsEnabled = value; + if (trailsEnabled) { + for (var i = 0; i < planets.length; ++i) { + planets[i].resetTrails(); + } + //if trails are off and we've already created trails, remove existing trails + } else { + for (var i = 0; i < planets.length; ++i) { + for (var j = 0; j < planets[i].lineStack.length; ++j) { + Entities.editEntity(planets[i].lineStack[j], { + visible: false + }); + } + planets[i].lineStack = []; + } + } + }, + function() { + return trailsEnabled; + }, + function(value) { + return value; + }); + settings.hide(); + + +function mousePressEvent(event) { + + var clicked = Overlays.getOverlayAtPoint({ + x: event.x, + y: event.y + }); + + if (clicked == systemViewButton) { + MyAvatar.position = startingPosition; + Camera.setOrientation(cameraStart); + if (paused) { + resume(); + } + + if (!systemViewPanel.visible) { + systemViewPanel.show(); + } else { + systemViewPanel.hide(); + } + } + var zoomed = false; + if (clicked == zoomButton && !satelliteView) { + if (!zoomPanel.visible) { + zoomPanel.show(); + pause(); + } else { + zoomPanel.hide(); + if (zoomed) { + + MyAvatar.position = startingPosition; + Camera.setOrientation(cameraStart); + resume(); + } + + } + } + + for (var i = 0; i < planets.length; ++i) { + if (zoomPanel.visible && clicked == zoomPanel.buttons[i]) { + planets[i].zoom(); + zoomed = true; + break; + } + } + + if (systemViewPanel.visible && clicked == pauseButton) { + if (!paused) { + pause(); + } else { + resume(); + } + } + if (clicked == satelliteButton) { + if (satelliteView) { + satelliteGame.endGame(); + MyAvatar.position = startingPosition; + satelliteView = false; + resume(); + } else { + pause(); + var confirmed = Window.confirm("Start satellite game?"); + if (!confirmed) { + resume(); + continue; + } + satelliteView = true; + MyAvatar.position = { + x: 200, + y: 200, + z: 200 + }; + Camera.setPosition({ + x: 200, + y: 200, + z: 200 + }); + satelliteGame = new SatelliteGame(); + } + } -// // USE FLOATING UI PANEL TO IMPROVE UI EXPERIENCE - -// var paused = false; - -// // Create UI panel -// var panel = new Panel(PANEL_X, PANEL_Y); - -// var g_multiplier = 1.0; -// panel.newSlider("Adjust Gravitational Force: ", 0.1, 5.0, -// function(value) { -// g_multiplier = value; -// G = G_ref * g_multiplier; -// }, - -// function() { -// return g_multiplier; -// }, -// function(value) { -// return value.toFixed(1) + "x"; -// }); - -// var period_multiplier = 1.0; -// var last_alpha = period_multiplier; - -// panel.newSubPanel("Adjust Orbital Periods"); -// /* - -// TODO: Loop through all planets, creating new sliders and adjusting their respective orbital periods - - -// */ - -// for (var i = 0; i < planets.length; ++i) -// panel.newSlider("Adjust Orbital Period: ", 0.1, 3.0, -// function(value) { -// period_multiplier = value; -// changePeriod(period_multiplier); -// }, -// function() { -// return period_multiplier; -// }, -// function(value) { -// return (value).toFixed(2) + "x"; -// })); - -// panel.newCheckbox("Leave Trails: ", -// function(value) { -// trailsEnabled = value; -// if (trailsEnabled) { -// for (var i = 0; i < NUM_PLANETS; ++i) { -// resetTrails(i); -// } -// //if trails are off and we've already created trails, remove existing trails -// } else if (planetLines.length != 0) { -// for (var i = 0; i < NUM_PLANETS; ++i) { -// for (var j = 0; j < planetLines[i].length; ++j) { -// Entities.editEntity(planetLines[i][j], {visible: false}); -// } -// planetLines[i] = []; -// } -// } -// }, -// function() { -// return trailsEnabled; -// }, -// function(value) { -// return value; -// })); - + if (clicked == settingsButton) { + if (!settings.visible) { + settings.show(); + } else { + settings.hide(); + } + } + if (clicked == stopButton) { + Script.stop(); + } +} // Clean up models, UI panels, lines, and button overlays function scriptEnding() { - satelliteGame.endGame(); - + mainPanel.remove(); + systemViewPanel.remove(); + zoomPanel.remove(); + + settings.destroy(); + Entities.deleteEntity(theSun); - for (var i = 0; i < NUM_PLANETS; ++i) { - Entities.deleteEntity(planets[i]); + for (var i = 0; i < planets.length; ++i) { + Entities.deleteEntity(planets[i].planet); } - Menu.removeMenu("Developer > Scene"); - panel.destroy(); - Overlays.deleteOverlay(pauseButton); - var e = Entities.findEntities(MyAvatar.position, 16000); for (i = 0; i < e.length; i++) { var props = Entities.getEntityProperties(e[i]); @@ -550,22 +788,27 @@ function scriptEnding() { } }; +Controller.mousePressEvent.connect(mousePressEvent); + + Controller.mouseMoveEvent.connect(function panelMouseMoveEvent(event) { + return settings.mouseMoveEvent(event); + }); + Controller.mousePressEvent.connect(function panelMousePressEvent(event) { + return settings.mousePressEvent(event); + }); + Controller.mouseDoublePressEvent.connect(function panelMouseDoublePressEvent(event) { + return settings.mouseDoublePressEvent(event); + }); + Controller.mouseReleaseEvent.connect(function(event) { + return settings.mouseReleaseEvent(event); + }); + Controller.keyPressEvent.connect(function(event) { + return settings.keyPressEvent(event); + }); + + + -// Controller.mouseMoveEvent.connect(function panelMouseMoveEvent(event) { -// return panel.mouseMoveEvent(event); -// }); -// Controller.mousePressEvent.connect(function panelMousePressEvent(event) { -// return panel.mousePressEvent(event); -// }); -// Controller.mouseDoublePressEvent.connect(function panelMouseDoublePressEvent(event) { -// return panel.mouseDoublePressEvent(event); -// }); -// Controller.mouseReleaseEvent.connect(function(event) { -// return panel.mouseReleaseEvent(event); -// }); -// Controller.mousePressEvent.connect(mousePressEvent); -// Controller.mouseDoublePressEvent.connect(mouseDoublePressEvent); -// Controller.keyPressEvent.connect(keyPressEvent); Script.scriptEnding.connect(scriptEnding); Script.update.connect(update); \ No newline at end of file diff --git a/examples/utilities/tools/cookies.js b/examples/utilities/tools/cookies.js index 751008fd99..1a8695188b 100644 --- a/examples/utilities/tools/cookies.js +++ b/examples/utilities/tools/cookies.js @@ -1360,7 +1360,7 @@ var CHECK_MARK_COLOR = { this.nextY = this.y + this.getHeight(); - var item = new CollapsablePanelItem(name, this.x, this.nextY, textWidth, rawHeight, panel); + var item = new CollapsablePanelItem(name, this.x, this.nextY, textWidth, rawHeight); item.isSubPanel = true; this.nextY += 1.5 * item.height; From 8c47bcc141e59ce72bf6f141cfcd4c1ac77b4266 Mon Sep 17 00:00:00 2001 From: bwent Date: Thu, 13 Aug 2015 16:00:55 -0700 Subject: [PATCH 05/12] implement new UI --- examples/example/games/satellite.js | 47 +- examples/example/planets-ui.js | 458 +++++++++++ examples/example/solarsystem.js | 1088 ++++++++++----------------- examples/libraries/uiwidgets.js | 26 +- 4 files changed, 896 insertions(+), 723 deletions(-) create mode 100644 examples/example/planets-ui.js diff --git a/examples/example/games/satellite.js b/examples/example/games/satellite.js index fd1243fbe0..8e37472ea4 100644 --- a/examples/example/games/satellite.js +++ b/examples/example/games/satellite.js @@ -19,7 +19,8 @@ Script.include('../utilities/tools/vector.js'); var URL = "https://s3.amazonaws.com/hifi-public/marketplace/hificontent/Scripts/planets/"; -SatelliteGame = function() { +SatelliteCreator = function() { + print("initializing satellite game"); var MAX_RANGE = 50.0; @@ -38,6 +39,9 @@ SatelliteGame = function() { var ZONE_DIM = 100.0; var LIGHT_INTENSITY = 1.5; + var center, distance; + + Earth = function(position, size) { this.earth = Entities.addEntity({ type: "Model", @@ -109,10 +113,35 @@ SatelliteGame = function() { } } - // Create earth model - var center = Vec3.sum(Camera.getPosition(), Vec3.multiply(MAX_RANGE, Quat.getFront(Camera.getOrientation()))); - var distance = Vec3.length(Vec3.subtract(center, Camera.getPosition())); - var earth = new Earth(center, EARTH_SIZE); + + this.init = function() { + if (this.isActive) { + this.quitGame(); + } + var confirmed = Window.confirm("Start satellite game?"); + if (!confirmed) { + return false; + } + + this.isActive = true; + MyAvatar.position = { + x: 200, + y: 200, + z: 200 + }; + Camera.setPosition({ + x: 200, + y: 200, + z: 200 + }); + + // Create earth model + center = Vec3.sum(Camera.getPosition(), Vec3.multiply(MAX_RANGE, Quat.getFront(Camera.getOrientation()))); + distance = Vec3.length(Vec3.subtract(center, Camera.getPosition())); + var earth = new Earth(center, EARTH_SIZE); + return true; + }; + var satellites = []; var SATELLITE_SIZE = 2.0; @@ -259,11 +288,12 @@ SatelliteGame = function() { } } - this.endGame = function() { + this.quitGame = function() { for (var i = 0; i < satellites.length; i++) { Entities.deleteEntity(satellites[i].satellite); satellites[i].arrow.cleanup(); } + this.isActive = false; earth.cleanup(); } @@ -285,6 +315,7 @@ SatelliteGame = function() { Controller.mouseMoveEvent.connect(mouseMoveEvent); Controller.mouseReleaseEvent.connect(mouseReleaseEvent); Script.update.connect(update); - Script.scriptEnding.connect(this.endGame); + Script.scriptEnding.connect(this.quitGame); + +} -} \ No newline at end of file diff --git a/examples/example/planets-ui.js b/examples/example/planets-ui.js new file mode 100644 index 0000000000..56cf7b18e1 --- /dev/null +++ b/examples/example/planets-ui.js @@ -0,0 +1,458 @@ +// +// widgets-example.js +// games +// +// Copyright 2015 High Fidelity, Inc. +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +var ICONS_URL = 'https://s3.amazonaws.com/hifi-public/marketplace/hificontent/Scripts/planets/images/'; + +var panelX = 1250; +var panelY = 500; +var panelWidth = 50; +var panelHeight = 210; + +Script.include('../libraries/uiwidgets.js'); + +UI.setDefaultVisibility(true); + +var ICON_WIDTH = 40.0; +var ICON_HEIGHT = 40.0; +var ICON_COLOR = UI.rgba(45, 45, 45, 0.7); +var FOCUSED_COLOR = UI.rgba(250, 250, 250, 1.0); + +var PANEL_BACKGROUND_COLOR = UI.rgba(100, 100, 100, 0.7); + +var PANEL_PADDING = 7.0; +var PANEL_BORDER = 12.0; +var SUBPANEL_GAP = 1.0; + +var icons = []; + +function addImage(panel, iconId) { + var icon = panel.add(new UI.Image({ + 'imageURL': ICONS_URL + iconId + '.svg', + 'width': ICON_WIDTH, + 'height': ICON_HEIGHT, + 'color': ICON_COLOR, + 'alpha': ICON_COLOR.a + })); + icons.push(icon); + return icon; +} + + +var panels = []; + +function addPanel(properties) { + properties.background = properties.background || {}; + properties.background.backgroundColor = properties.background.backgroundColor || + PANEL_BACKGROUND_COLOR; + properties.background.backgroundAlpha = properties.background.backgroundAlpha || + PANEL_BACKGROUND_COLOR.a; + properties.padding = properties.padding || { + x: PANEL_PADDING, + y: PANEL_PADDING + }; + properties.border = properties.border || { + x: PANEL_BORDER, + y: PANEL_BORDER + }; + + var panel = new UI.WidgetStack(properties); + panels.push(panel); + return panel; +} + +function makeDraggable(panel, target) { + if (!target) { + target = panel; + } + var dragStart = null; + var initialPos = null; + + panel.addAction('onDragBegin', function(event) { + dragStart = { + x: event.x, + y: event.y + }; + initialPos = { + x: target.position.x, + y: target.position.y + }; + }); + panel.addAction('onDragUpdate', function(event) { + target.setPosition( + initialPos.x + event.x - dragStart.x, + initialPos.y + event.y - dragStart.y + ); + UI.updateLayout(); + }); + panel.addAction('onDragEnd', function() { + dragStart = dragEnd = null; + }); +} + +function setText(text) { + return function() { + demoLabel.setText(text); + UI.updateLayout(); + }; +} + +function join(obj) { + var s = "{"; + var sep = "\n"; + for (var k in obj) { + s += sep + k + ": " + ("" + obj[k]).replace("\n", "\n"); + sep = ",\n"; + } + if (s.length > 1) + return s + " }"; + return s + "}"; +} + +setText = undefined; + +var tooltipWidget = new UI.Label({ + text: "", + width: 500, + height: 20, + visible: false +}); + +function addTooltip(widget, text) { + widget.addAction('onMouseOver', function(event, widget) { + tooltipWidget.setVisible(true); + tooltipWidget.setPosition(widget.position.x + widget.getWidth() + 20, widget.position.y); + tooltipWidget.setText(text); + UI.updateLayout(); + }); + widget.addAction('onMouseExit', function() { + tooltipWidget.setVisible(false); + UI.updateLayout(); + }); +} + +var mainPanel = addPanel({ + dir: '+y' +}); +makeDraggable(mainPanel); +mainPanel.setPosition(1200, 250); +mainPanel.setVisible(true); + +var systemViewButton = addImage(mainPanel, 'solarsystems'); +var zoomButton = addImage(mainPanel, 'magnifier'); +var satelliteButton = addImage(mainPanel, 'satellite'); +var settingsButton = addImage(mainPanel, 'settings'); +var stopButton = addImage(mainPanel, 'close'); + +addTooltip(systemViewButton, "system view"); +addTooltip(zoomButton, "zoom"); +addTooltip(satelliteButton, "satelite view"); +addTooltip(settingsButton, "settings"); +addTooltip(stopButton, "exit"); + +var systemViewPanel = addPanel({ + dir: '+x', + visible: false +}); +var restartButton = addImage(systemViewPanel, 'reverse'); +var pauseButton = addImage(systemViewPanel, 'playpause'); +var rideButton = addImage(systemViewPanel, 'forward'); + +pauseButton.addAction('onClick', function() { + if (!paused) { + pause(); + } else { + resume(); + } +}); + +// rideButton.addAction('onClick', function() { +// Script.include('orbit-ride.js'); +// new OrbitRider(); +// }); + +restartButton.addAction('onClick', function() { + Script.stop(); + var thisScript = Script.resolvePath("solarsystem.js"); + Script.load(thisScript); +}); + +var zoomPanel = addPanel({ + dir: '+x', + visible: false +}); +var zoomButtons = []; +for (var i = 0; i < planets.length; ++i) { + var label = zoomPanel.add(new UI.Label({ + text: planets[i].name, + width: 80, + height: 20, + backgroundAlpha: 1.0 + })); + zoomButtons.push(label); + UI.updateLayout(); +} +UI.updateLayout(); + +zoomButtons.forEach(function(button, i) { + var planet = planets[i]; + button.addAction('onClick', function() { + planet.zoom(); + }); +}); + + +var settingsPanel = addPanel({ + dir: '+y', + visible: false +}); + +function addCheckbox(parent, label, labelWidth, enabled, onValueChanged) { + var layout = parent.add(new UI.WidgetStack({ + dir: '+x', + visible: true, + backgroundAlpha: 0.0 + })); + var label = layout.add(new UI.Label({ + text: label, + width: labelWidth, + height: 20, + backgroundAlpha: 0.0 + })); + + var defaultColor = UI.rgb(10, 10, 10); + + var checkbox = layout.add(new UI.Checkbox({ + width: 20, + height: 20, + padding: { + x: 3, + y: 3 + }, + backgroundColor: defaultColor, + backgroundAlpha: 0.9, + checked: enabled, + onValueChanged: onValueChanged + })); + + checkbox.label = label; + checkbox.layout = layout; + checkbox.setValue = function(value) { + checkbox.setChecked(value); + } + return checkbox; +} + +function addSlider(parent, label, labelWidth, defaultValue, min, max, valueChanged) { + var layout = parent.add(new UI.WidgetStack({ + dir: '+x', + visible: true + })); + var label = layout.add(new UI.Label({ + text: label, + width: labelWidth, + height: 27 + })); + var display = layout.add(new UI.Label({ + text: " ", + width: 50, + height: 27 + })); + var slider = layout.add(new UI.Slider({ + value: defaultValue, + maxValue: max, + minValue: min, + width: 300, + height: 20, + backgroundColor: UI.rgb(10, 10, 10), + backgroundAlpha: 1.0, + slider: { // slider knob + width: 30, + height: 18, + backgroundColor: UI.rgb(120, 120, 120), + backgroundAlpha: 1.0 + } + })); + slider.addAction('onDoubleClick', function() { + slider.setValue(defaultValue); + UI.updateLayout(); + }); + display.setText("" + (+slider.getValue().toFixed(2))); + slider.onValueChanged = function(value) { + valueChanged(value); + display.setText("" + (+value.toFixed(2))); + UI.updateLayout(); + } + slider.label = label; + slider.layout = layout; + return slider; +} + +settingsPanel.showTrailsButton = addCheckbox(settingsPanel, "show trails", 120, trailsEnabled, function(trailsEnabled) { + if (trailsEnabled) { + for (var i = 0; i < planets.length; ++i) { + planets[i].resetTrails(); + } + //if trails are off and we've already created trails, remove existing trails + } else { + for (var i = 0; i < planets.length; ++i) { + planets[i].clearTrails(); + } + } +}); +var g_multiplier = 1.0; +settingsPanel.gravitySlider = addSlider(settingsPanel, "gravity scale ", 200, g_multiplier, 0.0, 5.0, function (value) { + g_multiplier = value; + GRAVITY = REFERENCE_GRAVITY * g_multiplier; +}); + +var period_multiplier = 1.0; +var last_alpha = period_multiplier; + +settingsPanel.periodSlider = addSlider(settingsPanel, "orbital period scale ", 200, period_multiplier, 0.0, 3.0, function (value) { + period_multiplier = value; + changePeriod(period_multiplier); +}); + +function changePeriod(alpha) { + var ratio = last_alpha / alpha; + GRAVITY = Math.pow(ratio, 2.0) * GRAVITY; + for (var i = 0; i < planets.length; ++i) { + planets[i].period = ratio * planets[i].period; + planets[i].velocity = Vec3.multiply(ratio, planets[i].velocity); + planets[i].resetTrails(); + } + last_alpha = alpha; +} + +var satelliteGame; +satelliteButton.addAction('onClick', function() { + if (satelliteGame && satelliteGame.isActive) { + MyAvatar.position = startingPosition; + satelliteGame.quitGame(); + resume(); + } else { + pause(); + satelliteGame = new SatelliteCreator(); + satelliteGame.init(); + } +}); + + +var subpanels = [systemViewPanel, zoomPanel, settingsPanel]; + +function hideSubpanelsExcept(panel) { + subpanels.forEach(function(x) { + if (x != panel) { + x.setVisible(false); + } + }); +} + +function attachPanel(panel, button) { + button.addAction('onClick', function() { + hideSubpanelsExcept(panel); + panel.setVisible(!panel.isVisible()); + UI.updateLayout(); + }) + + UI.addAttachment(panel, button, function(target, rel) { + target.setPosition( + rel.position.x - (target.getWidth() + target.border.x + SUBPANEL_GAP), + rel.position.y - target.border.y + ); + }); +} +attachPanel(systemViewPanel, systemViewButton); +attachPanel(zoomPanel, zoomButton); +attachPanel(settingsPanel, settingsButton); + + +var addColorToggle = function(widget) { + widget.addAction('onMouseOver', function() { + widget.setColor(FOCUSED_COLOR); + }); + widget.addAction('onMouseExit', function() { + widget.setColor(ICON_COLOR); + }); +} + + +systemViewPanel.addAction('onMouseOver', function() { + hideSubpanelsExcept(systemViewPanel); + UI.updateLayout(); +}); + + +zoomButton.addAction('onClick', function() { + hideSubpanelsExcept(zoomPanel); + UI.updateLayout(); +}); +UI.updateLayout(); + +stopButton.addAction('onClick', function() { + teardown(); + Script.stop(); +}); + +// Panel drag behavior +// (click + drag on border to drag) +(function() { + var dragged = null; + this.startDrag = function(dragAction) { + dragged = dragAction; + } + this.updateDrag = function(event) { + if (dragged) { + print("Update drag"); + dragged.updateDrag(event); + } + } + this.clearDrag = function(event) { + if (dragged) + print("End drag"); + dragged = null; + } +})(); + +var buttons = icons; + +buttons.map(addColorToggle); +panels.map(function(panel) { + makeDraggable(panel, mainPanel); +}); + +// Cleanup script resources +function teardown() { + if (satelliteGame) { + satelliteGame.quitGame(); + } + UI.teardown(); +}; + +UI.debug.setVisible(false); + +var inputHandler = { + onMouseMove: function(event) { + updateDrag(event); + UI.handleMouseMove(event); + }, + onMousePress: function(event) { + UI.handleMousePress(event); + }, + onMouseRelease: function(event) { + clearDrag(event); + UI.handleMouseRelease(event); + }, + onMouseDoublePress: function(event) { + UI.handleMouseDoublePress(event); + } +}; +Controller.mousePressEvent.connect(inputHandler.onMousePress); +Controller.mouseMoveEvent.connect(inputHandler.onMouseMove); +Controller.mouseReleaseEvent.connect(inputHandler.onMouseRelease); +Controller.mouseDoublePressEvent.connect(inputHandler.onMouseDoublePress); diff --git a/examples/example/solarsystem.js b/examples/example/solarsystem.js index 196ad57f86..3e4a39ed71 100644 --- a/examples/example/solarsystem.js +++ b/examples/example/solarsystem.js @@ -18,797 +18,463 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -Script.include([ - 'games/satellite.js' -]); +CreateSimulation = function() { + Script.include("https://hifi-public.s3.amazonaws.com/eric/scripts/tween.js"); + Script.include('games/satellite.js'); -var DAMPING = 0.0; -var LIFETIME = 6000; -var BASE_URL = "https://s3.amazonaws.com/hifi-public/marketplace/hificontent/Scripts/planets/planets/"; + var DAMPING = this.DAMPING = 0.0; + var LIFETIME = this.LIFETIME = 6000; + var BASE_URL = this.BASE_URL = "https://s3.amazonaws.com/hifi-public/marketplace/hificontent/Scripts/planets/planets/"; -// Save intiial avatar and camera position -var startingPosition = { - x: 8000, - y: 8000, - z: 8000 -}; -MyAvatar.position = startingPosition; -var cameraStart = Camera.getOrientation(); + // Save intiial avatar and camera position + var startingPosition = this.startingPosition = { + x: 8000, + y: 8000, + z: 8000 + }; + MyAvatar.position = startingPosition; + var cameraStart = this.cameraStart = Camera.getOrientation(); -// Place the sun -var MAX_RANGE = 80.0; -var center = Vec3.sum(startingPosition, Vec3.multiply(MAX_RANGE, Vec3.normalize(Quat.getFront(Camera.getOrientation())))); -var SUN_SIZE = 7.0; + // Place the sun + var MAX_RANGE = this.MAX_RANGE = 80.0; + var center = this.center = Vec3.sum(startingPosition, Vec3.multiply(MAX_RANGE, Vec3.normalize(Quat.getFront(Camera.getOrientation())))); + var SUN_SIZE = this.SUN_SIZE = 7.0; -var theSun = Entities.addEntity({ - type: "Model", - modelURL: BASE_URL + "sun.fbx", - position: center, - dimensions: { - x: SUN_SIZE, - y: SUN_SIZE, - z: SUN_SIZE - }, - angularVelocity: { - x: 0.0, - y: 0.1, - z: 0.0 - }, - angularDamping: DAMPING, - damping: DAMPING, - ignoreCollisions: false, - lifetime: LIFETIME, - collisionsWillMove: false -}); - - - -// Reference values for physical constants -var referenceRadius = 15.0; -var referenceDiameter = 0.6; -var referencePeriod = 3.0; -var LARGE_BODY_MASS = 250.0; -var SMALL_BODY_MASS = LARGE_BODY_MASS * 0.000000333; -var GRAVITY = (Math.pow(referenceRadius, 3.0) / Math.pow((referencePeriod / (2.0 * Math.PI)), 2.0)) / LARGE_BODY_MASS; -var REFERENCE_GRAVITY = GRAVITY; - -var planets = []; -var planetCount = 0; - -var trailsEnabled = true; -var MAX_POINTS_PER_LINE = 60; -var LINE_DIM = 200; -var LINE_WIDTH = 20; - -var VELOCITY_OFFSET_Y = -0.3; -var VELOCITY_OFFSET_Z = 0.9; - -var index = 0; - -var Planet = function(name, trailColor, radiusScale, sizeScale) { - - this.name = name; - this.trailColor = trailColor; - - this.trail = []; - this.lineStack = []; - - this.radius = radiusScale * referenceRadius; - this.position = Vec3.sum(center, { - x: this.radius, - y: 0.0, - z: 0.0 - }); - this.period = (2.0 * Math.PI) * Math.sqrt(Math.pow(this.radius, 3.0) / (GRAVITY * LARGE_BODY_MASS)); - this.gravity = GRAVITY; - this.initialVelocity = Math.sqrt((GRAVITY * LARGE_BODY_MASS) / this.radius); - this.velocity = Vec3.multiply(this.initialVelocity, Vec3.normalize({ - x: 0, - y: VELOCITY_OFFSET_Y, - z: VELOCITY_OFFSET_Z - })); - this.dimensions = sizeScale * referenceDiameter; - this.sizeScale = sizeScale; - - this.planet = Entities.addEntity({ + var theSun = this.theSun = Entities.addEntity({ type: "Model", - modelURL: BASE_URL + name + ".fbx", - position: this.position, + modelURL: BASE_URL + "sun.fbx", + position: center, dimensions: { - x: this.dimensions, - y: this.dimensions, - z: this.dimensions + x: SUN_SIZE, + y: SUN_SIZE, + z: SUN_SIZE + }, + angularVelocity: { + x: 0.0, + y: 0.1, + z: 0.0 }, - velocity: this.velocity, angularDamping: DAMPING, damping: DAMPING, ignoreCollisions: false, lifetime: LIFETIME, - collisionsWillMove: true, + collisionsWillMove: false }); - this.computeAcceleration = function() { - var acc = -(this.gravity * LARGE_BODY_MASS) * Math.pow(this.radius, (-2.0)); - return acc; - }; - this.update = function(timeStep) { - var between = Vec3.subtract(this.position, center); - var speed = this.computeAcceleration(this.radius) * timeStep; - var vel = Vec3.multiply(speed, Vec3.normalize(between)); - // Update velocity and position - this.velocity = Vec3.sum(this.velocity, vel); - this.position = Vec3.sum(this.position, Vec3.multiply(timeStep, this.velocity)); - Entities.editEntity(this.planet, { - velocity: this.velocity, - position: this.position - }); + // Reference values for physical constants + var referenceRadius = this.referenceRadius = 15.0; + var referenceDiameter = this.referenceDiameter = 0.6; + var referencePeriod = this.referencePeriod = 3.0; + var LARGE_BODY_MASS = this.LARGE_BODY_MASS = 250.0; + var SMALL_BODY_MASS = this.SMALL_BODY_MASS = LARGE_BODY_MASS * 0.000000333; + var GRAVITY = this.GRAVITY = (Math.pow(referenceRadius, 3.0) / Math.pow((referencePeriod / (2.0 * Math.PI)), 2.0)) / LARGE_BODY_MASS; + var REFERENCE_GRAVITY = this.REFERENCE_GRAVITY = GRAVITY; + + var planets = this.planets = []; + var trailsEnabled = this.trailsEnabled = true; + var MAX_POINTS_PER_LINE = this.MAX_POINTS_PER_LINE = 60; + var LINE_DIM = this.LINE_DIM = 200; + var LINE_WIDTH = this.LINE_WIDTH = 20; + + var VELOCITY_OFFSET_Y = this.VELOCITY_OFFSET_Y = -0.3; + var VELOCITY_OFFSET_Z = this.VELOCITY_OFFSET_Z = 0.9; + + var index = this.index = 0; + + this.computeLocalPoint = function(linePos, worldPoint) { + var localPoint = Vec3.subtract(worldPoint, linePos); + return localPoint; + } + + // Create a new line + this.newLine = function(lineStack, point, period, color) { + if (elapsed < period) { + var line = Entities.addEntity({ + position: point, + type: "Line", + color: color, + dimensions: { + x: LINE_DIM, + y: LINE_DIM, + z: LINE_DIM + }, + lifetime: LIFETIME, + lineWidth: LINE_WIDTH + }); + lineStack.push(line); + } else { + // Begin overwriting first lines after one full revolution (one period) + var firstLine = lineStack.shift(); + print("editing first line"); + Entities.editEntity(firstLine, { + position: point, + linePoints: [{ + x: 0.0, + y: 0.0, + z: 0.0 + }] + }); + lineStack.push(firstLine); - if (trailsEnabled) { - this.updateTrail(); } - }; + var points = []; + points.push(computeLocalPoint(point, point)); + return points; + } - this.resetTrails = function() { - elapsed = 0.0; + this.pause = function() { + for (var i = 0; i < planets.length; ++i) { + Entities.editEntity(planets[i].planet, { + velocity: { + x: 0.0, + y: 0.0, + z: 0.0 + } + }); + planets[i].label = new PlanetLabel(planets[i].name, i); + planets[i].label.show(); + } + paused = true; + } + + this.resume = function() { + for (var i = 0; i < planets.length; ++i) { + planets[i].label.hide(); + } + paused = false; + } + + var Planet = function(name, trailColor, radiusScale, sizeScale) { + + this.name = name; + this.trailColor = trailColor; this.trail = []; this.lineStack = []; - //add the first line to both the line entity stack and the trail - this.trail.push(newLine(this.lineStack, this.position, this.period, this.trailColor)); - }; + this.radius = radiusScale * referenceRadius; + this.position = Vec3.sum(center, { + x: this.radius, + y: 0.0, + z: 0.0 + }); + this.period = (2.0 * Math.PI) * Math.sqrt(Math.pow(this.radius, 3.0) / (GRAVITY * LARGE_BODY_MASS)); - this.updateTrail = function() { - var point = this.position; - var linePos = Entities.getEntityProperties(this.lineStack[this.lineStack.length - 1]).position; + this.initialVelocity = Math.sqrt((GRAVITY * LARGE_BODY_MASS) / this.radius); - this.trail.push(computeLocalPoint(linePos, point)); + this.velocity = Vec3.multiply(this.initialVelocity, Vec3.normalize({ + x: 0, + y: VELOCITY_OFFSET_Y, + z: VELOCITY_OFFSET_Z + })); + this.dimensions = sizeScale * referenceDiameter; + this.sizeScale = sizeScale; - Entities.editEntity(this.lineStack[this.lineStack.length - 1], { - linePoints: this.trail + this.planet = Entities.addEntity({ + type: "Model", + modelURL: BASE_URL + name + ".fbx", + position: this.position, + dimensions: { + x: this.dimensions, + y: this.dimensions, + z: this.dimensions + }, + velocity: this.velocity, + angularDamping: DAMPING, + damping: DAMPING, + ignoreCollisions: false, + lifetime: LIFETIME, + collisionsWillMove: false, }); - if (this.trail.length == MAX_POINTS_PER_LINE) { - this.trail = newLine(this.lineStack, point, this.period, this.trailColor); + this.computeAcceleration = function() { + var acc = -(GRAVITY * LARGE_BODY_MASS) * Math.pow(this.radius, (-2.0)); + return acc; + }; + + this.update = function(deltaTime) { + var between = Vec3.subtract(this.position, center); + var speed = this.computeAcceleration(this.radius) * deltaTime; + var vel = Vec3.multiply(speed, Vec3.normalize(between)); + + // Update velocity and position + this.velocity = Vec3.sum(this.velocity, vel); + this.position = Vec3.sum(this.position, Vec3.multiply(deltaTime, this.velocity)); + Entities.editEntity(this.planet, { + velocity: this.velocity, + position: this.position + }); + + if (trailsEnabled) { + this.updateTrail(); + } + }; + + this.clearTrails = function() { + + for (var j = 0; j < this.lineStack.length; ++j) { + Entities.editEntity(this.lineStack[j], { + visible: false + }); + } + this.lineStack = []; + } - }; + this.resetTrails = function() { + elapsed = 0.0; - this.zoom = function() { - Script.include('file:///Users/bridget/Desktop/tween.js'); - init(this); - // var viewingRange = sizeScale * 2.0; - // var direction = Vec3.subtract(this.position, MyAvatar.position); - // var dist = Vec3.length(direction); - // var timer = 0; - // while (dist > viewingRange && timer < 8000000) { - // timer++; - // if (timer % 10000 == 0) { - // MyAvatar.position = Vec3.sum(MyAvatar.position, Vec3.normalize(direction)); - // direction = Vec3.subtract(this.position, MyAvatar.position); - // dist = Vec3.length(direction); - // } - // } - }; + this.trail = []; + this.lineStack = []; + //add the first line to both the line entity stack and the trail + this.trail.push(newLine(this.lineStack, this.position, this.period, this.trailColor)); + }; - this.adjustPeriod = function(alpha) { - // Update global G constant, period, poke velocity to new value - var ratio = this.last_alpha / alpha; - this.gravity = Math.pow(ratio, 2.0) * GRAVITY; - this.period = ratio * this.period; - this.velocity = Vec3.multiply(ratio, this.velocity); + this.updateTrail = function() { + var point = this.position; + var linePos = Entities.getEntityProperties(this.lineStack[this.lineStack.length - 1]).position; - this.last_alpha = alpha; - }; + this.trail.push(computeLocalPoint(linePos, point)); + Entities.editEntity(this.lineStack[this.lineStack.length - 1], { + linePoints: this.trail + }); - index++; - this.resetTrails(); + if (this.trail.length == MAX_POINTS_PER_LINE) { + this.trail = newLine(this.lineStack, point, this.period, this.trailColor); + } + }; -} + this.zoom = function() { + pause(); -// rideOrbit = function() { - -// Script.include('file:///Users/bridget/Desktop/tween.js'); -// init(); - -// } - - -var MERCURY_LINE_COLOR = { - red: 255, - green: 255, - blue: 255 -}; -var VENUS_LINE_COLOR = { - red: 255, - green: 160, - blue: 110 -}; -var EARTH_LINE_COLOR = { - red: 10, - green: 150, - blue: 160 -}; -var MARS_LINE_COLOR = { - red: 180, - green: 70, - blue: 10 -}; -var JUPITER_LINE_COLOR = { - red: 250, - green: 140, - blue: 0 -}; -var SATURN_LINE_COLOR = { - red: 235, - green: 215, - blue: 0 -}; -var URANUS_LINE_COLOR = { - red: 135, - green: 205, - blue: 240 -}; -var NEPTUNE_LINE_COLOR = { - red: 30, - green: 140, - blue: 255 -}; -var PLUTO_LINE_COLOR = { - red: 255, - green: 255, - blue: 255 -}; - -planets.push(new Planet("mercury", MERCURY_LINE_COLOR, 0.387, 0.383)); -planets.push(new Planet("venus", VENUS_LINE_COLOR, 0.723, 0.949)); -planets.push(new Planet("earth", EARTH_LINE_COLOR, 1.0, 1.0)); -planets.push(new Planet("mars", MARS_LINE_COLOR, 1.52, 0.532)); -planets.push(new Planet("jupiter", JUPITER_LINE_COLOR, 5.20, 11.21)); -planets.push(new Planet("saturn", SATURN_LINE_COLOR, 9.58, 9.45)); -planets.push(new Planet("uranus", URANUS_LINE_COLOR, 19.20, 4.01)); -planets.push(new Planet("neptune", NEPTUNE_LINE_COLOR, 30.05, 3.88)); -planets.push(new Planet("pluto", PLUTO_LINE_COLOR, 39.48, 0.186)); - -var LABEL_X = 8.0; -var LABEL_Y = 3.0; -var LABEL_Z = 1.0; -var LABEL_DIST = 8.0; -var TEXT_HEIGHT = 1.0; - -var PlanetLabel = function(name, index) { - var text = name + " Speed: " + Vec3.length(planets[index].velocity).toFixed(2); - this.labelPos = Vec3.sum(planets[index].position, { - x: 0.0, - y: LABEL_DIST, - z: LABEL_DIST - }); - this.linePos = planets[index].position; - - this.line = Entities.addEntity({ - type: "Line", - position: this.linePos, - dimensions: { - x: 20, - y: 20, - z: 20 - }, - lineWidth: LINE_WIDTH, - color: { - red: 255, - green: 255, - blue: 255 - }, - linePoints: [{ + this.tweening = true; + var tweenTime = 800; + var startingPosition = MyAvatar.position; + var endingPosition = Vec3.sum({ x: 0, y: 0, - z: 0 - }, - computeLocalPoint(this.linePos, this.labelPos) - ], - visible: false - }); + z: this.sizeScale + }, this.position); + var currentProps = { + x: startingPosition.x, + y: startingPosition.y, + z: startingPosition.z, + }; + var endProps = { + x: endingPosition.x, + y: endingPosition.y, + z: endingPosition.z, + }; - this.label = Entities.addEntity({ - type: "Text", - text: text, - lineHeight: TEXT_HEIGHT, - dimensions: { - x: LABEL_X, - y: LABEL_Y, - z: LABEL_Z - }, - position: this.labelPos, - backgroundColor: { - red: 10, - green: 10, - blue: 10 - }, - faceCamera: true, - visible: false - }); + var moveTween = new TWEEN.Tween(currentProps). + to(endProps, tweenTime). + easing(TWEEN.Easing.Cubic.InOut). + onUpdate(function() { + print(JSON.stringify(currentProps)); + MyAvatar.position = { + x: currentProps.x, + y: currentProps.y, + z: currentProps.z + }; + }).start(); - this.show = function() { - this.showing = true; - Entities.editEntity(this.line, { - visible: true + moveTween.onComplete(function() { + this.tweening = false; + }); + + } + + index++; + this.resetTrails(); + + } + + + var MERCURY_LINE_COLOR = { + red: 255, + green: 255, + blue: 255 + }; + var VENUS_LINE_COLOR = { + red: 255, + green: 160, + blue: 110 + }; + var EARTH_LINE_COLOR = { + red: 10, + green: 150, + blue: 160 + }; + var MARS_LINE_COLOR = { + red: 180, + green: 70, + blue: 10 + }; + var JUPITER_LINE_COLOR = { + red: 250, + green: 140, + blue: 0 + }; + var SATURN_LINE_COLOR = { + red: 235, + green: 215, + blue: 0 + }; + var URANUS_LINE_COLOR = { + red: 135, + green: 205, + blue: 240 + }; + var NEPTUNE_LINE_COLOR = { + red: 30, + green: 140, + blue: 255 + }; + var PLUTO_LINE_COLOR = { + red: 255, + green: 255, + blue: 255 + }; + + planets.push(new Planet("mercury", MERCURY_LINE_COLOR, 0.387, 0.383)); + planets.push(new Planet("venus", VENUS_LINE_COLOR, 0.723, 0.949)); + planets.push(new Planet("earth", EARTH_LINE_COLOR, 1.0, 1.0)); + planets.push(new Planet("mars", MARS_LINE_COLOR, 1.52, 0.532)); + planets.push(new Planet("jupiter", JUPITER_LINE_COLOR, 5.20, 11.21)); + planets.push(new Planet("saturn", SATURN_LINE_COLOR, 9.58, 9.45)); + planets.push(new Planet("uranus", URANUS_LINE_COLOR, 19.20, 4.01)); + planets.push(new Planet("neptune", NEPTUNE_LINE_COLOR, 30.05, 3.88)); + planets.push(new Planet("pluto", PLUTO_LINE_COLOR, 39.48, 0.186)); + + var LABEL_X = 8.0; + var LABEL_Y = 3.0; + var LABEL_Z = 1.0; + var LABEL_DIST = 8.0; + var TEXT_HEIGHT = 1.0; + + var PlanetLabel = function(name, index) { + var text = name + " Speed: " + Vec3.length(planets[index].velocity).toFixed(2); + this.labelPos = Vec3.sum(planets[index].position, { + x: 0.0, + y: LABEL_DIST, + z: LABEL_DIST }); - Entities.editEntity(this.label, { - visible: true - }); - } + this.linePos = planets[index].position; - this.hide = function() { - this.showing = false; - Entities.editEntity(this.line, { - visible: false - }); - Entities.editEntity(this.label, { - visible: false - }); - } -} - - -var time = 0.0; -var elapsed; -var TIME_STEP = 60.0; -var dt = 1.0 / TIME_STEP; - -var planetLines = []; -var trails = []; -var paused = false; - -function update(deltaTime) { - if (paused) { - return; - } - //deltaTime = dt; - time++; - if (time % TIME_STEP == 0) { - elapsed++; - } - - for (var i = 0; i < planets.length; ++i) { - planets[i].update(deltaTime); - } -} - -function pause() { - for (var i = 0; i < planets.length; ++i) { - Entities.editEntity(planets[i].planet, { - velocity: { - x: 0.0, - y: 0.0, - z: 0.0 - } - }); - planets[i].label = new PlanetLabel(planets[i].name, i); - planets[i].label.show(); - } - paused = true; -} - -function resume() { - for (var i = 0; i < planets.length; ++i) { - planets[i].label.hide(); - } - paused = false; -} - -function computeLocalPoint(linePos, worldPoint) { - var localPoint = Vec3.subtract(worldPoint, linePos); - return localPoint; -} - -// Create a new line -function newLine(lineStack, point, period, color) { - if (elapsed < period) { - var line = Entities.addEntity({ - position: point, + this.line = Entities.addEntity({ type: "Line", - color: color, + position: this.linePos, dimensions: { - x: LINE_DIM, - y: LINE_DIM, - z: LINE_DIM + x: 20, + y: 20, + z: 20 }, - lifetime: LIFETIME, - lineWidth: LINE_WIDTH - }); - lineStack.push(line); - } else { - // Begin overwriting first lines after one full revolution (one period) - var firstLine = lineStack.shift(); - Entities.editEntity(firstLine, { - position: point, - linePoints: [{ - x: 0.0, - y: 0.0, - z: 0.0 - }] - }); - lineStack.push(firstLine); - - } - var points = []; - points.push(computeLocalPoint(point, point)); - return points; -} - - - -var spacing = 8; -var textWidth = 70; -var buttonWidth = 30; -var buttonHeight = 30; - -var ICONS_URL = 'https://s3.amazonaws.com/hifi-public/marketplace/hificontent/Scripts/planets/images/'; - -var UIPanel = function(x, y, orientation) { - this.visible = false; - this.buttons = []; - this.x = x; - this.y = y; - this.nextX = x + spacing; - this.nextY = y + spacing; - this.width = spacing * 2.0; - this.height = spacing * 2.0; - - this.background = Overlays.addOverlay("text", { - backgroundColor: { - red: 240, - green: 240, - blue: 255 - }, - x: this.x, - y: this.y, - width: this.width, - height: this.height, - alpha: 1.0, - backgroundAlpha: 0.7, - visible: false - }); - - this.addIcon = function(iconID) { - var icon = Overlays.addOverlay("image", { + lineWidth: LINE_WIDTH, color: { red: 255, green: 255, blue: 255 }, - imageURL: ICONS_URL + iconID + '.svg', - x: this.nextX, - y: this.nextY, - width: buttonWidth, - height: buttonHeight, - alpha: 1.0, + linePoints: [{ + x: 0, + y: 0, + z: 0 + }, + computeLocalPoint(this.linePos, this.labelPos) + ], visible: false }); - - if (orientation === 'horizontal') { - this.nextX += buttonWidth + spacing; - this.width += buttonWidth; - - } else if (orientation === 'vertical') { - this.nextY += buttonHeight + spacing; - this.height += buttonHeight; - } - - Overlays.editOverlay(this.background, { - width: buttonWidth + this.width, - height: buttonHeight + this.height - }); - - this.buttons.push(icon); - return icon; - }; - - this.addText = function(text) { - var icon = Overlays.addOverlay("text", { - color: { - red: 240, - green: 240, - blue: 255 - }, + this.label = Entities.addEntity({ + type: "Text", text: text, - x: this.nextX, - y: this.nextY, - width: textWidth, - height: buttonHeight, + lineHeight: TEXT_HEIGHT, + dimensions: { + x: LABEL_X, + y: LABEL_Y, + z: LABEL_Z + }, + position: this.labelPos, + backgroundColor: { + red: 10, + green: 10, + blue: 10 + }, + faceCamera: true, visible: false }); - if (orientation === 'horizontal') { - this.nextX += textWidth + spacing; - this.width += textWidth; - - } else if (orientation === 'vertical') { - this.nextY += buttonHeight + spacing; - this.height += buttonHeight; - } - - Overlays.editOverlay(this.background, { - width: textWidth + this.width, - height: buttonHeight + this.height - }); - - this.buttons.push(icon); - return icon; - }; - - - this.show = function() { - Overlays.editOverlay(this.background, { - visible: true - }); - for (var i in this.buttons) { - Overlays.editOverlay(this.buttons[i], { + this.show = function() { + this.showing = true; + Entities.editEntity(this.line, { + visible: true + }); + Entities.editEntity(this.label, { visible: true }); } - this.visible = true; - }; - this.hide = function() { - Overlays.editOverlay(this.background, { - visible: false - }); - for (var i in this.buttons) { - Overlays.editOverlay(this.buttons[i], { + this.hide = function() { + this.showing = false; + Entities.editEntity(this.line, { + visible: false + }); + Entities.editEntity(this.label, { visible: false }); } - this.visible = false; - }; + } - this.remove = function() { - Overlays.deleteOverlay(this.background); - for (var i in this.buttons) { - Overlays.deleteOverlay(this.buttons[i]); - } - }; + var planetLines = []; + var trails = this.trails = []; + var paused = this.paused = false; -} - -var panelX = 1250; -var panelY = 500; -var panelWidth = 50; -var panelHeight = 210; - -var mainPanel = new UIPanel(panelX, panelY, 'vertical'); - -var systemViewButton = mainPanel.addIcon('solarsystems'); -var systemViewPanel = new UIPanel(panelX - 140, panelY, 'horizontal'); -var reverseButton = systemViewPanel.addIcon('reverse'); -var pauseButton = systemViewPanel.addIcon('playpause'); -var forwardButton = systemViewPanel.addIcon('forward'); - -var zoomButton = mainPanel.addIcon('magnifier'); -var zoomPanel = new UIPanel(panelX - 900, panelY + (buttonHeight + spacing) * 4.0, 'horizontal'); -for (var i = 0; i < planets.length; ++i) { - zoomPanel.addText(planets[i].name); -} - -var satelliteButton = mainPanel.addIcon('satellite'); -var settingsButton = mainPanel.addIcon('settings'); -var stopButton = mainPanel.addIcon('close'); - -mainPanel.show(); - -Script.include('../utilities/tools/cookies.js'); - - -var satelliteView; -var satelliteGame; - var settings = new Panel(panelX - 610, panelY - 80); - - var g_multiplier = 1.0; - settings.newSlider("Gravitational Force ", 0.1, 5.0, - function(value) { - g_multiplier = value; - GRAVITY = REFERENCE_GRAVITY * g_multiplier; - }, - - function() { - return g_multiplier; - }, - function(value) { - return value.toFixed(1) + "x"; - }); - - - var subPanel = settings.newSubPanel("Orbital Periods"); - - for (var i = 0; i < planets.length; ++i) { - planets[i].period_multiplier = 1.0; - planets[i].last_alpha = planets[i].period_multiplier; - - subPanel.newSlider(planets[i].name, 0.1, 3.0, - function(value) { - planets[i].period_multiplier = value; - planets[i].adjustPeriod(value); - }, - function() { - return planets[i].period_multiplier; - }, - function(value) { - return (value).toFixed(2) + "x"; - }); - } - settings.newCheckbox("Leave Trails: ", - function(value) { - trailsEnabled = value; - if (trailsEnabled) { - for (var i = 0; i < planets.length; ++i) { - planets[i].resetTrails(); - } - //if trails are off and we've already created trails, remove existing trails - } else { - for (var i = 0; i < planets.length; ++i) { - for (var j = 0; j < planets[i].lineStack.length; ++j) { - Entities.editEntity(planets[i].lineStack[j], { - visible: false - }); - } - planets[i].lineStack = []; - } - } - }, - function() { - return trailsEnabled; - }, - function(value) { - return value; - }); - settings.hide(); - - -function mousePressEvent(event) { - - var clicked = Overlays.getOverlayAtPoint({ - x: event.x, - y: event.y - }); - - if (clicked == systemViewButton) { - MyAvatar.position = startingPosition; - Camera.setOrientation(cameraStart); + this.update = function(deltaTime) { if (paused) { - resume(); + return; } - - if (!systemViewPanel.visible) { - systemViewPanel.show(); - } else { - systemViewPanel.hide(); - } - } - var zoomed = false; - if (clicked == zoomButton && !satelliteView) { - if (!zoomPanel.visible) { - zoomPanel.show(); - pause(); - } else { - zoomPanel.hide(); - if (zoomed) { - - MyAvatar.position = startingPosition; - Camera.setOrientation(cameraStart); - resume(); - } - - } - } - - for (var i = 0; i < planets.length; ++i) { - if (zoomPanel.visible && clicked == zoomPanel.buttons[i]) { - planets[i].zoom(); - zoomed = true; - break; - } - } - - if (systemViewPanel.visible && clicked == pauseButton) { - if (!paused) { - pause(); - } else { - resume(); - } - } - - - - if (clicked == satelliteButton) { - if (satelliteView) { - satelliteGame.endGame(); - MyAvatar.position = startingPosition; - satelliteView = false; - resume(); - } else { - pause(); - var confirmed = Window.confirm("Start satellite game?"); - if (!confirmed) { - resume(); + for (var i = 0; i < planets.length; ++i) { + if (planets[i].tweening) { + TWEEN.update(); continue; } - satelliteView = true; - MyAvatar.position = { - x: 200, - y: 200, - z: 200 - }; - Camera.setPosition({ - x: 200, - y: 200, - z: 200 - }); - satelliteGame = new SatelliteGame(); + planets[i].update(deltaTime); } } - if (clicked == settingsButton) { - if (!settings.visible) { - settings.show(); - } else { - settings.hide(); - } - } + Script.include('planets-ui.js'); - if (clicked == stopButton) { - Script.stop(); - } + // Clean up models, UI panels, lines, and button overlays + this.scriptEnding = function() { + Entities.deleteEntity(theSun); + for (var i = 0; i < planets.length; ++i) { + Entities.deleteEntity(planets[i].planet); + } + var e = Entities.findEntities(MyAvatar.position, 10000); + for (i = 0; i < e.length; i++) { + var props = Entities.getEntityProperties(e[i]); + if (props.type === "Line" || props.type === "Text") { + Entities.deleteEntity(e[i]); + } + } + }; + Script.update.connect(update); + } -// Clean up models, UI panels, lines, and button overlays -function scriptEnding() { - mainPanel.remove(); - systemViewPanel.remove(); - zoomPanel.remove(); - - settings.destroy(); - - Entities.deleteEntity(theSun); - for (var i = 0; i < planets.length; ++i) { - Entities.deleteEntity(planets[i].planet); - } +Script.scriptEnding.connect(function() { + scriptEnding(); + teardown(); +}); + +CreateSimulation(); - var e = Entities.findEntities(MyAvatar.position, 16000); - for (i = 0; i < e.length; i++) { - var props = Entities.getEntityProperties(e[i]); - if (props.type === "Line" || props.type === "Text") { - Entities.deleteEntity(e[i]); - } - } -}; - -Controller.mousePressEvent.connect(mousePressEvent); - - Controller.mouseMoveEvent.connect(function panelMouseMoveEvent(event) { - return settings.mouseMoveEvent(event); - }); - Controller.mousePressEvent.connect(function panelMousePressEvent(event) { - return settings.mousePressEvent(event); - }); - Controller.mouseDoublePressEvent.connect(function panelMouseDoublePressEvent(event) { - return settings.mouseDoublePressEvent(event); - }); - Controller.mouseReleaseEvent.connect(function(event) { - return settings.mouseReleaseEvent(event); - }); - Controller.keyPressEvent.connect(function(event) { - return settings.keyPressEvent(event); - }); - -Script.scriptEnding.connect(scriptEnding); -Script.update.connect(update); \ No newline at end of file diff --git a/examples/libraries/uiwidgets.js b/examples/libraries/uiwidgets.js index fa898ea7bc..70eda8e5f2 100644 --- a/examples/libraries/uiwidgets.js +++ b/examples/libraries/uiwidgets.js @@ -501,7 +501,7 @@ Box.prototype.destroy = function () { } } Box.prototype.hasOverlay = function (overlayId) { - return this.overlay && this.overlay.getId() === overlayId; + return /*this.overlay &&*/ this.overlay.getId() === overlayId; } Box.prototype.getOverlay = function () { return this.overlay; @@ -615,7 +615,7 @@ Slider.prototype.toString = function () { } Slider.prototype.applyLayout = function () { if (!this.slider) { - ui.complain("Slider.applyLayout on " + this + " failed"); + // ui.complain("Slider.applyLayout on " + this + " failed"); return; } var val = (this.value - this.minValue) / (this.maxValue - this.minValue); @@ -654,6 +654,7 @@ var Checkbox = UI.Checkbox = function (properties) { this.position.x + (this.width - this.checkMark.width) * 0.5, this.position.y + (this.height - this.checkMark.height) * 0.5 ); + this.checkMark.parent = this; this.onValueChanged = properties.onValueChanged || function () {}; @@ -919,9 +920,16 @@ var getFocusedWidget = function (event) { var dispatchEvent = function (action, event, widget) { function dispatchActions (actions) { + var dispatchedActions = false; + ui.logEvent("dispatching to [" + actions.join(", ") + "]"); actions.forEach(function(action) { action(event, widget); + ui.logEvent("dispatched to " + action); + dispatchedActions = true; }); + if (!dispatchedActions) { + // ui.logEvent("No actions to dispatch"); + } } if (widget.actions[action]) { @@ -963,7 +971,7 @@ UI.handleMouseMove = function (event, canStartDrag) { } UI.handleMousePress = function (event) { - print("Mouse clicked"); + // print("Mouse clicked"); UI.handleMouseMove(event); ui.clickedWidget = ui.focusedWidget; if (ui.clickedWidget) { @@ -971,8 +979,18 @@ UI.handleMousePress = function (event) { } } +UI.handleMouseDoublePress = function (event) { + // print("DOUBLE CLICK!"); + var focused = getFocusedWidget(event); + UI.handleMouseMove(event); + if (focused) { + // print("dispatched onDoubleClick"); + dispatchEvent('onDoubleClick', event, focused); + } +} + UI.handleMouseRelease = function (event) { - print("Mouse released"); + // print("Mouse released"); if (ui.draggedWidget) { dispatchEvent('onDragEnd', event, ui.draggedWidget); From 2dbba322fd116451118d8f4f310b3f145d55134b Mon Sep 17 00:00:00 2001 From: bwent Date: Thu, 13 Aug 2015 18:13:08 -0700 Subject: [PATCH 06/12] bug fixes, ride through system --- examples/example/games/satellite.js | 12 ++--- examples/example/planets-ui.js | 44 ++++++++++----- examples/example/solarsystem.js | 84 +++++++++++++++++++---------- 3 files changed, 93 insertions(+), 47 deletions(-) diff --git a/examples/example/games/satellite.js b/examples/example/games/satellite.js index 8e37472ea4..7f1dc8dd46 100644 --- a/examples/example/games/satellite.js +++ b/examples/example/games/satellite.js @@ -125,14 +125,14 @@ SatelliteCreator = function() { this.isActive = true; MyAvatar.position = { - x: 200, - y: 200, - z: 200 + x: 1000, + y: 1000, + z: 1000 }; Camera.setPosition({ - x: 200, - y: 200, - z: 200 + x: 1000, + y: 1000, + z: 1000 }); // Create earth model diff --git a/examples/example/planets-ui.js b/examples/example/planets-ui.js index 56cf7b18e1..6446e82b0e 100644 --- a/examples/example/planets-ui.js +++ b/examples/example/planets-ui.js @@ -125,7 +125,7 @@ var tooltipWidget = new UI.Label({ function addTooltip(widget, text) { widget.addAction('onMouseOver', function(event, widget) { tooltipWidget.setVisible(true); - tooltipWidget.setPosition(widget.position.x + widget.getWidth() + 20, widget.position.y); + tooltipWidget.setPosition(widget.position.x + widget.getWidth() + 20, widget.position.y + 10); tooltipWidget.setText(text); UI.updateLayout(); }); @@ -170,15 +170,19 @@ pauseButton.addAction('onClick', function() { } }); -// rideButton.addAction('onClick', function() { -// Script.include('orbit-ride.js'); -// new OrbitRider(); -// }); +rideButton.addAction('onClick', function() { + if (!paused) { + pause(); + } + var confirmed = Window.confirm('Ride through the solar system?'); + if (confirmed) { + Script.include('https://hifi-staff.s3.amazonaws.com/bridget/tween.js'); + init(); + } +}); restartButton.addAction('onClick', function() { - Script.stop(); - var thisScript = Script.resolvePath("solarsystem.js"); - Script.load(thisScript); + restart(); }); var zoomPanel = addPanel({ @@ -190,18 +194,27 @@ for (var i = 0; i < planets.length; ++i) { var label = zoomPanel.add(new UI.Label({ text: planets[i].name, width: 80, - height: 20, - backgroundAlpha: 1.0 + height: 20 })); zoomButtons.push(label); UI.updateLayout(); } UI.updateLayout(); + + zoomButtons.forEach(function(button, i) { var planet = planets[i]; button.addAction('onClick', function() { - planet.zoom(); + if (!planets[i].isZoomed) { + planet.zoom(); + planet.isZoomed = true; + } else { + MyAvatar.position = startingPosition; + Camera.setPosition(cameraStart); + planet.isZoomed = false; + } + }); }); @@ -305,7 +318,7 @@ settingsPanel.showTrailsButton = addCheckbox(settingsPanel, "show trails", 120, } }); var g_multiplier = 1.0; -settingsPanel.gravitySlider = addSlider(settingsPanel, "gravity scale ", 200, g_multiplier, 0.0, 5.0, function (value) { +settingsPanel.gravitySlider = addSlider(settingsPanel, "gravity scale ", 200, g_multiplier, 0.0, 5.0, function(value) { g_multiplier = value; GRAVITY = REFERENCE_GRAVITY * g_multiplier; }); @@ -313,7 +326,7 @@ settingsPanel.gravitySlider = addSlider(settingsPanel, "gravity scale ", 200, g_ var period_multiplier = 1.0; var last_alpha = period_multiplier; -settingsPanel.periodSlider = addSlider(settingsPanel, "orbital period scale ", 200, period_multiplier, 0.0, 3.0, function (value) { +settingsPanel.periodSlider = addSlider(settingsPanel, "orbital period scale ", 200, period_multiplier, 0.0, 3.0, function(value) { period_multiplier = value; changePeriod(period_multiplier); }); @@ -428,12 +441,13 @@ panels.map(function(panel) { // Cleanup script resources function teardown() { + UI.teardown(); if (satelliteGame) { satelliteGame.quitGame(); } - UI.teardown(); }; + UI.debug.setVisible(false); var inputHandler = { @@ -456,3 +470,5 @@ Controller.mousePressEvent.connect(inputHandler.onMousePress); Controller.mouseMoveEvent.connect(inputHandler.onMouseMove); Controller.mouseReleaseEvent.connect(inputHandler.onMouseRelease); Controller.mouseDoublePressEvent.connect(inputHandler.onMouseDoublePress); + +Script.scriptEnding.connect(teardown); \ No newline at end of file diff --git a/examples/example/solarsystem.js b/examples/example/solarsystem.js index 3e4a39ed71..bcd7284e14 100644 --- a/examples/example/solarsystem.js +++ b/examples/example/solarsystem.js @@ -37,7 +37,7 @@ CreateSimulation = function() { // Place the sun - var MAX_RANGE = this.MAX_RANGE = 80.0; + var MAX_RANGE = this.MAX_RANGE = 100.0; var center = this.center = Vec3.sum(startingPosition, Vec3.multiply(MAX_RANGE, Vec3.normalize(Quat.getFront(Camera.getOrientation())))); var SUN_SIZE = this.SUN_SIZE = 7.0; @@ -75,7 +75,7 @@ CreateSimulation = function() { var planets = this.planets = []; var trailsEnabled = this.trailsEnabled = true; - var MAX_POINTS_PER_LINE = this.MAX_POINTS_PER_LINE = 60; + var MAX_POINTS_PER_LINE = this.MAX_POINTS_PER_LINE = 40; var LINE_DIM = this.LINE_DIM = 200; var LINE_WIDTH = this.LINE_WIDTH = 20; @@ -108,7 +108,6 @@ CreateSimulation = function() { } else { // Begin overwriting first lines after one full revolution (one period) var firstLine = lineStack.shift(); - print("editing first line"); Entities.editEntity(firstLine, { position: point, linePoints: [{ @@ -148,7 +147,6 @@ CreateSimulation = function() { } var Planet = function(name, trailColor, radiusScale, sizeScale) { - this.name = name; this.trailColor = trailColor; @@ -161,6 +159,7 @@ CreateSimulation = function() { y: 0.0, z: 0.0 }); + this.startPosition = this.position; this.period = (2.0 * Math.PI) * Math.sqrt(Math.pow(this.radius, 3.0) / (GRAVITY * LARGE_BODY_MASS)); this.initialVelocity = Math.sqrt((GRAVITY * LARGE_BODY_MASS) / this.radius); @@ -220,7 +219,7 @@ CreateSimulation = function() { visible: false }); } - this.lineStack = []; + } this.resetTrails = function() { @@ -249,10 +248,11 @@ CreateSimulation = function() { }; this.zoom = function() { - pause(); - + if (!paused) { + pause(); + } this.tweening = true; - var tweenTime = 800; + var tweenTime = 1000; var startingPosition = MyAvatar.position; var endingPosition = Vec3.sum({ x: 0, @@ -274,12 +274,12 @@ CreateSimulation = function() { to(endProps, tweenTime). easing(TWEEN.Easing.Cubic.InOut). onUpdate(function() { - print(JSON.stringify(currentProps)); MyAvatar.position = { x: currentProps.x, y: currentProps.y, z: currentProps.z }; + Camera.lookAt(endingPosition); }).start(); moveTween.onComplete(function() { @@ -433,23 +433,31 @@ CreateSimulation = function() { var trails = this.trails = []; var paused = this.paused = false; + var time = 0; + var elapsed; + var TIME_STEP = 80; + this.update = function(deltaTime) { - if (paused) { - return; - } for (var i = 0; i < planets.length; ++i) { - if (planets[i].tweening) { - TWEEN.update(); - continue; + TWEEN.update(); + if (paused) { + return; } planets[i].update(deltaTime); } + + time++; + if (time % TIME_STEP == 0) { + elapsed++; + } + } Script.include('planets-ui.js'); // Clean up models, UI panels, lines, and button overlays this.scriptEnding = function() { + Entities.deleteEntity(theSun); for (var i = 0; i < planets.length; ++i) { Entities.deleteEntity(planets[i].planet); @@ -461,20 +469,42 @@ CreateSimulation = function() { Entities.deleteEntity(e[i]); } } - }; - Script.update.connect(update); - + }; + + this.restart = function() { + if (paused) { + resume(); + } + for (var i = 0; i < planets.length; ++i) { + Entities.deleteEntity(planets[i].planet); + planets[i].clearTrails(); + } + planets.length = 0; + planets.push(new Planet("mercury", MERCURY_LINE_COLOR, 0.387, 0.383)); + planets.push(new Planet("venus", VENUS_LINE_COLOR, 0.723, 0.949)); + planets.push(new Planet("earth", EARTH_LINE_COLOR, 1.0, 1.0)); + planets.push(new Planet("mars", MARS_LINE_COLOR, 1.52, 0.532)); + planets.push(new Planet("jupiter", JUPITER_LINE_COLOR, 5.20, 11.21)); + planets.push(new Planet("saturn", SATURN_LINE_COLOR, 9.58, 9.45)); + planets.push(new Planet("uranus", URANUS_LINE_COLOR, 19.20, 4.01)); + planets.push(new Planet("neptune", NEPTUNE_LINE_COLOR, 30.05, 3.88)); + planets.push(new Planet("pluto", PLUTO_LINE_COLOR, 39.48, 0.186)); + + + for (var i = 0; i < planets.length; ++i) { + planets[i].resetTrails(); + } + + elapsed = 0.0; + MyAvatar.position = startingPosition; + Camera.setPosition(cameraStart); + }; + + } - -Script.scriptEnding.connect(function() { - scriptEnding(); - teardown(); -}); - CreateSimulation(); - - - +Script.update.connect(update); +Script.scriptEnding.connect(scriptEnding); \ No newline at end of file From 58713c0d57c5c4b821ab83f290798d8aa76a208e Mon Sep 17 00:00:00 2001 From: bwent Date: Fri, 14 Aug 2015 09:55:05 -0700 Subject: [PATCH 07/12] more minor bug fixes --- examples/example/games/satellite.js | 12 +++++- examples/example/planets-ui.js | 58 ++++++++++++++++++++++++++--- examples/example/solarsystem.js | 55 +++++++++++---------------- 3 files changed, 85 insertions(+), 40 deletions(-) diff --git a/examples/example/games/satellite.js b/examples/example/games/satellite.js index 7f1dc8dd46..c0914ebe3c 100644 --- a/examples/example/games/satellite.js +++ b/examples/example/games/satellite.js @@ -40,6 +40,7 @@ SatelliteCreator = function() { var LIGHT_INTENSITY = 1.5; var center, distance; + var earth; Earth = function(position, size) { @@ -107,6 +108,7 @@ SatelliteCreator = function() { }); this.cleanup = function() { + print('cleaning up earth models'); Entities.deleteEntity(this.clouds); Entities.deleteEntity(this.earth); Entities.deleteEntity(this.zone); @@ -138,7 +140,7 @@ SatelliteCreator = function() { // Create earth model center = Vec3.sum(Camera.getPosition(), Vec3.multiply(MAX_RANGE, Quat.getFront(Camera.getOrientation()))); distance = Vec3.length(Vec3.subtract(center, Camera.getPosition())); - var earth = new Earth(center, EARTH_SIZE); + earth = new Earth(center, EARTH_SIZE); return true; }; @@ -289,12 +291,18 @@ SatelliteCreator = function() { } this.quitGame = function() { + if(!this.isActive) { + return; + } + print("ending satellite game"); + this.isActive = false; + for (var i = 0; i < satellites.length; i++) { Entities.deleteEntity(satellites[i].satellite); satellites[i].arrow.cleanup(); } - this.isActive = false; earth.cleanup(); + } diff --git a/examples/example/planets-ui.js b/examples/example/planets-ui.js index 6446e82b0e..f749f97337 100644 --- a/examples/example/planets-ui.js +++ b/examples/example/planets-ui.js @@ -22,7 +22,7 @@ var ICON_HEIGHT = 40.0; var ICON_COLOR = UI.rgba(45, 45, 45, 0.7); var FOCUSED_COLOR = UI.rgba(250, 250, 250, 1.0); -var PANEL_BACKGROUND_COLOR = UI.rgba(100, 100, 100, 0.7); +var PANEL_BACKGROUND_COLOR = UI.rgba(120, 120, 120, 0.8); var PANEL_PADDING = 7.0; var PANEL_BORDER = 12.0; @@ -162,7 +162,17 @@ var restartButton = addImage(systemViewPanel, 'reverse'); var pauseButton = addImage(systemViewPanel, 'playpause'); var rideButton = addImage(systemViewPanel, 'forward'); +var tweening, tweeningPaused; +Script.include('https://hifi-staff.s3.amazonaws.com/bridget/tween.js'); + pauseButton.addAction('onClick', function() { + if (tweening) { + if(!tweeningPaused) { + tweeningPaused = true; + } else { + tweeningPaused = false; + } + } if (!paused) { pause(); } else { @@ -170,19 +180,47 @@ pauseButton.addAction('onClick', function() { } }); +// Allow to toggle pause with spacebar +function keyPressEvent(event) { + if(event.text == "SPACE") { + if (tweening) { + if(!tweeningPaused) { + tweeningPaused = true; + } else { + tweeningPaused = false; + } + } + if (!paused) { + pause(); + } else { + resume(); + } + } +} + rideButton.addAction('onClick', function() { if (!paused) { pause(); + } + if (tweening) { + tweening = false; + tweeningPaused = true; + restart(); + return; } var confirmed = Window.confirm('Ride through the solar system?'); if (confirmed) { - Script.include('https://hifi-staff.s3.amazonaws.com/bridget/tween.js'); init(); + tweening = true; + tweeningPaused = false; } + }); restartButton.addAction('onClick', function() { restart(); + tweening = false; + }); var zoomPanel = addPanel({ @@ -202,17 +240,19 @@ for (var i = 0; i < planets.length; ++i) { UI.updateLayout(); - +var zoomView = false; zoomButtons.forEach(function(button, i) { var planet = planets[i]; button.addAction('onClick', function() { if (!planets[i].isZoomed) { planet.zoom(); planet.isZoomed = true; + zoomView = true; } else { MyAvatar.position = startingPosition; Camera.setPosition(cameraStart); planet.isZoomed = false; + zoomView = false; } }); @@ -305,7 +345,8 @@ function addSlider(parent, label, labelWidth, defaultValue, min, max, valueChang return slider; } -settingsPanel.showTrailsButton = addCheckbox(settingsPanel, "show trails", 120, trailsEnabled, function(trailsEnabled) { +settingsPanel.showTrailsButton = addCheckbox(settingsPanel, "show trails", 120, trailsEnabled, function(value) { + trailsEnabled = value; if (trailsEnabled) { for (var i = 0; i < planets.length; ++i) { planets[i].resetTrails(); @@ -347,7 +388,9 @@ satelliteButton.addAction('onClick', function() { if (satelliteGame && satelliteGame.isActive) { MyAvatar.position = startingPosition; satelliteGame.quitGame(); - resume(); + if(paused) { + resume(); + } } else { pause(); satelliteGame = new SatelliteCreator(); @@ -402,6 +445,9 @@ systemViewPanel.addAction('onMouseOver', function() { zoomButton.addAction('onClick', function() { + if (zoomView) { + restart(); + } hideSubpanelsExcept(zoomPanel); UI.updateLayout(); }); @@ -471,4 +517,6 @@ Controller.mouseMoveEvent.connect(inputHandler.onMouseMove); Controller.mouseReleaseEvent.connect(inputHandler.onMouseRelease); Controller.mouseDoublePressEvent.connect(inputHandler.onMouseDoublePress); +Controller.keyPressEvent.connect(keyPressEvent); + Script.scriptEnding.connect(teardown); \ No newline at end of file diff --git a/examples/example/solarsystem.js b/examples/example/solarsystem.js index bcd7284e14..7c58cfb299 100644 --- a/examples/example/solarsystem.js +++ b/examples/example/solarsystem.js @@ -22,6 +22,9 @@ CreateSimulation = function() { Script.include("https://hifi-public.s3.amazonaws.com/eric/scripts/tween.js"); Script.include('games/satellite.js'); + + trailsEnabled = true; + var DAMPING = this.DAMPING = 0.0; var LIFETIME = this.LIFETIME = 6000; var BASE_URL = this.BASE_URL = "https://s3.amazonaws.com/hifi-public/marketplace/hificontent/Scripts/planets/planets/"; @@ -74,7 +77,6 @@ CreateSimulation = function() { var REFERENCE_GRAVITY = this.REFERENCE_GRAVITY = GRAVITY; var planets = this.planets = []; - var trailsEnabled = this.trailsEnabled = true; var MAX_POINTS_PER_LINE = this.MAX_POINTS_PER_LINE = 40; var LINE_DIM = this.LINE_DIM = 200; var LINE_WIDTH = this.LINE_WIDTH = 20; @@ -213,6 +215,7 @@ CreateSimulation = function() { }; this.clearTrails = function() { + elapsed = 0.0; for (var j = 0; j < this.lineStack.length; ++j) { Entities.editEntity(this.lineStack[j], { @@ -279,10 +282,10 @@ CreateSimulation = function() { y: currentProps.y, z: currentProps.z }; - Camera.lookAt(endingPosition); }).start(); moveTween.onComplete(function() { + Camera.lookAt(endingPosition); this.tweening = false; }); @@ -340,16 +343,19 @@ CreateSimulation = function() { blue: 255 }; - planets.push(new Planet("mercury", MERCURY_LINE_COLOR, 0.387, 0.383)); - planets.push(new Planet("venus", VENUS_LINE_COLOR, 0.723, 0.949)); - planets.push(new Planet("earth", EARTH_LINE_COLOR, 1.0, 1.0)); - planets.push(new Planet("mars", MARS_LINE_COLOR, 1.52, 0.532)); - planets.push(new Planet("jupiter", JUPITER_LINE_COLOR, 5.20, 11.21)); - planets.push(new Planet("saturn", SATURN_LINE_COLOR, 9.58, 9.45)); - planets.push(new Planet("uranus", URANUS_LINE_COLOR, 19.20, 4.01)); - planets.push(new Planet("neptune", NEPTUNE_LINE_COLOR, 30.05, 3.88)); - planets.push(new Planet("pluto", PLUTO_LINE_COLOR, 39.48, 0.186)); - + this.initPlanets = function() { + planets.push(new Planet("mercury", MERCURY_LINE_COLOR, 0.387, 0.383)); + planets.push(new Planet("venus", VENUS_LINE_COLOR, 0.723, 0.949)); + planets.push(new Planet("earth", EARTH_LINE_COLOR, 1.0, 1.0)); + planets.push(new Planet("mars", MARS_LINE_COLOR, 1.52, 0.532)); + planets.push(new Planet("jupiter", JUPITER_LINE_COLOR, 5.20, 11.21)); + planets.push(new Planet("saturn", SATURN_LINE_COLOR, 9.58, 9.45)); + planets.push(new Planet("uranus", URANUS_LINE_COLOR, 19.20, 4.01)); + planets.push(new Planet("neptune", NEPTUNE_LINE_COLOR, 30.05, 3.88)); + planets.push(new Planet("pluto", PLUTO_LINE_COLOR, 39.48, 0.186)); + } + initPlanets(); + var LABEL_X = 8.0; var LABEL_Y = 3.0; var LABEL_Z = 1.0; @@ -439,7 +445,6 @@ CreateSimulation = function() { this.update = function(deltaTime) { for (var i = 0; i < planets.length; ++i) { - TWEEN.update(); if (paused) { return; } @@ -479,32 +484,16 @@ CreateSimulation = function() { Entities.deleteEntity(planets[i].planet); planets[i].clearTrails(); } - planets.length = 0; - planets.push(new Planet("mercury", MERCURY_LINE_COLOR, 0.387, 0.383)); - planets.push(new Planet("venus", VENUS_LINE_COLOR, 0.723, 0.949)); - planets.push(new Planet("earth", EARTH_LINE_COLOR, 1.0, 1.0)); - planets.push(new Planet("mars", MARS_LINE_COLOR, 1.52, 0.532)); - planets.push(new Planet("jupiter", JUPITER_LINE_COLOR, 5.20, 11.21)); - planets.push(new Planet("saturn", SATURN_LINE_COLOR, 9.58, 9.45)); - planets.push(new Planet("uranus", URANUS_LINE_COLOR, 19.20, 4.01)); - planets.push(new Planet("neptune", NEPTUNE_LINE_COLOR, 30.05, 3.88)); - planets.push(new Planet("pluto", PLUTO_LINE_COLOR, 39.48, 0.186)); - - - for (var i = 0; i < planets.length; ++i) { - planets[i].resetTrails(); - } - + time = 0.0; elapsed = 0.0; + planets.length = 0; + initPlanets(); + MyAvatar.position = startingPosition; Camera.setPosition(cameraStart); }; - - } - CreateSimulation(); - Script.update.connect(update); Script.scriptEnding.connect(scriptEnding); \ No newline at end of file From 2c5c362957e87d608c559c201350724d28642a48 Mon Sep 17 00:00:00 2001 From: bwent Date: Fri, 14 Aug 2015 10:19:57 -0700 Subject: [PATCH 08/12] adding rocket and refresh icons --- examples/example/planets-ui.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/example/planets-ui.js b/examples/example/planets-ui.js index f749f97337..05e1d134eb 100644 --- a/examples/example/planets-ui.js +++ b/examples/example/planets-ui.js @@ -158,9 +158,9 @@ var systemViewPanel = addPanel({ dir: '+x', visible: false }); -var restartButton = addImage(systemViewPanel, 'reverse'); +var restartButton = addImage(systemViewPanel, 'refresh'); var pauseButton = addImage(systemViewPanel, 'playpause'); -var rideButton = addImage(systemViewPanel, 'forward'); +var rideButton = addImage(systemViewPanel, 'rocket'); var tweening, tweeningPaused; Script.include('https://hifi-staff.s3.amazonaws.com/bridget/tween.js'); From 68fcf4ec8ac29999040b2cffd696f8511d86f970 Mon Sep 17 00:00:00 2001 From: bwent Date: Fri, 14 Aug 2015 11:06:44 -0700 Subject: [PATCH 09/12] formatting --- examples/example/planets-ui.js | 16 ++++++++-------- examples/example/solarsystem.js | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/example/planets-ui.js b/examples/example/planets-ui.js index 05e1d134eb..55e58de227 100644 --- a/examples/example/planets-ui.js +++ b/examples/example/planets-ui.js @@ -167,11 +167,11 @@ Script.include('https://hifi-staff.s3.amazonaws.com/bridget/tween.js'); pauseButton.addAction('onClick', function() { if (tweening) { - if(!tweeningPaused) { + if (!tweeningPaused) { tweeningPaused = true; } else { tweeningPaused = false; - } + } } if (!paused) { pause(); @@ -182,13 +182,13 @@ pauseButton.addAction('onClick', function() { // Allow to toggle pause with spacebar function keyPressEvent(event) { - if(event.text == "SPACE") { - if (tweening) { - if(!tweeningPaused) { + if (event.text == "SPACE") { + if (tweening) { + if (!tweeningPaused) { tweeningPaused = true; } else { tweeningPaused = false; - } + } } if (!paused) { pause(); @@ -201,7 +201,7 @@ function keyPressEvent(event) { rideButton.addAction('onClick', function() { if (!paused) { pause(); - } + } if (tweening) { tweening = false; tweeningPaused = true; @@ -388,7 +388,7 @@ satelliteButton.addAction('onClick', function() { if (satelliteGame && satelliteGame.isActive) { MyAvatar.position = startingPosition; satelliteGame.quitGame(); - if(paused) { + if (paused) { resume(); } } else { diff --git a/examples/example/solarsystem.js b/examples/example/solarsystem.js index 7c58cfb299..05f10eeebb 100644 --- a/examples/example/solarsystem.js +++ b/examples/example/solarsystem.js @@ -24,7 +24,7 @@ CreateSimulation = function() { trailsEnabled = true; - + var DAMPING = this.DAMPING = 0.0; var LIFETIME = this.LIFETIME = 6000; var BASE_URL = this.BASE_URL = "https://s3.amazonaws.com/hifi-public/marketplace/hificontent/Scripts/planets/planets/"; @@ -222,7 +222,7 @@ CreateSimulation = function() { visible: false }); } - + } this.resetTrails = function() { @@ -344,7 +344,7 @@ CreateSimulation = function() { }; this.initPlanets = function() { - planets.push(new Planet("mercury", MERCURY_LINE_COLOR, 0.387, 0.383)); + planets.push(new Planet("mercury", MERCURY_LINE_COLOR, 0.387, 0.383)); planets.push(new Planet("venus", VENUS_LINE_COLOR, 0.723, 0.949)); planets.push(new Planet("earth", EARTH_LINE_COLOR, 1.0, 1.0)); planets.push(new Planet("mars", MARS_LINE_COLOR, 1.52, 0.532)); @@ -355,7 +355,7 @@ CreateSimulation = function() { planets.push(new Planet("pluto", PLUTO_LINE_COLOR, 39.48, 0.186)); } initPlanets(); - + var LABEL_X = 8.0; var LABEL_Y = 3.0; var LABEL_Z = 1.0; From 7bd9e2653ca14aef12940a77f3492c4c42e88686 Mon Sep 17 00:00:00 2001 From: bwent Date: Fri, 14 Aug 2015 15:09:48 -0700 Subject: [PATCH 10/12] more bug fixes --- examples/example/games/satellite.js | 4 ---- examples/example/planets-ui.js | 11 ++--------- examples/example/solarsystem.js | 19 ++++++++++++------- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/examples/example/games/satellite.js b/examples/example/games/satellite.js index c0914ebe3c..db65198b87 100644 --- a/examples/example/games/satellite.js +++ b/examples/example/games/satellite.js @@ -20,7 +20,6 @@ Script.include('../utilities/tools/vector.js'); var URL = "https://s3.amazonaws.com/hifi-public/marketplace/hificontent/Scripts/planets/"; SatelliteCreator = function() { - print("initializing satellite game"); var MAX_RANGE = 50.0; @@ -291,9 +290,6 @@ SatelliteCreator = function() { } this.quitGame = function() { - if(!this.isActive) { - return; - } print("ending satellite game"); this.isActive = false; diff --git a/examples/example/planets-ui.js b/examples/example/planets-ui.js index 55e58de227..afca55a65e 100644 --- a/examples/example/planets-ui.js +++ b/examples/example/planets-ui.js @@ -165,6 +165,7 @@ var rideButton = addImage(systemViewPanel, 'rocket'); var tweening, tweeningPaused; Script.include('https://hifi-staff.s3.amazonaws.com/bridget/tween.js'); + pauseButton.addAction('onClick', function() { if (tweening) { if (!tweeningPaused) { @@ -172,6 +173,7 @@ pauseButton.addAction('onClick', function() { } else { tweeningPaused = false; } + return; } if (!paused) { pause(); @@ -183,13 +185,6 @@ pauseButton.addAction('onClick', function() { // Allow to toggle pause with spacebar function keyPressEvent(event) { if (event.text == "SPACE") { - if (tweening) { - if (!tweeningPaused) { - tweeningPaused = true; - } else { - tweeningPaused = false; - } - } if (!paused) { pause(); } else { @@ -214,13 +209,11 @@ rideButton.addAction('onClick', function() { tweening = true; tweeningPaused = false; } - }); restartButton.addAction('onClick', function() { restart(); tweening = false; - }); var zoomPanel = addPanel({ diff --git a/examples/example/solarsystem.js b/examples/example/solarsystem.js index 05f10eeebb..139efe3ad8 100644 --- a/examples/example/solarsystem.js +++ b/examples/example/solarsystem.js @@ -21,9 +21,8 @@ CreateSimulation = function() { Script.include("https://hifi-public.s3.amazonaws.com/eric/scripts/tween.js"); Script.include('games/satellite.js'); - - - trailsEnabled = true; + + var trailsEnabled = this.trailsEnabled = true; var DAMPING = this.DAMPING = 0.0; var LIFETIME = this.LIFETIME = 6000; @@ -127,6 +126,9 @@ CreateSimulation = function() { } this.pause = function() { + if(paused) { + return; + } for (var i = 0; i < planets.length; ++i) { Entities.editEntity(planets[i].planet, { velocity: { @@ -142,6 +144,9 @@ CreateSimulation = function() { } this.resume = function() { + if(!paused) { + return; + } for (var i = 0; i < planets.length; ++i) { planets[i].label.hide(); } @@ -216,18 +221,15 @@ CreateSimulation = function() { this.clearTrails = function() { elapsed = 0.0; - for (var j = 0; j < this.lineStack.length; ++j) { Entities.editEntity(this.lineStack[j], { visible: false }); } - } this.resetTrails = function() { elapsed = 0.0; - this.trail = []; this.lineStack = []; //add the first line to both the line entity stack and the trail @@ -488,12 +490,15 @@ CreateSimulation = function() { elapsed = 0.0; planets.length = 0; initPlanets(); + MyAvatar.position = startingPosition; Camera.setPosition(cameraStart); }; + } CreateSimulation(); Script.update.connect(update); -Script.scriptEnding.connect(scriptEnding); \ No newline at end of file +Script.scriptEnding.connect(scriptEnding); + From 7834a18bcfa5d82b4fd8fab5a7ed14ef7a43535d Mon Sep 17 00:00:00 2001 From: bwent Date: Fri, 14 Aug 2015 15:11:05 -0700 Subject: [PATCH 11/12] add untracked changes --- examples/example/solarsystem.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/examples/example/solarsystem.js b/examples/example/solarsystem.js index 139efe3ad8..0a16bf52f7 100644 --- a/examples/example/solarsystem.js +++ b/examples/example/solarsystem.js @@ -21,7 +21,7 @@ CreateSimulation = function() { Script.include("https://hifi-public.s3.amazonaws.com/eric/scripts/tween.js"); Script.include('games/satellite.js'); - + var trailsEnabled = this.trailsEnabled = true; var DAMPING = this.DAMPING = 0.0; @@ -126,7 +126,7 @@ CreateSimulation = function() { } this.pause = function() { - if(paused) { + if (paused) { return; } for (var i = 0; i < planets.length; ++i) { @@ -144,7 +144,7 @@ CreateSimulation = function() { } this.resume = function() { - if(!paused) { + if (!paused) { return; } for (var i = 0; i < planets.length; ++i) { @@ -490,7 +490,7 @@ CreateSimulation = function() { elapsed = 0.0; planets.length = 0; initPlanets(); - + MyAvatar.position = startingPosition; Camera.setPosition(cameraStart); @@ -500,5 +500,4 @@ CreateSimulation = function() { CreateSimulation(); Script.update.connect(update); -Script.scriptEnding.connect(scriptEnding); - +Script.scriptEnding.connect(scriptEnding); \ No newline at end of file From 8609129228b3ad1855cf7d27c19e98f3311501f4 Mon Sep 17 00:00:00 2001 From: bwent Date: Fri, 14 Aug 2015 17:07:01 -0700 Subject: [PATCH 12/12] fix header --- examples/example/solarsystem.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/examples/example/solarsystem.js b/examples/example/solarsystem.js index 0a16bf52f7..7b9967b001 100644 --- a/examples/example/solarsystem.js +++ b/examples/example/solarsystem.js @@ -1,17 +1,11 @@ // // solarsystem.js -// games +// example // // Created by Bridget Went, 5/28/15. // Copyright 2015 High Fidelity, Inc. // -// The start to a project to build a virtual physics classroom to simulate the solar system, gravity, and orbital physics. -// - A sun with oribiting planets is created in front of the user -// - UI elements allow for adjusting the period, gravity, trails, and energy recalculations -// - Click "PAUSE" to pause the animation and show planet labels -// - In this mode, double-click a planet label to zoom in on that planet -// -Double-clicking on earth label initiates satellite orbiter game -// -Press "TAB" to toggle back to solar system view +// A project to build a virtual physics classroom to simulate the solar system, gravity, and orbital physics. // // // Distributed under the Apache License, Version 2.0. @@ -500,4 +494,4 @@ CreateSimulation = function() { CreateSimulation(); Script.update.connect(update); -Script.scriptEnding.connect(scriptEnding); \ No newline at end of file +Script.scriptEnding.connect(scriptEnding);