This commit is contained in:
James B. Pollack 2016-03-04 17:29:22 -08:00
parent e3d44c4e27
commit 691c0c7d24
2 changed files with 127 additions and 49 deletions

View file

@ -43,8 +43,8 @@ var bubbleSound = SoundCache.getSound(BUBBLE_SOUND_URL);
var URCHIN_FORWARD_OFFSET =-TANK_DIMENSIONS.x; var URCHIN_FORWARD_OFFSET =-TANK_DIMENSIONS.x;
//depth of tank //depth of tank
var URCHIN_LATERAL_OFFSET = 0.15; var URCHIN_LATERAL_OFFSET = -0.15;
var URCHIN_VERTICAL_OFFSET = -0.4; var URCHIN_VERTICAL_OFFSET = -0.37;
var URCHIN_MODEL_URL = 'http://hifi-content.s3.amazonaws.com/DomainContent/Home/fishTank/Urchin.fbx'; 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; var ROCKS_FORWARD_OFFSET = (TANK_DIMENSIONS.x/2)-0.75;
//depth of tank //depth of tank
var ROCKS_LATERAL_OFFSET = 0.05; var ROCKS_LATERAL_OFFSET = 0.0;
var ROCKS_VERTICAL_OFFSET = -0.45; 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'; 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; bubbleProperties.position = finalOffset;
bubbleSystem = Entities.addEntity(bubbleProperties); bubbleSystem = Entities.addEntity(bubbleProperties);
createBubbleSound(finalOffset); //createBubbleSound(finalOffset);
} }
@ -169,7 +169,7 @@ function getOffsetFromTankCenter(VERTICAL_OFFSET, FORWARD_OFFSET, LATERAL_OFFSET
function createBubbleSound(position) { function createBubbleSound(position) {
var audioProperties = { var audioProperties = {
volume: 1, volume: 0,
position: position, position: position,
loop: true loop: true
}; };
@ -297,8 +297,8 @@ createUrchin();
createRocks(); createRocks();
var customKey = 'hifi-home-fishtank'; var customKey = 'hifi-home-fishtank';
var id = fishTank;
print('FISH TANK ID AT START:: ' + id)
var data = { var data = {
fishLoaded: false, fishLoaded: false,
bubbleSystem: bubbleSystem, bubbleSystem: bubbleSystem,
@ -310,11 +310,19 @@ var data = {
innerContainer: innerContainer, innerContainer: innerContainer,
} }
print('DATA AT CREATE IS:::' + JSON.stringify(data));
setEntityCustomData(customKey, id, data); //fisthank initialize has a different UUID than the model that i see
setEntityCustomData('grabbableKey', id, { Script.setTimeout(function(){
grabbable: false 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() { function cleanup() {
Entities.deleteEntity(fishTank); Entities.deleteEntity(fishTank);

View file

@ -1,7 +1,6 @@
(function() { (function() {
Script.include('../../../../examples/libraries/virtualBaton.js?' + Math.random()); 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 //only one person should simulate the tank at a time -- we pass around a virtual baton
var baton; var baton;
@ -71,16 +70,22 @@
}, },
initialize: function(entityID) { initialize: function(entityID) {
print('JBP fishtank initialize' + entityID) var properties = Entities.getEntityProperties(entityID)
var properties = Entities.getEntityProperties(entityID);
if (properties.userData.length === 0 || properties.hasOwnProperty('userData') === false) { if (properties.hasOwnProperty('userData') === false || properties.userData.length === 0) {
_this.initTimeout = Script.setTimeout(function() { _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); _this.initialize(entityID);
}, 1000) }, 1000)
} else { } else {
print('JBP userdata before parse attempt' + properties.userData) print('JBP userdata before parse attempt' + properties.userData)
_this.userData = null; _this.userData = null;
try { try {
@ -106,6 +111,9 @@
this.entityID = entityID; this.entityID = entityID;
_entityID = entityID; _entityID = entityID;
this.initialize(entityID); this.initialize(entityID);
// if(_this.initTimeout!==null){
// Script.clearTimeout(_this.initTimeout)
// }
this.initTimeout = null; this.initTimeout = null;
}, },
@ -124,7 +132,8 @@
} }
}, },
update: function() { update: function(deltaTime) {
if (iOwn === false) { if (iOwn === false) {
print('i dont own') print('i dont own')
//exit if we're not supposed to be simulating the fish //exit if we're not supposed to be simulating the fish
@ -132,7 +141,7 @@
} }
// print('i am the owner!') // print('i am the owner!')
//do stuff //do stuff
updateFish(); updateFish(deltaTime);
_this.seeIfOwnerIsLookingAtTheTank(); _this.seeIfOwnerIsLookingAtTheTank();
}, },
@ -166,11 +175,9 @@
}, },
debugSphereOff: function() { debugSphereOff: function() {
// Entities.deleteEntity(_this.debugSphere);
Entities.editEntity(_this.debugSphere, { Entities.editEntity(_this.debugSphere, {
visible: false visible: false
}) })
//_this.debugSphere = null;
}, },
@ -224,12 +231,9 @@
}; };
var brn = _this.userData['hifi-home-fishtank']['corners'].brn;
var userData = JSON.parse(_this.currentProperties.userData); var tfl = _this.userData['hifi-home-fishtank']['corners'].tfl;
var innerContainer = _this.userData['hifi-home-fishtank'].innerContainer;
var brn = userData['hifi-home-fishtank']['corners'].brn;
var tfl = userData['hifi-home-fishtank']['corners'].tfl;
var innerContainer = userData['hifi-home-fishtank'].innerContainer;
var intersection = Entities.findRayIntersection(pickRay, true, [innerContainer], [_this.entityID, brn, tfl]); var intersection = Entities.findRayIntersection(pickRay, true, [innerContainer], [_this.entityID, brn, tfl]);
@ -329,18 +333,24 @@
var TANK_WIDTH = TANK_DIMENSIONS.z / 2; var TANK_WIDTH = TANK_DIMENSIONS.z / 2;
var TANK_HEIGHT = TANK_DIMENSIONS.y / 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 MAX_SIGHT_DISTANCE = 1.5;
var MIN_SEPARATION = 0.15; var MIN_SEPARATION = 0.15;
var AVOIDANCE_FORCE = 0.32; var AVOIDANCE_FORCE = 0.032;
var COHESION_FORCE = 0.025; var COHESION_FORCE = 0.025;
var ALIGNMENT_FORCE = 0.025; var ALIGNMENT_FORCE = 0.025;
var LOOK_ATTRACTOR_FORCE = 0.029; var LOOK_ATTRACTOR_FORCE = 0.029;
var LOOK_ATTRACTOR_DISTANCE = 1.75; var LOOK_ATTRACTOR_DISTANCE = 1.75;
var SWIMMING_FORCE = 0.05; var SWIMMING_FORCE = 0.05;
var SWIMMING_SPEED = 0.5; 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 = false;
var THROTTLE_RATE = 100; var THROTTLE_RATE = 100;
@ -389,9 +399,18 @@
_this.tankLocked = true; _this.tankLocked = true;
print('NO FISH YET SO LOAD EM!!!') print('NO FISH YET SO LOAD EM!!!')
loadFish(NUM_FISH); loadFish(NUM_FISH);
setEntityCustomData(FISHTANK_USERDATA_KEY, _this.entityID, { var data = {
fishLoaded: true 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; _this.userData['hifi-home-fishtank'].fishLoaded = true;
Script.setTimeout(function() { Script.setTimeout(function() {
_this.fish = _this.findFishInTank(); _this.fish = _this.findFishInTank();
@ -426,6 +445,8 @@
y: 0, y: 0,
z: 0 z: 0
}; };
var userData = JSON.parse(_this.currentProperties.userData); var userData = JSON.parse(_this.currentProperties.userData);
var innerContainer = userData['hifi-home-fishtank']['innerContainer']; var innerContainer = userData['hifi-home-fishtank']['innerContainer'];
var bounds = Entities.getEntityProperties(innerContainer, "boundingBox").boundingBox; var bounds = Entities.getEntityProperties(innerContainer, "boundingBox").boundingBox;
@ -541,18 +562,26 @@
// Orient in direction of velocity // Orient in direction of velocity
var rotation = Quat.rotationBetween(Vec3.UNIT_NEG_Z, 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 VELOCITY_FOLLOW_RATE = 0.30;
var safeEuler = Quat.safeEulerAngles(rotation); 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 // Only update properties if they have changed, to save bandwidth
var MIN_POSITION_CHANGE_FOR_UPDATE = 0.001; var MIN_POSITION_CHANGE_FOR_UPDATE = 0.001;
if (Vec3.distance(properties.position, position) < MIN_POSITION_CHANGE_FOR_UPDATE) { if (Vec3.distance(properties.position, position) < MIN_POSITION_CHANGE_FOR_UPDATE) {
Entities.editEntity(fish[i], { Entities.editEntity(fish[i], {
velocity: velocity, velocity: velocity,
rotation: Quat.mix(properties.rotation, rotation, VELOCITY_FOLLOW_RATE) rotation: mixedRotation
}); });
} else { } else {
Entities.editEntity(fish[i], { Entities.editEntity(fish[i], {
@ -607,17 +636,14 @@
// w: 1 // w: 1
// }, // },
// type: "Box", // type: "Box",
// dimensions: { dimensions: FISH_DIMENSIONS,
// x: FISH_WIDTH, velocity: {
// y: FISH_WIDTH, x: SWIMMING_SPEED,
// z: FISH_LENGTH y: SWIMMING_SPEED,
// }, z: SWIMMING_SPEED
// velocity: { },
// x: SWIMMING_SPEED,
// y: SWIMMING_SPEED,
// z: SWIMMING_SPEED
// },
damping: FISH_DAMPING, damping: FISH_DAMPING,
angularDamping: FISH_ANGULAR_DAMPING,
dynamic: false, dynamic: false,
lifetime: LIFETIME, lifetime: LIFETIME,
color: { color: {
@ -638,5 +664,49 @@
Script.update.disconnect(_this.update); 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(); return new FishTank();
}); });