Merge pull request #7287 from PhilipRosedale/fireflies

Fireflies
This commit is contained in:
Seth Alves 2016-03-09 15:20:27 -08:00
commit fbbc27213d
2 changed files with 183 additions and 0 deletions

View file

@ -0,0 +1,106 @@
//
// Created by Philip Rosedale on March 7, 2016
// Copyright 2016 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
//
// A firefly which is animated by passerbys. It's physical, no gravity, periodic forces applied.
// If a firefly is found to
//
(function () {
var entityID,
timeoutID = null,
properties,
shouldSimulate = false,
ACTIVE_CHECK_INTERVAL = 100, // milliseconds
INACTIVE_CHECK_INTERVAL = 1000, // milliseconds
MAX_DISTANCE_TO_SIMULATE = 20, // meters
LIGHT_LIFETIME = 1400, // milliseconds a firefly light will stay alive
BUMP_SPEED = 1.5, // average velocity given by a bump
BUMP_CHANCE = 0.33,
MIN_SPEED = 0.125, // below this speed, firefly gets a new bump
SPIN_SPEED = 3.5,
BRIGHTNESS = 0.25,
wantDebug = false
function randomVector(size) {
return { x: (Math.random() - 0.5) * size,
y: (Math.random() - 0.5) * size,
z: (Math.random() - 0.5) * size };
}
function printDebug(message) {
if (wantDebug) {
print(message);
}
}
function maybe() {
properties = Entities.getEntityProperties(entityID);
var speed = Vec3.length(properties.velocity);
var distance = Vec3.distance(MyAvatar.position, properties.position);
printDebug("maybe: speed: " + speed + ", distance: " + distance);
if (shouldSimulate) {
// We are simulating this firefly, so do stuff:
if (distance > MAX_DISTANCE_TO_SIMULATE) {
shouldSimulate = false;
} else if ((speed < MIN_SPEED) && (Math.random() < BUMP_CHANCE)) {
bump();
makeLight();
}
} else if (Vec3.length(properties.velocity) == 0.0) {
// We've found a firefly that is not being simulated, so maybe take it over
if (distance < MAX_DISTANCE_TO_SIMULATE) {
shouldSimulate = true;
}
}
timeoutID = Script.setTimeout(maybe, (shouldSimulate == true) ? ACTIVE_CHECK_INTERVAL : INACTIVE_CHECK_INTERVAL);
}
function bump() {
// Give the firefly a little brownian hop
printDebug("bump!");
var velocity = randomVector(BUMP_SPEED);
if (velocity.y < 0.0) { velocity.y *= -1.0 };
Entities.editEntity(entityID, { velocity: velocity,
angularVelocity: randomVector(SPIN_SPEED) });
}
function makeLight() {
printDebug("make light!");
// create a light attached to the firefly that lives for a while
Entities.addEntity({
type: "Light",
name: "firefly light",
intensity: 4.0 * BRIGHTNESS,
falloffRadius: 8.0 * BRIGHTNESS,
dimensions: {
x: 30 * BRIGHTNESS,
y: 30 * BRIGHTNESS,
z: 30 * BRIGHTNESS
},
position: Vec3.sum(properties.position, { x: 0, y: 0.2, z: 0 }),
parentID: entityID,
color: {
red: 150 + Math.random() * 100,
green: 100 + Math.random() * 50,
blue: 150 + Math.random() * 100
},
lifetime: LIGHT_LIFETIME / 1000
});
}
this.preload = function (givenEntityID) {
printDebug("preload firefly...");
entityID = givenEntityID;
timeoutID = Script.setTimeout(maybe, ACTIVE_CHECK_INTERVAL);
};
this.unload = function () {
printDebug("unload firefly...");
if (timeoutID !== undefined) {
Script.clearTimeout(timeoutID);
}
};
})

View file

@ -0,0 +1,77 @@
//
// Created by Philip Rosedale on March 7, 2016
// Copyright 2016 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
//
// Make some fireflies
//
var SIZE = 0.05;
//var ENTITY_URL = "file:///c:/users/dev/philip/examples/fireflies/firefly.js?"+Math.random()
var ENTITY_URL = "https://s3.amazonaws.com/hifi-public/scripts/fireflies/firefly.js"
var RATE_PER_SECOND = 50; // The entity server will drop data if we create things too fast.
var SCRIPT_INTERVAL = 100;
var LIFETIME = 120;
var NUMBER_TO_CREATE = 100;
var GRAVITY = { x: 0, y: -1.0, z: 0 };
var DAMPING = 0.5;
var ANGULAR_DAMPING = 0.5;
var collidable = true;
var gravity = true;
var RANGE = 10;
var HEIGHT = 3;
var HOW_FAR_IN_FRONT_OF_ME = 1.0;
var totalCreated = 0;
var center = Vec3.sum(MyAvatar.position, Vec3.multiply(HOW_FAR_IN_FRONT_OF_ME, Quat.getFront(Camera.orientation)));
function randomVector(range) {
return {
x: (Math.random() - 0.5) * range.x,
y: (Math.random() - 0.5) * range.y,
z: (Math.random() - 0.5) * range.z
}
}
Vec3.print("Center: ", center);
Script.setInterval(function () {
if (!Entities.serversExist() || !Entities.canRez() || (totalCreated > NUMBER_TO_CREATE)) {
return;
}
var numToCreate = RATE_PER_SECOND * (SCRIPT_INTERVAL / 1000.0);
for (var i = 0; (i < numToCreate) && (totalCreated < NUMBER_TO_CREATE); i++) {
var position = Vec3.sum(center, randomVector({ x: RANGE, y: HEIGHT, z: RANGE }));
position.y += HEIGHT / 2.0;
Entities.addEntity({
type: "Box",
name: "firefly",
position: position,
dimensions: { x: SIZE, y: SIZE, z: SIZE },
color: { red: 150 + Math.random() * 100, green: 100 + Math.random() * 50, blue: 0 },
damping: DAMPING,
angularDamping: ANGULAR_DAMPING,
gravity: (gravity ? GRAVITY : { x: 0, y: 0, z: 0}),
dynamic: collidable,
script: ENTITY_URL,
lifetime: LIFETIME
});
totalCreated++;
print("Firefly #" + totalCreated);
}
}, SCRIPT_INTERVAL);