only look attractor if close

This commit is contained in:
James B. Pollack 2016-03-01 17:44:21 -08:00
parent 24da3b1019
commit 1bcd85d106
2 changed files with 232 additions and 47 deletions

View file

@ -1,5 +1,6 @@
var fishtank, bubbleSystem, bubbleSound;
var fishtank, bubbleSystem, bubbleSound, bubbleInjector;
var CLEANUP = true;
var TANK_DIMENSIONS = {
x: 1.3393,
y: 1.3515,
@ -15,7 +16,7 @@ var DEBUG_COLOR = {
blue: 255
}
var center = Vec3.sum(MyAvatar.position, Vec3.multiply(Quat.getFront(MyAvatar.orientation), 2 * TANK_WIDTH));
var center = Vec3.sum(MyAvatar.position, Vec3.multiply(Quat.getFront(MyAvatar.orientation), 1 * TANK_WIDTH));
var TANK_POSITION = center;
@ -23,9 +24,9 @@ var TANK_SCRIPT = Script.resolvePath('tank.js?' + Math.random())
var TANK_MODEL_URL = "http://hifi-content.s3.amazonaws.com/DomainContent/Home/fishTank/aquarium-6.fbx";
var BUBBLE_SYSTEM_FORWARD_OFFSET = 0.2;
var BUBBLE_SYSTEM_LATERAL_OFFSET = 0.2;
var BUBBLE_SYSTEM_VERTICAL_OFFSET = -0.2;
var BUBBLE_SYSTEM_FORWARD_OFFSET = 0.0;
var BUBBLE_SYSTEM_LATERAL_OFFSET = TANK_DIMENSIONS.x - 0.25;
var BUBBLE_SYSTEM_VERTICAL_OFFSET = -1;
var BUBBLE_SYSTEM_DIMENSIONS = {
x: TANK_DIMENSIONS.x / 8,
@ -65,7 +66,7 @@ function createFishTank() {
function createBubbleSystem() {
var tankProperties = Entities.getEntityProperties(fishTank);
var tankProperties = Entities.getEntityProperties(fishtank);
var bubbleProperties = {
"color": {},
"isEmitting": 1,
@ -130,31 +131,100 @@ function createBubbleSystem() {
finalOffset = Vec3.sum(finalOffset, frontOffset);
finalOffset = Vec3.sum(finalOffset, rightOffset);
print('final bubble offset:: ' + JSON.stringify(finalOffset));
bubbleProperties.position = finalOffset;
bubbleSystem = Entities.addEntity(bubbleProperties);
createBubbleSound(finalOffset);
// createBubbleSound(finalOffset);
}
function createBubbleSound(position) {
var audioProperties = {
volume: 0.2,
volume: 1,
position: position,
loop: true
};
Audio.playSound(bubbleSound, audioProperties);
bubbleInjector = Audio.playSound(bubbleSound, audioProperties);
}
function cleanup() {
Entities.deleteEntity(fishTank);
Entities.deleteEntity(bubbleSystem);
bubbleInjector.stop();
bubbleInjector = null;
}
function createInnerContainer(){
var containerProps = {
name:"hifi-home-fishtank-inner-container",
type:'Box',
color:{
red:0,
green:0,
blue:255
},
dimensions:{
}
};
}
function createEntitiesAtCorners() {
var bounds = Entities.getEntityProperties(fishTank, "boundingBox").boundingBox;
var lowerProps = {
name:'hifi-home-fishtank-lower-corner',
type: "Box",
dimensions: {
x: 0.2,
y: 0.2,
z: 0.2
},
color: {
red: 255,
green: 0,
blue: 0
},
collisionless: true,
position: bounds.brn
}
var upperProps = {
name:'hifi-home-fishtank-upper-corner',
type: "Box",
dimensions: {
x: 0.2,
y: 0.2,
z: 0.2
},
color: {
red: 0,
green: 255,
blue: 0
},
collisionless: true,
position:bounds.tfl
}
var lowerCorner = Entities.addEntity(lowerProps);
var upperCorner = Entities.addEntity(upperProps);
print('CORNERS :::' + JSON.stringify(upperCorner) )
print('CORNERS :::' + JSON.stringify(lowerCorner) )
}
createFishTank();
createBubbleSystem();
createBubbleSound();
createEntitiesAtCorners();
//createBubbleSound();
Script.scriptEnding.connect(cleanup);
if (CLEANUP === true) {
Script.scriptEnding.connect(cleanup);
}

View file

@ -11,6 +11,8 @@
var connected = false;
var TANK_SEARCH_RADIUS = 5;
var WANT_LOOK_DEBUG_LINE = false;
var WANT_LOOK_DEBUG_SPHERE = true;
var INTERSECT_COLOR = {
red: 255,
@ -50,8 +52,9 @@
lookAttractor: null,
overlayLine: null,
overlayLineDistance: 3,
debugSphere: null,
findFishInTank: function() {
// print('looking for a fish in the tank')
print('looking for a fish in the tank')
var results = Entities.findEntities(_this.currentProperties.position, TANK_SEARCH_RADIUS);
var fishList = [];
@ -109,7 +112,7 @@
unload: function() {
print(' UNLOAD')
Script.update.disconnect(_this.update);
if (WANT_LOOK_DEBUG === true) {
if (WANT_LOOK_DEBUG_LINE === true) {
_this.overlayLineOff();
}
if (baton) {
@ -129,6 +132,43 @@
_this.seeIfOwnerIsLookingAtTheTank();
},
debugSphereOn: function(position) {
if (_this.debugSphere !== null) {
Entities.editEntity(_this.debugSphere, {
visible: true
})
return;
}
var sphereProperties = {
type: 'Sphere',
parentID: _this.entityID,
dimensions: {
x: 0.1,
y: 0.1,
z: 0.1,
},
color: INTERSECT_COLOR,
position: position,
collisionless: true
}
_this.debugSphere = Entities.addEntity(sphereProperties);
},
updateDebugSphere: function(position) {
Entities.editEntity(_this.debugSphere, {
visible:true,
position: position
})
},
debugSphereOff: function() {
// Entities.deleteEntity(_this.debugSphere);
Entities.editEntity(_this.debugSphere, {
visible: false
})
//_this.debugSphere = null;
},
overlayLineOn: function(closePoint, farPoint, color) {
if (_this.overlayLine === null) {
@ -175,33 +215,59 @@
direction: front
};
if (WANT_LOOK_DEBUG === true) {
if (WANT_LOOK_DEBUG_LINE === true) {
_this.overlayLineOn(pickRay.origin, Vec3.sum(pickRay.origin, Vec3.multiply(front, _this.overlayLineDistance)), INTERSECT_COLOR);
};
var intersection = Entities.findRayIntersection(pickRay, true, [_this.entityID]);
if (intersection.intersects && intersection.entityID === _this.entityID) {
print('intersecting a tank')
print('intersection:: ' + JSON.stringify(intersection));
//print('intersecting a tank')
if (WANT_LOOK_DEBUG_SPHERE === true) {
if (_this.debugSphere === null) {
_this.debugSphereOn(intersection.intersection);
} else {
_this.updateDebugSphere(intersection.intersection);
}
}
print('INT DIST: ' + intersection.distance);
if (intersection.distance > 1.5) {
print('NOT CLOSE ENOUGH TO THE TANK')
if (WANT_LOOK_DEBUG_SPHERE === true) {
_this.debugSphereOff();
}
return
}
// print('intersection:: ' + JSON.stringify(intersection));
if (_this.hasLookAttractor === false) {
_this.createLookAttractor();
_this.createLookAttractor(intersection.intersection, intersection.distance);
} else if (_this.hasLookAttractor === true) {
_this.updateLookAttractor();
_this.updateLookAttractor(intersection.intersection, intersection.distance);
}
} else {
if (_this.hasLookAttractor === true) {
clearLookAttractor();
_this.clearLookAttractor();
}
if (WANT_LOOK_DEBUG_SPHERE === true) {
_this.debugSphereOff();
}
}
},
//look attractors could be private to the tank...
createLookAttractor: function(position) {
_this.lookAttractor = position;
createLookAttractor: function(position, distance) {
_this.lookAttractor = {
position: position,
distance: distance
};
_this.hasLookAttractor = true;
},
updateLookAttractor: function(position) {
_this.lookAttractor = position;
updateLookAttractor: function(position, distance) {
_this.lookAttractor = {
position: position,
distance: distance
};
},
clearLookAttractor: function() {
_this.hasLookAttractor = false;
@ -253,30 +319,29 @@
z: 3.5914
};
var TANK_WIDTH = TANK_DIMENSIONS.z / 3;
var TANK_HEIGHT = TANK_DIMENSIONS.y / 3;
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 MAX_SIGHT_DISTANCE = 0.8;
var MAX_SIGHT_DISTANCE = 1.5;
var MIN_SEPARATION = 0.15;
var AVOIDANCE_FORCE = 0.3;
var AVOIDANCE_FORCE = 0.32;
var COHESION_FORCE = 0.025;
var ALIGNMENT_FORCE = 0.025;
var LOOK_ATTRACTOR_FORCE = 0.030;
var LOOK_ATTRACTOR_FORCE = 0.029;
var SWIMMING_FORCE = 0.05;
var SWIMMING_SPEED = 0.5;
var FISH_DAMPING = 0.25;
var WANT_LOOK_DEBUG = false;
var THROTTLE = false;
var THROTTLE_RATE = 100;
var sinceLastUpdate = 0;
var FISH_MODEL_URL = "http://hifi-content.s3.amazonaws.com/DomainContent/Home/fishTank/Fish-1.fbx";
var FISH_MODEL_TWO_URL = "http://hifi-content.s3.amazonaws.com/DomainContent/Home/fishTank/Fish-2.fbx";
// var FISH_MODEL_URL = "http://hifi-content.s3.amazonaws.com/DomainContent/Home/fishTank/Fish-1.fbx";
// var FISH_MODEL_TWO_URL = "http://hifi-content.s3.amazonaws.com/DomainContent/Home/fishTank/Fish-2.fbx";
var FISH_MODEL_URL = "http://hifi-content.s3.amazonaws.com/DomainContent/Home/fishTank/goodfish5.fbx";
var FISH_MODEL_TWO_URL = "http://hifi-content.s3.amazonaws.com/DomainContent/Home/fishTank/goodfish5.fbx";
var fishLoaded = false;
function randomVector(scale) {
@ -287,6 +352,44 @@
};
}
function createEntitiesAtCorners(lower, upper) {
var lowerProps = {
type: "box",
dimensions: {
x: 0.2,
y: 0.2,
z: 0.2
},
color: {
red: 255,
green: 0,
blue: 0
},
collisionless: true,
position: lower
}
var upperProps = {
type: "box",
dimensions: {
x: 0.2,
y: 0.2,
z: 0.2
},
color: {
red: 0,
green: 255,
blue: 0
},
collisionless: true,
position: upper
}
_this.lowerCorner = Entities.addEntity(lowerProps);
_this.upperCorner = Entities.addEntity(upperProps);
}
function updateFish(deltaTime) {
if (_this.tankLocked === true) {
@ -317,7 +420,9 @@
fishLoaded: true
});
_this.userData['hifi-home-fishtank'].fishLoaded = true;
_this.fish = _this.findFishInTank();
Script.setTimeout(function() {
_this.fish = _this.findFishInTank();
}, 2000)
return;
} else {
@ -330,10 +435,10 @@
var fish = _this.fish;
// print('how many fish do i find?' + fish.length)
// print('how many fish do i find?' + fish.length)
if (fish.length === 0) {
print('no fish...')
// print('no fish...')
return
};
@ -362,6 +467,8 @@
z: center.z + (TANK_WIDTH / 2)
};
//createEntitiesAtCorners(lowerCorner,upperCorner);
// First pre-load an array with properties on all the other fish so our per-fish loop
// isn't doing it.
var flockProperties = [];
@ -432,12 +539,15 @@
//attractors
//[position, radius, force]
if (_this.hasLookAttractor === true) {
var attractorPosition = _this.lookAttractor;
var towardAttractor = Vec3.subtract(attractorPosition, position);
velocity = Vec3.mix(velocity, Vec3.multiply(Vec3.normalize(towardAttractor), Vec3.length(velocity)), LOOK_ATTRACTOR_FORCE);
}
}
if (_this.hasLookAttractor === true) {
//print('has a look attractor, so use it')
var attractorPosition = _this.lookAttractor.position;
var towardAttractor = Vec3.subtract(attractorPosition, position);
velocity = Vec3.mix(velocity, Vec3.multiply(Vec3.normalize(towardAttractor), Vec3.length(velocity)), LOOK_ATTRACTOR_FORCE);
}
// Try to swim at a constant speed
@ -470,6 +580,10 @@
var rotation = Quat.rotationBetween(Vec3.UNIT_NEG_Z, velocity);
var VELOCITY_FOLLOW_RATE = 0.30;
var safeEuler = Quat.safeEulerAngles(rotation);
// 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) {
@ -523,12 +637,13 @@
modelURL: fish.length % 2 === 0 ? FISH_MODEL_URL : FISH_MODEL_TWO_URL,
position: position,
parentID: _this.entityID,
rotation: {
x: 0,
y: 0,
z: 0,
w: 1
},
// rotation: {
// x: 0,
// y: 0,
// z: 0,
// w: 1
// },
// type: "Box",
// dimensions: {
// x: FISH_WIDTH,
// y: FISH_WIDTH,