Skip to content

Commit

Permalink
Beta update 23
Browse files Browse the repository at this point in the history
  • Loading branch information
morgan3d committed Jan 18, 2020
1 parent 399e771 commit 924ec97
Show file tree
Hide file tree
Showing 8 changed files with 499 additions and 117 deletions.
123 changes: 123 additions & 0 deletions console/filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
Open Source under the BSD-2-Clause License:
Copyright 2020 Morgan McGuire, https://casual-effects.com
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";

/* "1 euro filter"
Tuning:
- Decreasing the `minimumCutoff` [frequency] will decrease jitter.
- Increasing the `speed` will decrease lag.
Start with speed = 0 and adjust minCutoff until results are good
for slowly-changing values. Then change the values quickly and
increase speed until the lag is minimal (while still preserving
jitter filtering).
https://hal.inria.fr/hal-00670496/document
https://jaantollander.com/noise-filtering-using-one-euro-filter.html
*/
function EuroFilter(minCutoff, speed) {
if (minCutoff === undefined) { minCutoff = 1; }
speed = speed || 0;
minCutoff = Math.max(1e-4, minCutoff);
speed = Math.max(0, speed);

const derivativeCutoff = 1;
this._param = {
smoothValue: NaN,
smoothDerivative: 0,
derivativeCutoff: derivativeCutoff,

// f_c from the paper
minCutoff: minCutoff,

// beta from the paper
speed: speed
};
}


EuroFilter.prototype._smoothLerp = function(curr, prev, dt, cutoff) {
// The 2pi is meaningless for a non-cyclic system, but we retain
// it to match filter coefficients when porting from other systems.
const r = 2 * Math.PI * cutoff * dt;
const a = r / (r + 1);
return a * curr + (1 - a) * prev;
}


EuroFilter.prototype.reset = function () { this._param.smoothValue = NaN; }

EuroFilter.prototype.get = function () { return this._param.smoothValue; }


// dt defaults to 1
EuroFilter.prototype.update = function (x, dt) {
if (dt === undefined) { dt = 1; }
const param = this._param, smoothLerp = this._smoothLerp;

if ((param.smoothValue === undefined) || isNaN(param.smoothValue) || (dt <= 0) || (dt === Infinity)) {
// First value
param.smoothValue = x;
param.smoothDerivative = 0;
return;
}

// Filter the derivative
const dx = (x - param.smoothValue) / dt;
param.smoothDerivative = smoothLerp(dx, param.smoothDerivative, dt, param.derivativeCutoff);

// Filter the value
const cutoff = param.minCutoff + Math.abs(param.smoothDerivative) * param.speed;
param.smoothValue = smoothLerp(x, param.smoothValue, dt, cutoff);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////

function EMWAFilter(hysteresis) {
if (hysteresis === undefined) { hysteresis = 0.9};

this._hysteresis = Math.min(1, Math.max(hysteresis, 0));
this.reset();
}

EMWAFilter.prototype.reset = function() { this._smoothValue = NaN; }

EMWAFilter.prototype.get = function() { return this._smoothValue; }

EMWAFilter.prototype.update = function(value, dt) {
if (dt === undefined) { dt = 1; }
if (this._smoothValue === undefined || isNaN(this._smoothValue) || dt <= 0 || dt === Infinity) {
this._smoothValue = value;
return;
}

const h = Math.pow(this._hysteresis, 1 / dt);
this._smoothValue = this._smoothValue * h + value * (1 - h);
}
8 changes: 5 additions & 3 deletions console/matter-extensions.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
/* Extensions to matter.js added for quadplay */

// Extend collisionFilter so that it may contain a 'body' field and
// may contain an 'excludedBodies' array of bodies. If present,
// may contain an 'excludedBodies' WeakMap of bodies. If present,
// collision lists are checked both ways to determine if the objects
// can collide with each other. Either filter can exclude the other.
// Note that undefined > 0 is false, so an element that is either not
// present or has a count that is zero will not exclude the collision

Matter.Detector.canCollide = function(filterA, filterB) {
if ((filterA.group === filterB.group) && (filterA.group !== 0)) {
return filterA.group > 0;
} else if ((filterA.mask & filterB.category) !== 0 && (filterB.mask & filterA.category) !== 0) {
return ! (filterA.body &&
filterB.body &&
((filterA.excludedBodies && filterA.excludedBodies.length && (filterA.excludedBodies.indexOf(filterB.body) !== -1)) ||
(filterB.excludedBodies && filterB.excludedBodies.length && (filterB.excludedBodies.indexOf(filterA.body) !== -1))));
((filterA.excludedBodies && filterA.excludedBodies.get(filterB.body) > 0) ||
(filterB.excludedBodies && filterB.excludedBodies.get(filterA.body) > 0)));
}

return false;
Expand Down
Loading

0 comments on commit 924ec97

Please sign in to comment.