From 691c0c7d24115c0368b34f7c1aa3503c41482f91 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Fri, 4 Mar 2016 17:29:22 -0800 Subject: [PATCH] gofish! --- .../Home/fishTank/createFishTank.js | 34 +++-- .../DomainContent/Home/fishTank/tank.js | 142 +++++++++++++----- 2 files changed, 127 insertions(+), 49 deletions(-) diff --git a/unpublishedScripts/DomainContent/Home/fishTank/createFishTank.js b/unpublishedScripts/DomainContent/Home/fishTank/createFishTank.js index e84cab2a68..f52a56563d 100644 --- a/unpublishedScripts/DomainContent/Home/fishTank/createFishTank.js +++ b/unpublishedScripts/DomainContent/Home/fishTank/createFishTank.js @@ -43,8 +43,8 @@ var bubbleSound = SoundCache.getSound(BUBBLE_SOUND_URL); var URCHIN_FORWARD_OFFSET =-TANK_DIMENSIONS.x; //depth of tank -var URCHIN_LATERAL_OFFSET = 0.15; -var URCHIN_VERTICAL_OFFSET = -0.4; +var URCHIN_LATERAL_OFFSET = -0.15; +var URCHIN_VERTICAL_OFFSET = -0.37; var URCHIN_MODEL_URL = 'http://hifi-content.s3.amazonaws.com/DomainContent/Home/fishTank/Urchin.fbx'; @@ -56,8 +56,8 @@ var URCHIN_DIMENSIONS = { var ROCKS_FORWARD_OFFSET = (TANK_DIMENSIONS.x/2)-0.75; //depth of tank -var ROCKS_LATERAL_OFFSET = 0.05; -var ROCKS_VERTICAL_OFFSET = -0.45; +var ROCKS_LATERAL_OFFSET = 0.0; +var ROCKS_VERTICAL_OFFSET = (-TANK_DIMENSIONS.y/2)+0.25; var ROCK_MODEL_URL = 'http://hifi-content.s3.amazonaws.com/DomainContent/Home/fishTank/Aquarium-Rocks-2.fbx'; @@ -144,7 +144,7 @@ function createBubbleSystem() { bubbleProperties.position = finalOffset; bubbleSystem = Entities.addEntity(bubbleProperties); - createBubbleSound(finalOffset); + //createBubbleSound(finalOffset); } @@ -169,7 +169,7 @@ function getOffsetFromTankCenter(VERTICAL_OFFSET, FORWARD_OFFSET, LATERAL_OFFSET function createBubbleSound(position) { var audioProperties = { - volume: 1, + volume: 0, position: position, loop: true }; @@ -297,8 +297,8 @@ createUrchin(); createRocks(); var customKey = 'hifi-home-fishtank'; -var id = fishTank; -print('FISH TANK ID AT START:: ' + id) + + var data = { fishLoaded: false, bubbleSystem: bubbleSystem, @@ -310,11 +310,19 @@ var data = { innerContainer: innerContainer, } -print('DATA AT CREATE IS:::' + JSON.stringify(data)); -setEntityCustomData(customKey, id, data); -setEntityCustomData('grabbableKey', id, { - grabbable: false -}); + +//fisthank initialize has a different UUID than the model that i see +Script.setTimeout(function(){ + print('CREATE TIMEOUT!!!') + print('TANK AT CREATE IS::: '+ fishTank) + print('DATA AT CREATE IS:::' + JSON.stringify(data)); + +setEntityCustomData(customKey, fishTank, data); +// setEntityCustomData('grabbableKey', id, { +// grabbable: false +// }); +},2000) + function cleanup() { Entities.deleteEntity(fishTank); diff --git a/unpublishedScripts/DomainContent/Home/fishTank/tank.js b/unpublishedScripts/DomainContent/Home/fishTank/tank.js index 1fc174d4fb..5bcb4c99a8 100644 --- a/unpublishedScripts/DomainContent/Home/fishTank/tank.js +++ b/unpublishedScripts/DomainContent/Home/fishTank/tank.js @@ -1,7 +1,6 @@ (function() { Script.include('../../../../examples/libraries/virtualBaton.js?' + Math.random()); - Script.include('../../../../examples/libraries/utils.js?' + Math.random()); //only one person should simulate the tank at a time -- we pass around a virtual baton var baton; @@ -71,16 +70,22 @@ }, initialize: function(entityID) { - print('JBP fishtank initialize' + entityID) - var properties = Entities.getEntityProperties(entityID); - if (properties.userData.length === 0 || properties.hasOwnProperty('userData') === false) { + var properties = Entities.getEntityProperties(entityID) + + if (properties.hasOwnProperty('userData') === false || properties.userData.length === 0) { _this.initTimeout = Script.setTimeout(function() { - print('JBP no user data yet, try again in one second') + if (properties.hasOwnProperty('userData')) { + print('JBP has user data property') + } + if (properties.userData.length === 0) { + print('JBP user data length is zero') + } + + print('JBP try again in one second') _this.initialize(entityID); }, 1000) } else { - print('JBP userdata before parse attempt' + properties.userData) _this.userData = null; try { @@ -106,6 +111,9 @@ this.entityID = entityID; _entityID = entityID; this.initialize(entityID); + // if(_this.initTimeout!==null){ + // Script.clearTimeout(_this.initTimeout) + // } this.initTimeout = null; }, @@ -124,7 +132,8 @@ } }, - update: function() { + update: function(deltaTime) { + if (iOwn === false) { print('i dont own') //exit if we're not supposed to be simulating the fish @@ -132,7 +141,7 @@ } // print('i am the owner!') //do stuff - updateFish(); + updateFish(deltaTime); _this.seeIfOwnerIsLookingAtTheTank(); }, @@ -166,11 +175,9 @@ }, debugSphereOff: function() { - // Entities.deleteEntity(_this.debugSphere); Entities.editEntity(_this.debugSphere, { - visible: false - }) - //_this.debugSphere = null; + visible: false + }) }, @@ -224,12 +231,9 @@ }; - - var userData = JSON.parse(_this.currentProperties.userData); - - var brn = userData['hifi-home-fishtank']['corners'].brn; - var tfl = userData['hifi-home-fishtank']['corners'].tfl; - var innerContainer = userData['hifi-home-fishtank'].innerContainer; + var brn = _this.userData['hifi-home-fishtank']['corners'].brn; + var tfl = _this.userData['hifi-home-fishtank']['corners'].tfl; + var innerContainer = _this.userData['hifi-home-fishtank'].innerContainer; var intersection = Entities.findRayIntersection(pickRay, true, [innerContainer], [_this.entityID, brn, tfl]); @@ -329,18 +333,24 @@ var TANK_WIDTH = TANK_DIMENSIONS.z / 2; var TANK_HEIGHT = TANK_DIMENSIONS.y / 2; - var FISH_WIDTH = 0.03; - var FISH_LENGTH = 0.15; + + var FISH_DIMENSIONS = { + x: 0.0149, + y: 0.02546, + z: 0.0823 + } + var MAX_SIGHT_DISTANCE = 1.5; var MIN_SEPARATION = 0.15; - var AVOIDANCE_FORCE = 0.32; + var AVOIDANCE_FORCE = 0.032; var COHESION_FORCE = 0.025; var ALIGNMENT_FORCE = 0.025; var LOOK_ATTRACTOR_FORCE = 0.029; var LOOK_ATTRACTOR_DISTANCE = 1.75; var SWIMMING_FORCE = 0.05; var SWIMMING_SPEED = 0.5; - var FISH_DAMPING = 0.25; + var FISH_DAMPING = 0.55; + var FISH_ANGULAR_DAMPING = 0.55; var THROTTLE = false; var THROTTLE_RATE = 100; @@ -389,9 +399,18 @@ _this.tankLocked = true; print('NO FISH YET SO LOAD EM!!!') loadFish(NUM_FISH); - setEntityCustomData(FISHTANK_USERDATA_KEY, _this.entityID, { - fishLoaded: true - }); + var data = { + fishLoaded: true, + bubbleSystem: _this.userData['hifi-home-fishtank'].bubbleSystem, + bubbleSound: _this.userData['hifi-home-fishtank'].bubbleSound, + corners: { + brn: _this.userData['hifi-home-fishtank'].lowerCorner, + tfl: _this.userData['hifi-home-fishtank'].upperCorner + }, + innerContainer: _this.userData['hifi-home-fishtank'].innerContainer, + + } + setEntityCustomData(FISHTANK_USERDATA_KEY, _this.entityID, data); _this.userData['hifi-home-fishtank'].fishLoaded = true; Script.setTimeout(function() { _this.fish = _this.findFishInTank(); @@ -426,6 +445,8 @@ y: 0, z: 0 }; + + var userData = JSON.parse(_this.currentProperties.userData); var innerContainer = userData['hifi-home-fishtank']['innerContainer']; var bounds = Entities.getEntityProperties(innerContainer, "boundingBox").boundingBox; @@ -541,18 +562,26 @@ // Orient in direction of velocity var rotation = Quat.rotationBetween(Vec3.UNIT_NEG_Z, velocity); + + var mixedRotation =Quat.mix(properties.rotation, rotation, VELOCITY_FOLLOW_RATE); var VELOCITY_FOLLOW_RATE = 0.30; var safeEuler = Quat.safeEulerAngles(rotation); + safeEuler.z = safeEuler.z *= 0.925; + var newQuat = Quat.fromPitchYawRollDegrees(safeEuler.x, safeEuler.y, safeEuler.z); + + var finalQuat = Quat.multiply(rotation, newQuat); + + // Only update properties if they have changed, to save bandwidth var MIN_POSITION_CHANGE_FOR_UPDATE = 0.001; if (Vec3.distance(properties.position, position) < MIN_POSITION_CHANGE_FOR_UPDATE) { Entities.editEntity(fish[i], { velocity: velocity, - rotation: Quat.mix(properties.rotation, rotation, VELOCITY_FOLLOW_RATE) + rotation: mixedRotation }); } else { Entities.editEntity(fish[i], { @@ -607,17 +636,14 @@ // w: 1 // }, // type: "Box", - // dimensions: { - // x: FISH_WIDTH, - // y: FISH_WIDTH, - // z: FISH_LENGTH - // }, - // velocity: { - // x: SWIMMING_SPEED, - // y: SWIMMING_SPEED, - // z: SWIMMING_SPEED - // }, + dimensions: FISH_DIMENSIONS, + velocity: { + x: SWIMMING_SPEED, + y: SWIMMING_SPEED, + z: SWIMMING_SPEED + }, damping: FISH_DAMPING, + angularDamping: FISH_ANGULAR_DAMPING, dynamic: false, lifetime: LIFETIME, color: { @@ -638,5 +664,49 @@ Script.update.disconnect(_this.update); }) + + function setEntityUserData(id, data) { + var json = JSON.stringify(data) + Entities.editEntity(id, { + userData: json + }); + } + + // FIXME do non-destructive modification of the existing user data + function getEntityUserData(id) { + var results = null; + var properties = Entities.getEntityProperties(id, "userData"); + if (properties.userData) { + try { + results = JSON.parse(properties.userData); + } catch (err) { + // print('error parsing json'); + // print('properties are:'+ properties.userData); + } + } + return results ? results : {}; + } + + + // Non-destructively modify the user data of an entity. + function setEntityCustomData(customKey, id, data) { + var userData = getEntityUserData(id); + if (data == null) { + delete userData[customKey]; + } else { + userData[customKey] = data; + } + setEntityUserData(id, userData); + } + + function getEntityCustomData(customKey, id, defaultValue) { + var userData = getEntityUserData(id); + if (undefined != userData[customKey]) { + return userData[customKey]; + } else { + return defaultValue; + } + } + return new FishTank(); }); \ No newline at end of file