mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 05:17:24 +02:00
speedups per code review
This commit is contained in:
parent
ee95f7906b
commit
a72c49793e
1 changed files with 29 additions and 7 deletions
|
@ -11,7 +11,7 @@
|
||||||
|
|
||||||
|
|
||||||
var LIFETIME = 60;
|
var LIFETIME = 60;
|
||||||
var NUM_FISH = 20;
|
var NUM_FISH = 15;
|
||||||
var TANK_WIDTH = 3.0;
|
var TANK_WIDTH = 3.0;
|
||||||
var TANK_HEIGHT = 1.0;
|
var TANK_HEIGHT = 1.0;
|
||||||
var FISH_WIDTH = 0.03;
|
var FISH_WIDTH = 0.03;
|
||||||
|
@ -48,24 +48,37 @@ function updateFish(deltaTime) {
|
||||||
var averagePosition = { x: 0, y: 0, z: 0 };
|
var averagePosition = { x: 0, y: 0, z: 0 };
|
||||||
var birdPositionsCounted = 0;
|
var birdPositionsCounted = 0;
|
||||||
var birdVelocitiesCounted = 0;
|
var birdVelocitiesCounted = 0;
|
||||||
|
|
||||||
|
// First pre-load an array with properties on all the other fish so our per-fish loop
|
||||||
|
// isn't doing it.
|
||||||
|
var flockProperties = [];
|
||||||
|
for (var i = 0; i < fish.length; i++) {
|
||||||
|
var otherProps = Entities.getEntityProperties(fish[i].entityId, ["position", "velocity"]);
|
||||||
|
flockProperties.push(otherProps);
|
||||||
|
}
|
||||||
|
|
||||||
for (var i = 0; i < fish.length; i++) {
|
for (var i = 0; i < fish.length; i++) {
|
||||||
if (fish[i].entityId) {
|
if (fish[i].entityId) {
|
||||||
var properties = Entities.getEntityProperties(fish[i].entityId);
|
// Get only the properties we need, because that is faster
|
||||||
|
// var properties = flockProperties[i];
|
||||||
|
var properties = Entities.getEntityProperties(fish[i].entityId, ["position", "velocity"]);
|
||||||
// If Bird has been deleted, bail
|
// If Bird has been deleted, bail
|
||||||
if (properties.id != fish[i].entityId) {
|
if (properties.id != fish[i].entityId) {
|
||||||
fish[i].entityId = false;
|
fish[i].entityId = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var velocity = properties.velocity;
|
// Store old values so we can check if they have changed enough to update
|
||||||
var position = properties.position;
|
var velocity = { x: properties.velocity.x, y: properties.velocity.y, z: properties.velocity.z };
|
||||||
|
var position = { x: properties.position.x, y: properties.position.y, z: properties.position.z };
|
||||||
averageVelocity = { x: 0, y: 0, z: 0 };
|
averageVelocity = { x: 0, y: 0, z: 0 };
|
||||||
averagePosition = { x: 0, y: 0, z: 0 };
|
averagePosition = { x: 0, y: 0, z: 0 };
|
||||||
|
|
||||||
var othersCounted = 0;
|
var othersCounted = 0;
|
||||||
for (var j = 0; j < fish.length; j++) {
|
for (var j = 0; j < fish.length; j++) {
|
||||||
if (i != j) {
|
if (i != j) {
|
||||||
var otherProps = Entities.getEntityProperties(fish[j].entityId);
|
// Get only the properties we need, because that is faster
|
||||||
|
var otherProps = flockProperties[j];
|
||||||
var separation = Vec3.distance(properties.position, otherProps.position);
|
var separation = Vec3.distance(properties.position, otherProps.position);
|
||||||
if (separation < MAX_SIGHT_DISTANCE) {
|
if (separation < MAX_SIGHT_DISTANCE) {
|
||||||
averageVelocity = Vec3.sum(averageVelocity, otherProps.velocity);
|
averageVelocity = Vec3.sum(averageVelocity, otherProps.velocity);
|
||||||
|
@ -91,6 +104,7 @@ function updateFish(deltaTime) {
|
||||||
velocity = Vec3.mix(velocity, Vec3.multiply(Vec3.normalize(velocity), SWIMMING_SPEED), SWIMMING_FORCE);
|
velocity = Vec3.mix(velocity, Vec3.multiply(Vec3.normalize(velocity), SWIMMING_SPEED), SWIMMING_FORCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Keep fish in their 'tank'
|
// Keep fish in their 'tank'
|
||||||
if (position.x < lowerCorner.x) {
|
if (position.x < lowerCorner.x) {
|
||||||
position.x = lowerCorner.x;
|
position.x = lowerCorner.x;
|
||||||
|
@ -116,7 +130,15 @@ function updateFish(deltaTime) {
|
||||||
|
|
||||||
// Orient in direction of velocity
|
// Orient in direction of velocity
|
||||||
var rotation = Quat.rotationBetween(Vec3.UNIT_NEG_Z, velocity);
|
var rotation = Quat.rotationBetween(Vec3.UNIT_NEG_Z, velocity);
|
||||||
Entities.editEntity(fish[i].entityId, { position: position, velocity: velocity, rotation: Quat.mix(properties.rotation, rotation, 0.33) });
|
var VELOCITY_FOLLOW_RATE = 0.33;
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
Entities.editEntity(fish[i].entityId, { velocity: velocity, rotation: Quat.mix(properties.rotation, rotation, VELOCITY_FOLLOW_RATE) });
|
||||||
|
} else {
|
||||||
|
Entities.editEntity(fish[i].entityId, { position: position, velocity: velocity, rotation: Quat.mix(properties.rotation, rotation, VELOCITY_FOLLOW_RATE) });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue