This commit is contained in:
James B. Pollack 2015-11-17 11:33:48 -08:00
parent f669f8ac79
commit 08ca03e0af
2 changed files with 126 additions and 108 deletions

View file

@ -1,39 +1,45 @@
Script.include('steer.js') Script.include('steer.js');
var steer = loadSteer(); var steer = loadSteer();
Script.include('tween.js'); Script.include('../libraries/tween.js');
var TWEEN = loadTween(); var TWEEN = loadTween();
var USE_CONSTANT_SPAWNER = false; var USE_CONSTANT_SPAWNER = true;
var RAT_SPAWNER_LOCATION = { var RAT_SPAWNER_LOCATION = {
x: 0, x: 1001,
y: 0, y: 98.5,
z: 0 z: 1039
}; };
var RAT_NEST_LOCATION = { var RAT_NEST_LOCATION = {
x: 0, x: 1003.5,
y: 0, y: 99,
z: 0 z: 964.2
}; };
var RAT_MODEL_URL = ''; var RAT_DIMENSIONS = {
var RAT_RUNNING_ANIMATION_URL = ''; x: 0.22,
var RAT_DEATH_ANIMATION_URL = ''; y: 0.32,
z: 1.14
};
var RAT_IN_NEST_DISTANCE = 0.25;
var RAT_MODEL_URL = 'http://hifi-content.s3.amazonaws.com/james/rat/ratrotated.fbx';
var RAT_RUNNING_ANIMATION_URL = 'http://hifi-content.s3.amazonaws.com/james/rat/walk.fbx';
var RAT_DEATH_ANIMATION_URL = 'http://hifi-content.s3.amazonaws.com/james/rat/death.fbx';
var RAT_IN_NEST_DISTANCE = 3;
//how many milliseconds between rats //how many milliseconds between rats
var RAT_SPAWN_RATE = 1000; var RAT_SPAWN_RATE = 2500;
function playRatRunningAnimation(rat) {
function playRatRunningAnimation() {
var animationSettings = JSON.stringify({ var animationSettings = JSON.stringify({
running: true running: true
}); });
Entities.editEntity(rat, { Entities.editEntity(rat, {
animationURL: RAT_RUNNING_ANIMATION_URL, animationURL: RAT_RUNNING_ANIMATION_URL,
animationSettings: animationSettings, animationSettings: animationSettings
// animation: { // animation: {
// url: RAT_RUNNING_ANIMATION_URL, // url: RAT_RUNNING_ANIMATION_URL,
// running: true, // running: true,
@ -42,24 +48,23 @@ function playRatRunningAnimation() {
}); });
} }
function playRatDeathAnimation() { function playRatDeathAnimation(rat) {
var animationSettings = JSON.stringify({ var animationSettings = JSON.stringify({
running: true running: true
}); });
Entities.editEntity(rat, { Entities.editEntity(rat, {
animationURL: RAT_DEATH_ANIMATION_URL, animationURL: RAT_DEATH_ANIMATION_URL,
animationSettings: animationSettings, animationSettings: animationSettings
// animation: { // animation: {
// url: RAT_DEATH_ANIMATION_URL, // url: RAT_DEATH_ANIMATION_URL,
// running: true, // running: true
// fps: 180
// }, // },
}); });
} }
var ratProperties = { var ratProperties = {
name: 'Hifi-Rat', name: 'Rat',
type: 'Box', type: 'Box',
color: { color: {
red: 0, red: 0,
@ -71,21 +76,28 @@ var ratProperties = {
y: 1, y: 1,
z: 1 z: 1
}, },
collisionsWillMove: true,
gravity: {
x: 0,
y: -9.8,
z: 0
},
position: RAT_SPAWNER_LOCATION position: RAT_SPAWNER_LOCATION
}; };
var modelRatProperties = { var modelRatProperties = {
name: 'Hifi-Rat', name: 'rat',
type: 'Model', type: 'Model',
modelURL: RAT_MODEL_URL,
dimensions: RAT_DIMENSIONS, dimensions: RAT_DIMENSIONS,
position: RAT_SPAWNER_LOCATION, position: RAT_SPAWNER_LOCATION,
shapeType: 'Box', shapeType: 'Box',
collisionsWillMove: true, collisionsWillMove: true,
gravity: { gravity: {
x: 0, x: 0,
y: -9.8 y: -9.8,
z: 0 z: 0
}, }
//enable this if for some reason we want grabbable rats //enable this if for some reason we want grabbable rats
// userData:JSON.stringify({ // userData:JSON.stringify({
// grabbableKey:{ // grabbableKey:{
@ -112,56 +124,54 @@ var targetProperties = {
// script: Script.resolvePath('rat.js') // script: Script.resolvePath('rat.js')
}; };
var target = Entities.addEntity(targetProperties) var target = Entities.addEntity(targetProperties);
function addRat() { function addRat() {
var rat = Entities.addEntity(ratProperties); var rat = Entities.addEntity(modelRatProperties);
rats.push(rat); rats.push(rat);
} }
var rats = []; var rats = [];
addRat(); addRat();
var AVOIDER_Y_HEIGHT = 99;
var FIRST_AVOIDER_START_POSITION = { var FIRST_AVOIDER_START_POSITION = {
x: 0, x: 1004,
y: 0, y: AVOIDER_Y_HEIGHT,
z: 0 z: 1019
}; };
var FIRST_AVOIDER_FINISH_POSITION = { var FIRST_AVOIDER_FINISH_POSITION = {
x: 0, x: 997,
y: 0, y: AVOIDER_Y_HEIGHT,
z: 0 z: 1019
}; };
var SECOND_AVOIDER_START_POSITION = { var SECOND_AVOIDER_START_POSITION = {
x: 0, x: 998,
y: 0, y: AVOIDER_Y_HEIGHT,
z: 0 z: 998
}; };
var SECOND_AVOIDER_FINISH_POSITION = { var SECOND_AVOIDER_FINISH_POSITION = {
x: 0, x: 1005,
y: 0, y: AVOIDER_Y_HEIGHT,
z: 0 z: 999
}; };
var THIRD_AVOIDER_START_POSITION = { var THIRD_AVOIDER_START_POSITION = {
x: 0, x: 1001.5,
y: 0, y: 100,
z: 0 z: 978
}; };
var THIRD_AVOIDER_FINISH_POSITION = { var THIRD_AVOIDER_FINISH_POSITION = {
x: 0, x: 1005,
y: 0, y: 100,
z: 0 z: 974
}; };
var avoiders = [ var avoiders = [];
addAvoiderBlock(FIRST_AVOIDER_START_POSITION), addAvoiderBlock(FIRST_AVOIDER_START_POSITION);
addAvoiderBlock(SECOND_AVOIDER_START_POSITION), addAvoiderBlock(SECOND_AVOIDER_START_POSITION);
addAvoiderBlock(THIRD_AVOIDER_START_POSITION) addAvoiderBlock(THIRD_AVOIDER_START_POSITION);
];
function addAvoiderBlock(position) { function addAvoiderBlock(position) {
var avoiderProperties = { var avoiderProperties = {
name: 'Hifi-Rat-Avoider', name: 'Hifi-Rat-Avoider',
type: 'Box', type: 'Box',
@ -175,24 +185,23 @@ function addAvoiderBlock(position) {
y: 1, y: 1,
z: 1 z: 1
}, },
position: { position: position,
x: 1,
y: 1,
z: 1
},
collisionsWillMove: false, collisionsWillMove: false,
ignoreForCollisions: true ignoreForCollisions: true,
} visible:false
};
var avoider = Entities.addEntity(avoiderProperties); var avoider = Entities.addEntity(avoiderProperties);
avoiders.push(avoider); avoiders.push(avoider);
}; }
addAvoiderBlock();
tweenAvoider(avoiders[0]); tweenAvoider(avoiders[0], FIRST_AVOIDER_START_POSITION, FIRST_AVOIDER_FINISH_POSITION);
tweenAvoider(avoiders[1], SECOND_AVOIDER_START_POSITION, SECOND_AVOIDER_FINISH_POSITION);
tweenAvoider(avoiders[2], THIRD_AVOIDER_START_POSITION, THIRD_AVOIDER_FINISH_POSITION);
function tweenAvoider(entityID, startPosition, endPosition) { function tweenAvoider(entityID, startPosition, endPosition) {
var ANIMATION_DURATION = 500; var ANIMATION_DURATION = 4200;
var begin = { var begin = {
x: startPosition.x, x: startPosition.x,
@ -200,11 +209,11 @@ function tweenAvoider(entityID, startPosition, endPosition) {
z: startPosition.z z: startPosition.z
}; };
var target = endPosition; var end = endPosition;
var original = startPosition; var original = startPosition;
var tweenHead = new TWEEN.Tween(begin).to(target, ANIMATION_DURATION); var tweenHead = new TWEEN.Tween(begin).to(end, ANIMATION_DURATION);
function updateTo() { function updateTo() {
Entities.editEntity(entityID, { Entities.editEntity(entityID, {
@ -213,8 +222,8 @@ function tweenAvoider(entityID, startPosition, endPosition) {
y: begin.y, y: begin.y,
z: begin.z z: begin.z
} }
}) });
}; }
function updateBack() { function updateBack() {
Entities.editEntity(entityID, { Entities.editEntity(entityID, {
@ -224,7 +233,7 @@ function tweenAvoider(entityID, startPosition, endPosition) {
z: begin.z z: begin.z
} }
}) })
}; }
var tweenBack = new TWEEN.Tween(begin).to(original, ANIMATION_DURATION).onUpdate(updateBack); var tweenBack = new TWEEN.Tween(begin).to(original, ANIMATION_DURATION).onUpdate(updateBack);
@ -244,10 +253,10 @@ function updateTweens() {
function moveRats() { function moveRats() {
rats.forEach(function(rat) { rats.forEach(function(rat) {
checkDistanceFromNest(rat);
// print('debug1') // print('debug1')
var avatarFlightVectors = steer.fleeAllAvatars(rat); var avatarFlightVectors = steer.fleeAllAvatars(rat);
print('avatarFlightVectors' + avatarFlightVectors) // print('avatarFlightVectors' + avatarFlightVectors)
var i, j; var i, j;
var averageAvatarFlight; var averageAvatarFlight;
@ -259,7 +268,7 @@ function moveRats() {
} }
} }
averageAvatarFlight = Vec3.normalize(averageAvatarFlight); // averageAvatarFlight = Vec3.normalize(averageAvatarFlight);
averageAvatarFlight = Vec3.multiply(averageAvatarFlight, 1 / avatarFlightVectors.length); averageAvatarFlight = Vec3.multiply(averageAvatarFlight, 1 / avatarFlightVectors.length);
@ -276,7 +285,7 @@ function moveRats() {
} }
}; };
avarageAvoiderFlight = Vec3.normalize(averageAvoiderFlight); // avarageAvoiderFlight = Vec3.normalize(averageAvoiderFlight);
averageAvoiderFlight = Vec3.multiply(averageAvoiderFlight, 1 / avoidBlockVectors.length); averageAvoiderFlight = Vec3.multiply(averageAvoiderFlight, 1 / avoidBlockVectors.length);
@ -288,19 +297,24 @@ function moveRats() {
divisorCount++; divisorCount++;
averageVector = Vec3.sum(averageVector, averageAvatarFlight); averageVector = Vec3.sum(averageVector, averageAvatarFlight);
} }
if (avoidBlockVectors > 0) { if (avoidBlockVectors.length > 0) {
divisorCount++; divisorCount++;
averageVector = Vec3.sum(averageVector, averageAvoiderFlight); averageVector = Vec3.sum(averageVector, averageAvoiderFlight);
} }
averageVector = Vec3.multiply(averageVector, 1 / divisorCount); averageVector = Vec3.multiply(averageVector, 1 / divisorCount);
var thisRatProps = Entities.getEntityProperties(rat, ["position", "rotation"]);
var ratPosition = thisRatProps.position;
var ratToNest = Vec3.subtract(RAT_NEST_LOCATION, ratPosition);
var ratRotation = Quat.rotationBetween(Vec3.FRONT, ratToNest);
Entities.editEntity(rat, { Entities.editEntity(rat, {
velocity: averageVector velocity: averageVector,
rotation: ratRotation,
//rotation: Quat.fromPitchYawRollDegrees(0,0,0)
}) })
// castRay(rat); // castRay(rat);
}) })
} }
@ -308,10 +322,14 @@ Script.update.connect(moveRats)
Script.update.connect(updateTweens); Script.update.connect(updateTweens);
function checkDistanceFromNest(rat) { function checkDistanceFromNest(rat) {
var ratProps = Entitis.getEntityProperties(rat, "position"); var ratProps = Entities.getEntityProperties(rat, "position");
var distance = Vec3.distance(ratProps.position, RAT_NEST_LOCATION); var distance = Vec3.distance(ratProps.position, RAT_NEST_LOCATION);
if (distance < RAT_IN_NEST_DISTANCE) { if (distance < RAT_IN_NEST_DISTANCE) {
removeRatFromScene(); //print('at nest')
removeRatFromScene(rat);
} else {
// print('not yet at nest:::' + distance)
} }
} }
@ -323,8 +341,6 @@ function removeRatFromScene(rat) {
Entities.deleteEntity(rat); Entities.deleteEntity(rat);
} }
function cleanup() { function cleanup() {
while (rats.length > 0) { while (rats.length > 0) {
Entities.deleteEntity(rats.pop()); Entities.deleteEntity(rats.pop());
@ -340,14 +356,16 @@ function cleanup() {
Script.clearInterval(ratSpawnerInterval); Script.clearInterval(ratSpawnerInterval);
} }
Script.scriptEnding.connect(cleanup) Script.scriptEnding.connect(cleanup);
var ratSpawnerInterval; var ratSpawnerInterval;
if (USE_CONSTANT_SPAWNER === true) { if (USE_CONSTANT_SPAWNER === true) {
ratSpawnerInterval = Script.setInterval(function() { ratSpawnerInterval = Script.setInterval(function() {
addRat(); addRat();
}, RAT_SPAWN_RATE) // playRatRunningAnimation(rat);
rats.push(rat);
}, RAT_SPAWN_RATE);
} }
//unused for now, to be used for some collision avoidance on walls and stuff? //unused for now, to be used for some collision avoidance on walls and stuff?

View file

@ -3,8 +3,8 @@ function seek(thisEntity, target) {
var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]); var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]);
var location = properties.position; var location = properties.position;
var velocity = properties.velocity; var velocity = properties.velocity;
var MAX_SPEED = 1; var MAX_SPEED = 30;
var MAX_FORCE = 1; var MAX_FORCE = 20;
var desired = Vec3.subtract(targetPosition, location); var desired = Vec3.subtract(targetPosition, location);
desired = Vec3.normalize(desired); desired = Vec3.normalize(desired);
@ -48,8 +48,8 @@ function fleeAvatar(thisEntity, avatarPosition) {
var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]); var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]);
var location = properties.position; var location = properties.position;
var velocity = properties.velocity; var velocity = properties.velocity;
var MAX_SPEED = 2; var MAX_SPEED = 3;
var MAX_FORCE = 2; var MAX_FORCE = 10;
var desired = Vec3.subtract(location, avatarPosition); var desired = Vec3.subtract(location, avatarPosition);
var d = Vec3.length(desired); var d = Vec3.length(desired);
@ -69,7 +69,7 @@ function fleeAvatar(thisEntity, avatarPosition) {
} }
function fleeAllAvatars(thisEntity) { function fleeAllAvatars(thisEntity) {
print('FLEE AVATARS'); //print('FLEE AVATARS');
var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]); var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]);
var location = properties.position; var location = properties.position;
var velocity = properties.velocity; var velocity = properties.velocity;
@ -80,10 +80,10 @@ function fleeAllAvatars(thisEntity) {
var entityID = nearbyEntities[entityIndex]; var entityID = nearbyEntities[entityIndex];
var entityProps = Entities.getEntityProperties(entityID); var entityProps = Entities.getEntityProperties(entityID);
if (entityProps.name === 'Hifi-Avatar-Detector') { if (entityProps.name === 'Hifi-Avatar-Detector') {
print('found an avatar to flee') //print('found an avatar to flee')
var MAX_SPEED = 2; var MAX_SPEED = 8;
var MAX_FORCE = 2; var MAX_FORCE = 8;
var desired = Vec3.subtract(location, entityProps.position); var desired = Vec3.subtract(location, entityProps.position);
var d = Vec3.length(desired); var d = Vec3.length(desired);
@ -95,7 +95,7 @@ function fleeAllAvatars(thisEntity) {
steer = steerVector.limit(MAX_FORCE) steer = steerVector.limit(MAX_FORCE)
flightVectors.push(steer) flightVectors.push(steer)
} else { } else {
print('target too far away from this avatar to flee' + d); // print('target too far away from this avatar to flee' + d);
} }
} }
@ -105,33 +105,33 @@ function fleeAllAvatars(thisEntity) {
} }
function fleeAvoiderBlocks(thisEntity) { function fleeAvoiderBlocks(thisEntity) {
print('FLEE AVOIDER BLOCKS'); // print('FLEE AVOIDER BLOCKS');
var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]); var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]);
var location = properties.position; var location = properties.position;
var velocity = properties.velocity; var velocity = properties.velocity;
var nearbyEntities = Entities.findEntities(location, 3); var nearbyEntities = Entities.findEntities(location, 4);
var flightVectors = []; var flightVectors = [];
for (var entityIndex = 0; entityIndex < nearbyEntities.length; entityIndex++) { for (var entityIndex = 0; entityIndex < nearbyEntities.length; entityIndex++) {
var entityID = nearbyEntities[entityIndex]; var entityID = nearbyEntities[entityIndex];
var entityProps = Entities.getEntityProperties(entityID); var entityProps = Entities.getEntityProperties(entityID);
if (entityProps.name === 'Hifi-Rat-Avoider') { if (entityProps.name === 'Hifi-Rat-Avoider') {
print('found an avoiderblock to flee'); // print('found an avoiderblock to flee');
var MAX_SPEED = 2; var MAX_SPEED = 8;
var MAX_FORCE = 0.8; var MAX_FORCE = 6;
var desired = Vec3.subtract(location, entityProps.position); var desired = Vec3.subtract(location, entityProps.position);
var d = Vec3.length(desired); var d = Vec3.length(desired);
desired = Vec3.normalize(desired); desired = Vec3.normalize(desired);
desired = Vec3.multiply(MAX_SPEED, desired); desired = Vec3.multiply(MAX_SPEED, desired);
if (d < 3) { if (d < 5) {
var steer = Vec3.subtract(desired, velocity); var steer = Vec3.subtract(desired, velocity);
var steerVector = new V3(desired.x, desired.y, desired.z); var steerVector = new V3(desired.x, desired.y, desired.z);
steer = steerVector.limit(MAX_FORCE) steer = steerVector.limit(MAX_FORCE)
flightVectors.push(steer) flightVectors.push(steer);
} else { } else {
print('target too far away from this avoider to flee' + d); //print('target too far away from this avoider to flee' + d);
} }
} }
@ -146,8 +146,8 @@ function arrive(thisEntity, target) {
var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]); var properties = Entities.getEntityProperties(thisEntity, ["position", "velocity"]);
var location = properties.position; var location = properties.position;
var velocity = properties.velocity; var velocity = properties.velocity;
var MAX_SPEED = 3; var MAX_SPEED = 16;
var MAX_FORCE = 2; var MAX_FORCE = 12;
var ARRIVAL_DISTANCE = 3; var ARRIVAL_DISTANCE = 3;
var desired = Vec3.subtract(targetPosition, location); var desired = Vec3.subtract(targetPosition, location);