mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 12:08:54 +02:00
Fixed and tested particle birds
This commit is contained in:
parent
9e882957c4
commit
e669f1d835
1 changed files with 32 additions and 56 deletions
|
@ -11,23 +11,12 @@
|
||||||
// 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
|
||||||
//
|
//
|
||||||
|
|
||||||
// Normalizes a vector to unit length
|
|
||||||
function vNormalize(v) {
|
|
||||||
var length = vLength(v);
|
|
||||||
var rval = { x: v.x / length, y: v.y / length, z: v.z / length };
|
|
||||||
return rval;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 vLength(v) {
|
|
||||||
return Math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
|
|
||||||
}
|
|
||||||
|
|
||||||
function printVector(v) {
|
function printVector(v) {
|
||||||
print(v.x + ", " + v.y + ", " + v.z + "\n");
|
print(v.x + ", " + v.y + ", " + v.z + "\n");
|
||||||
}
|
}
|
||||||
|
@ -37,23 +26,6 @@ function randVector(a, b) {
|
||||||
return rval;
|
return rval;
|
||||||
}
|
}
|
||||||
|
|
||||||
function vMinus(a, b) {
|
|
||||||
var rval = { x: a.x - b.x, y: a.y - b.y, z: a.z - b.z };
|
|
||||||
return rval;
|
|
||||||
}
|
|
||||||
|
|
||||||
function vPlus(a, b) {
|
|
||||||
var rval = { x: a.x + b.x, y: a.y + b.y, z: a.z + b.z };
|
|
||||||
return rval;
|
|
||||||
}
|
|
||||||
|
|
||||||
function vCopy(a, b) {
|
|
||||||
a.x = b.x;
|
|
||||||
a.y = b.y;
|
|
||||||
a.z = b.z;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 };
|
||||||
|
@ -65,8 +37,6 @@ var startTimeInSeconds = new Date().getTime() / 1000;
|
||||||
var birdLifetime = 20; // lifetime of the birds in seconds!
|
var birdLifetime = 20; // lifetime of the birds 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 moving = false;
|
|
||||||
var tweeting = 0;
|
|
||||||
|
|
||||||
var CHANCE_OF_MOVING = 0.1;
|
var CHANCE_OF_MOVING = 0.1;
|
||||||
var CHANCE_OF_TWEETING = 0.05;
|
var CHANCE_OF_TWEETING = 0.05;
|
||||||
|
@ -74,14 +44,21 @@ var BIRD_GRAVITY = -0.1;
|
||||||
var BIRD_FLAP_SPEED = 10.0;
|
var BIRD_FLAP_SPEED = 10.0;
|
||||||
var BIRD_VELOCITY = 0.5;
|
var BIRD_VELOCITY = 0.5;
|
||||||
var myPosition = MyAvatar.position;
|
var myPosition = MyAvatar.position;
|
||||||
var targetPosition = myPosition;
|
|
||||||
|
|
||||||
var range = 1.0; // Distance around avatar where I can move
|
var range = 1.0; // Distance around avatar where I can move
|
||||||
|
|
||||||
var particleID;
|
// This is our Bird object
|
||||||
var birdParticleIDs = [];
|
function Bird (particleID, tweetSound, targetPosition) {
|
||||||
var birdTweetSounds = [];
|
this.particleID = particleID;
|
||||||
var previousFlapOffsets = [];
|
this.tweetSound = tweetSound;
|
||||||
|
this.previousFlapOffset = 0;
|
||||||
|
this.targetPosition = targetPosition;
|
||||||
|
this.moving = false;
|
||||||
|
this.tweeting = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Array of birds
|
||||||
|
var birds = [];
|
||||||
|
|
||||||
function addBird()
|
function addBird()
|
||||||
{
|
{
|
||||||
|
@ -111,22 +88,19 @@ function addBird()
|
||||||
color = { red: 50, green: 67, blue: 144 };
|
color = { red: 50, green: 67, blue: 144 };
|
||||||
size = 0.15;
|
size = 0.15;
|
||||||
}
|
}
|
||||||
size = 10000;
|
|
||||||
var properties = {
|
var properties = {
|
||||||
lifetime: birdLifetime,
|
lifetime: birdLifetime,
|
||||||
position: randVector(-range, range),
|
position: Vec3.sum(randVector(-range, range), myPosition),
|
||||||
velocity: { x: 0, y: 0, z: 0 },
|
velocity: { x: 0, y: 0, z: 0 },
|
||||||
gravity: { x: 0, y: BIRD_GRAVITY, z: 0 },
|
gravity: { x: 0, y: BIRD_GRAVITY, z: 0 },
|
||||||
radius : size,
|
radius : size,
|
||||||
color: color
|
color: color
|
||||||
};
|
};
|
||||||
|
|
||||||
previousFlapOffsets.push(0.0);
|
birds.push(new Bird(Particles.addParticle(properties), tweet, properties.position));
|
||||||
birdTweetSounds.push(tweet);
|
|
||||||
birdParticleIDs.push(Particles.addParticle(properties));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var numBirds = 1;
|
var numBirds = 30;
|
||||||
|
|
||||||
// Generate the birds
|
// Generate the birds
|
||||||
for (var i = 0; i < numBirds; i++) {
|
for (var i = 0; i < numBirds; i++) {
|
||||||
|
@ -152,26 +126,26 @@ function updateBirds(deltaTime) {
|
||||||
|
|
||||||
// Update all the birds
|
// Update all the birds
|
||||||
for (var i = 0; i < numBirds; i++) {
|
for (var i = 0; i < numBirds; i++) {
|
||||||
particleID = birdParticleIDs[i];
|
particleID = birds[i].particleID;
|
||||||
var properties = Particles.getParticleProperties(particleID);
|
var properties = Particles.getParticleProperties(particleID);
|
||||||
|
|
||||||
// Tweeting behavior
|
// Tweeting behavior
|
||||||
if (tweeting == 0) {
|
if (birds[i].tweeting == 0) {
|
||||||
if (Math.random() < CHANCE_OF_TWEETING) {
|
if (Math.random() < CHANCE_OF_TWEETING) {
|
||||||
var options = new AudioInjectionOptions();
|
var options = new AudioInjectionOptions();
|
||||||
options.position = properties.position;
|
options.position = properties.position;
|
||||||
options.volume = 0.75;
|
options.volume = 0.75;
|
||||||
Audio.playSound(birdTweetSounds[i], options);
|
Audio.playSound(birds[i].tweetSound, options);
|
||||||
tweeting = 10;
|
birds[i].tweeting = 10;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
tweeting -= 1;
|
birds[i].tweeting -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Begin movement by getting a target
|
// Begin movement by getting a target
|
||||||
if (moving == false) {
|
if (birds[i].moving == false) {
|
||||||
if (Math.random() < CHANCE_OF_MOVING) {
|
if (Math.random() < CHANCE_OF_MOVING) {
|
||||||
targetPosition = vPlus(randVector(-range, range), myPosition);
|
var targetPosition = Vec3.sum(randVector(-range, range), myPosition);
|
||||||
|
|
||||||
if (targetPosition.x < 0) {
|
if (targetPosition.x < 0) {
|
||||||
targetPosition.x = 0;
|
targetPosition.x = 0;
|
||||||
|
@ -192,26 +166,28 @@ function updateBirds(deltaTime) {
|
||||||
targetPosition.z = TREE_SCALE;
|
targetPosition.z = TREE_SCALE;
|
||||||
}
|
}
|
||||||
|
|
||||||
moving = true;
|
birds[i].targetPosition = targetPosition;
|
||||||
|
|
||||||
|
birds[i].moving = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If we are moving, move towards the target
|
// If we are moving, move towards the target
|
||||||
if (moving) {
|
if (birds[i].moving) {
|
||||||
var desiredVelocity = vMinus(targetPosition, properties.position);
|
var desiredVelocity = Vec3.subtract(birds[i].targetPosition, properties.position);
|
||||||
desiredVelocity = vScalarMult(vNormalize(desiredVelocity), BIRD_VELOCITY);
|
desiredVelocity = vScalarMult(Vec3.normalize(desiredVelocity), BIRD_VELOCITY);
|
||||||
|
|
||||||
properties.velocity = vInterpolate(properties.velocity, desiredVelocity, 0.2);
|
properties.velocity = vInterpolate(properties.velocity, desiredVelocity, 0.2);
|
||||||
// If we are near the target, we should get a new target
|
// If we are near the target, we should get a new target
|
||||||
if (vLength(vMinus(properties.position, targetPosition)) < (properties.radius / 5.0)) {
|
if (Vec3.length(Vec3.subtract(properties.position, birds[i].targetPosition)) < (properties.radius / 5.0)) {
|
||||||
moving = false;
|
birds[i].moving = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use a cosine wave offset to make it look like its flapping.
|
// Use a cosine wave offset to make it look like its flapping.
|
||||||
var offset = Math.cos(nowTimeInSeconds * BIRD_FLAP_SPEED) * properties.radius;
|
var offset = Math.cos(nowTimeInSeconds * BIRD_FLAP_SPEED) * properties.radius;
|
||||||
properties.position.y = properties.position.y + (offset - previousFlapOffsets[i]);
|
properties.position.y = properties.position.y + (offset - birds[i].previousFlapOffset);
|
||||||
// Change position relative to previous offset.
|
// Change position relative to previous offset.
|
||||||
previousFlapOffsets[i] = offset;
|
birds[i].previousFlapOffset = offset;
|
||||||
|
|
||||||
// Update the particle
|
// Update the particle
|
||||||
Particles.editParticle(particleID, properties);
|
Particles.editParticle(particleID, properties);
|
||||||
|
|
Loading…
Reference in a new issue