tweaks to butterflies, animation, plus reformatting to match coding standard

This commit is contained in:
ZappoMan 2014-09-04 11:40:32 -07:00
parent 957991b67e
commit ac45a56bf9

View file

@ -1,230 +1,241 @@
// //
// butterflyFlockTest1.js // butterflyFlockTest1.js
// //
// //
// Created by Adrian McCarlie on August 2, 2014 // Created by Adrian McCarlie on August 2, 2014
// Modified by Brad Hefta-Gaub to use Entities on Sept. 3, 2014 // Modified by Brad Hefta-Gaub to use Entities on Sept. 3, 2014
// Copyright 2014 High Fidelity, Inc. // Copyright 2014 High Fidelity, Inc.
// //
// This sample script creates a swarm of butterfly entities that fly around the avatar. // This sample script creates a swarm of butterfly entities that fly around the avatar.
// //
// Distributed under the Apache License, Version 2.0. // Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
function getRandomFloat(min, max) { function getRandomFloat(min, max) {
return Math.random() * (max - min) + min; return Math.random() * (max - min) + min;
} }
// Multiply vector by scalar // Multiply vector by scalar
function vScalarMult(v, s) { function vScalarMult(v, s) {
var rval = { x: v.x * s, y: v.y * s, z: v.z * s }; var rval = { x: v.x * s, y: v.y * s, z: v.z * s };
return rval; return rval;
} }
function printVector(v) { function printVector(v) {
print(v.x + ", " + v.y + ", " + v.z + "\n"); print(v.x + ", " + v.y + ", " + v.z + "\n");
} }
// Create a random vector with individual lengths between a,b // Create a random vector with individual lengths between a,b
function randVector(a, b) { function randVector(a, b) {
var rval = { x: a + Math.random() * (b - a), y: a + Math.random() * (b - a), z: a + Math.random() * (b - a) }; var rval = { x: a + Math.random() * (b - a), y: a + Math.random() * (b - a), z: a + Math.random() * (b - a) };
return rval; return rval;
} }
// Returns a vector which is fraction of the way between a and b // Returns a vector which is fraction of the way between a and b
function vInterpolate(a, b, fraction) { function vInterpolate(a, b, fraction) {
var rval = { x: a.x + (b.x - a.x) * fraction, y: a.y + (b.y - a.y) * fraction, z: a.z + (b.z - a.z) * fraction }; var rval = { x: a.x + (b.x - a.x) * fraction, y: a.y + (b.y - a.y) * fraction, z: a.z + (b.z - a.z) * fraction };
return rval; return rval;
} }
var startTimeInSeconds = new Date().getTime() / 1000; var startTimeInSeconds = new Date().getTime() / 1000;
var lifeTime = 60; // lifetime of the butterflies in seconds! var lifeTime = 60; // lifetime of the butterflies in seconds!
var range = 1.0; // Over what distance in meters do you want the flock to fly around var range = 1.0; // Over what distance in meters do you want the flock to fly around
var frame = 0; var frame = 0;
var CHANCE_OF_MOVING = 0.9; var CHANCE_OF_MOVING = 0.9;
var BUTTERFLY_GRAVITY = 0;//-0.06; var BUTTERFLY_GRAVITY = 0;//-0.06;
var BUTTERFLY_FLAP_SPEED = 1.0; var BUTTERFLY_FLAP_SPEED = 1.0;
var BUTTERFLY_VELOCITY = 0.55; var BUTTERFLY_VELOCITY = 0.55;
var myPosition = MyAvatar.position; var DISTANCE_IN_FRONT_OF_ME = 1.5;
var DISTANCE_ABOVE_ME = 1.5;
var pitch = 0.0;//experimental var flockPosition = Vec3.sum(MyAvatar.position,Vec3.sum(
var yaw = 0.0;//experimental Vec3.multiply(Quat.getFront(MyAvatar.orientation), DISTANCE_ABOVE_ME),
var roll = 0.0; //experimental Vec3.multiply(Quat.getFront(MyAvatar.orientation), DISTANCE_IN_FRONT_OF_ME)));
var rotation = Quat.fromPitchYawRollDegrees(pitch, yaw, roll);//experimental
// This is our butterfly object // set these pitch, yaw, roll to the needed values to orient the model as you want it
function defineButterfly(entityID, targetPosition) { var pitchInDegrees = 270.0;
this.entityID = entityID; var yawInDegrees = 0.0;
this.previousFlapOffset = 0; var rollInDegrees = 0.0;
this.targetPosition = targetPosition; var pitchInRadians = pitchInDegrees / 180.0 * Math.PI;
this.moving = false; var yawInRadians = yawInDegrees / 180.0 * Math.PI;
} var rollInRadians = rollInDegrees / 180.0 * Math.PI;
// Array of butterflies var rotation = Quat.fromPitchYawRollDegrees(pitchInDegrees, yawInDegrees, rollInDegrees);//experimental
var butterflies = [];
var numButterflies = 20; // This is our butterfly object
function addButterfly() { function defineButterfly(entityID, targetPosition) {
// Decide the size of butterfly this.entityID = entityID;
var color = { red: 100, green: 100, blue: 100 }; this.previousFlapOffset = 0;
var size = 0; this.targetPosition = targetPosition;
this.moving = false;
var which = Math.random(); }
if (which < 0.2) {
size = 0.08; // Array of butterflies
} else if (which < 0.4) { var butterflies = [];
size = 0.09; var numButterflies = 20;
} else if (which < 0.6) { function addButterfly() {
size = 0.8; // Decide the size of butterfly
} else if (which < 0.8) { var color = { red: 100, green: 100, blue: 100 };
size = 0.8; var size = 0;
} else {
size = 0.8; var which = Math.random();
} if (which < 0.2) {
size = 0.08;
myPosition = MyAvatar.position; } else if (which < 0.4) {
// if ( frame < numButterflies){ size = 0.09;
// myPosition = {x: myPosition.x, y: myPosition.y, z: myPosition.z }; } else if (which < 0.6) {
// } size = 0.8;
} else if (which < 0.8) {
var properties = { size = 0.8;
type: "Model", } else {
lifetime: lifeTime, size = 0.8;
position: Vec3.sum(randVector(-range, range), myPosition), }
velocity: { x: 0, y: 0.0, z: 0 },
gravity: { x: 0, y: 1.0, z: 0 }, flockPosition = Vec3.sum(MyAvatar.position,Vec3.sum(
damping: 0.1, Vec3.multiply(Quat.getFront(MyAvatar.orientation), DISTANCE_ABOVE_ME),
radius : size, Vec3.multiply(Quat.getFront(MyAvatar.orientation), DISTANCE_IN_FRONT_OF_ME)));
color: color,
rotation: rotation, var properties = {
//animationURL: "http://business.ozblog.me/objects/butterfly/newButterfly6.fbx", type: "Model",
//animationIsPlaying: true, lifetime: lifeTime,
modelURL: "http://business.ozblog.me/objects/butterfly/newButterfly6.fbx" position: Vec3.sum(randVector(-range, range), flockPosition),
}; velocity: { x: 0, y: 0.0, z: 0 },
properties.position.z = properties.position.z+1; gravity: { x: 0, y: 1.0, z: 0 },
butterflies.push(new defineButterfly(Entities.addEntity(properties), properties.position)); damping: 0.1,
} radius : size,
color: color,
// Generate the butterflies rotation: rotation,
for (var i = 0; i < numButterflies; i++) { animationURL: "http://business.ozblog.me/objects/butterfly/newButterfly2.fbx",
addButterfly(); animationIsPlaying: true,
} modelURL: "http://business.ozblog.me/objects/butterfly/newButterfly2.fbx"
};
// Main update function properties.position.z = properties.position.z+1;
function updateButterflies(deltaTime) { butterflies.push(new defineButterfly(Entities.addEntity(properties), properties.position));
// Check to see if we've been running long enough that our butterflies are dead }
var nowTimeInSeconds = new Date().getTime() / 1000;
if ((nowTimeInSeconds - startTimeInSeconds) >= lifeTime) { // Generate the butterflies
// print("our butterflies are dying, stop our script"); for (var i = 0; i < numButterflies; i++) {
Script.stop(); addButterfly();
return; }
}
// Main update function
frame++; function updateButterflies(deltaTime) {
// Only update every third frame // Check to see if we've been running long enough that our butterflies are dead
if ((frame % 3) == 0) { var nowTimeInSeconds = new Date().getTime() / 1000;
myPosition = MyAvatar.position; if ((nowTimeInSeconds - startTimeInSeconds) >= lifeTime) {
// print("our butterflies are dying, stop our script");
// Update all the butterflies Script.stop();
for (var i = 0; i < numButterflies; i++) { return;
entityID = butterflies[i].entityID; }
var properties = Entities.getEntityProperties(entityID);
frame++;
if (properties.position.y > myPosition.y + getRandomFloat(0.0,0.3)){ //0.3 //ceiling // Only update every third frame
properties.gravity.y = - 3.0; if ((frame % 3) == 0) {
properties.damping.y = 1.0; flockPosition = Vec3.sum(MyAvatar.position,Vec3.sum(
properties.velocity.y = 0; Vec3.multiply(Quat.getFront(MyAvatar.orientation), DISTANCE_ABOVE_ME),
properties.velocity.x = properties.velocity.x; Vec3.multiply(Quat.getFront(MyAvatar.orientation), DISTANCE_IN_FRONT_OF_ME)));
properties.velocity.z = properties.velocity.z;
if (properties.velocity.x < 0.5){ // Update all the butterflies
butterflies[i].moving = false; for (var i = 0; i < numButterflies; i++) {
} entityID = butterflies[i].entityID;
if (properties.velocity.z < 0.5){ var properties = Entities.getEntityProperties(entityID);
butterflies[i].moving = false;
} if (properties.position.y > flockPosition.y + getRandomFloat(0.0,0.3)){ //0.3 //ceiling
} properties.gravity.y = - 3.0;
properties.damping.y = 1.0;
if (properties.velocity.y <= -0.2) { properties.velocity.y = 0;
properties.velocity.y = 0.22; properties.velocity.x = properties.velocity.x;
properties.velocity.x = properties.velocity.x; properties.velocity.z = properties.velocity.z;
properties.velocity.z = properties.velocity.z; if (properties.velocity.x < 0.5){
} butterflies[i].moving = false;
}
if (properties.position.y < myPosition.y - getRandomFloat(0.0,0.3)) { //-0.3 // floor if (properties.velocity.z < 0.5){
properties.velocity.y = 0.9; butterflies[i].moving = false;
properties.gravity.y = - 4.0; }
properties.velocity.x = properties.velocity.x; }
properties.velocity.z = properties.velocity.z;
if (properties.velocity.x < 0.5){ if (properties.velocity.y <= -0.2) {
butterflies[i].moving = false; properties.velocity.y = 0.22;
} properties.velocity.x = properties.velocity.x;
if (properties.velocity.z < 0.5){ properties.velocity.z = properties.velocity.z;
butterflies[i].moving = false; }
}
} if (properties.position.y < flockPosition.y - getRandomFloat(0.0,0.3)) { //-0.3 // floor
properties.velocity.y = 0.9;
properties.gravity.y = - 4.0;
// Begin movement by getting a target properties.velocity.x = properties.velocity.x;
if (butterflies[i].moving == false) { properties.velocity.z = properties.velocity.z;
if (Math.random() < CHANCE_OF_MOVING) { if (properties.velocity.x < 0.5){
var targetPosition = Vec3.sum(randVector(-range, range), myPosition); butterflies[i].moving = false;
if (targetPosition.x < 0) { }
targetPosition.x = 0; if (properties.velocity.z < 0.5){
} butterflies[i].moving = false;
if (targetPosition.y < 0) { }
targetPosition.y = 0; }
}
if (targetPosition.z < 0) {
targetPosition.z = 0; // Begin movement by getting a target
} if (butterflies[i].moving == false) {
if (targetPosition.x > TREE_SCALE) { if (Math.random() < CHANCE_OF_MOVING) {
targetPosition.x = TREE_SCALE; var targetPosition = Vec3.sum(randVector(-range, range), flockPosition);
} if (targetPosition.x < 0) {
if (targetPosition.y > TREE_SCALE) { targetPosition.x = 0;
targetPosition.y = TREE_SCALE; }
} if (targetPosition.y < 0) {
if (targetPosition.z > TREE_SCALE) { targetPosition.y = 0;
targetPosition.z = TREE_SCALE; }
} if (targetPosition.z < 0) {
butterflies[i].targetPosition = targetPosition; targetPosition.z = 0;
butterflies[i].moving = true; }
} if (targetPosition.x > TREE_SCALE) {
} targetPosition.x = TREE_SCALE;
}
// If we are moving, move towards the target if (targetPosition.y > TREE_SCALE) {
if (butterflies[i].moving) { targetPosition.y = TREE_SCALE;
}
var holding = properties.velocity.y; if (targetPosition.z > TREE_SCALE) {
targetPosition.z = TREE_SCALE;
var desiredVelocity = Vec3.subtract(butterflies[i].targetPosition, properties.position); }
desiredVelocity = vScalarMult(Vec3.normalize(desiredVelocity), BUTTERFLY_VELOCITY); butterflies[i].targetPosition = targetPosition;
butterflies[i].moving = true;
properties.velocity = vInterpolate(properties.velocity, desiredVelocity, 0.2); }
properties.velocity.y = holding ; }
// If we are moving, move towards the target
// If we are near the target, we should get a new target if (butterflies[i].moving) {
if (Vec3.length(Vec3.subtract(properties.position, butterflies[i].targetPosition)) < (properties.radius / 1.0)) {
butterflies[i].moving = false; var holding = properties.velocity.y;
}
var desiredVelocity = Vec3.subtract(butterflies[i].targetPosition, properties.position);
var yawRads = Math.atan2(properties.velocity.z, properties.velocity.x); desiredVelocity = vScalarMult(Vec3.normalize(desiredVelocity), BUTTERFLY_VELOCITY);
yawRads = yawRads + Math.PI / 2.0;
var newOrientation = Quat.fromPitchYawRollRadians(0.0, yawRads, 0.0); properties.velocity = vInterpolate(properties.velocity, desiredVelocity, 0.2);
properties.rotation = newOrientation; properties.velocity.y = holding ;
}
// Use a cosine wave offset to make it look like its flapping. // If we are near the target, we should get a new target
var offset = Math.cos(nowTimeInSeconds * BUTTERFLY_FLAP_SPEED) * (properties.radius); if (Vec3.length(Vec3.subtract(properties.position, butterflies[i].targetPosition)) < (properties.radius / 1.0)) {
properties.position.y = properties.position.y + (offset - butterflies[i].previousFlapOffset); butterflies[i].moving = false;
// Change position relative to previous offset. }
butterflies[i].previousFlapOffset = offset;
Entities.editEntity(entityID, properties); var yawRads = Math.atan2(properties.velocity.z, properties.velocity.x);
} yawRads = yawRads + Math.PI / 2.0;
} var newOrientation = Quat.fromPitchYawRollRadians(pitchInRadians, yawRads, rollInRadians);
} properties.rotation = newOrientation;
}
// register the call back so it fires before each data send
// Use a cosine wave offset to make it look like its flapping.
var offset = Math.cos(nowTimeInSeconds * BUTTERFLY_FLAP_SPEED) * (properties.radius);
properties.position.y = properties.position.y + (offset - butterflies[i].previousFlapOffset);
// Change position relative to previous offset.
butterflies[i].previousFlapOffset = offset;
Entities.editEntity(entityID, properties);
}
}
}
// register the call back so it fires before each data send
Script.update.connect(updateButterflies); Script.update.connect(updateButterflies);