Skip to content

Commit 6d88961

Browse files
committed
initial work on smoothDamp
1 parent 832c3e4 commit 6d88961

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

src/goo/math/MathUtils.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,4 +409,44 @@ MathUtils.warnNaN = function (object, property) {
409409
});
410410
};
411411

412+
/**
413+
* Gradually changes a value towards a desired goal over time. The value is smoothed by some spring-damper like function, which will never overshoot. The function can be used to smooth any kind of value, positions, colors, scalars.
414+
* @param {number} current The current position.
415+
* @param {number} target The position we are trying to reach.
416+
* @param {Vector2} store An object to store the current position and velocity in.
417+
* @param {number} deltaTime The time since the last call to this function.
418+
* @param {number} [smoothTime=0.3] Approximately the time it will take to reach the target. A smaller value will reach the target faster.
419+
* @param {number} [maxSpeed=1e7] Optionally allows you to clamp the maximum speed.
420+
*/
421+
MathUtils.smoothDamp = function (current, target, store, deltaTime, smoothTime, maxSpeed) {
422+
if (smoothTime === undefined) {
423+
smoothTime = 0.3;
424+
}
425+
if (maxSpeed === undefined) {
426+
maxSpeed = 1e7;
427+
}
428+
429+
smoothTime = Math.max(0.0001, smoothTime);
430+
var num = 2 / smoothTime;
431+
var num2 = num * deltaTime;
432+
var num3 = 1 / (1 + num2 + 0.48 * num2 * num2 + 0.235 * num2 * num2 * num2);
433+
var num4 = current - target;
434+
var num5 = target;
435+
var num6 = maxSpeed * smoothTime;
436+
num4 = Math.max(-num6, Math.min(num6, num4));
437+
target = current - num4;
438+
var currentVelocity = store.y;
439+
var num7 = (currentVelocity + num * num4) * deltaTime;
440+
currentVelocity = (currentVelocity - num * num7) * num3;
441+
var num8 = target + (num4 + num7) * num3;
442+
if (num5 - current > 0 === num8 > num5){
443+
num8 = num5;
444+
currentVelocity = (num8 - num5) / deltaTime;
445+
}
446+
store.x = num8;
447+
store.y = currentVelocity;
448+
449+
return num8;
450+
};
451+
412452
module.exports = MathUtils;

src/goo/math/Vector2.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,27 @@ Vector2.prototype.subVector = ObjectUtils.warnOnce('Vector2.prototype.subVector
765765
return this;
766766
});
767767

768+
/**
769+
* Gradually changes the vector value value towards a desired goal over time. See MathUtils.smoothDamp.
770+
* @param {Vector2} target The position we are trying to reach.
771+
* @param {Vector2} currentVelocity An object to store the current position and velocity in.
772+
* @param {number} deltaTime The time since the last call to this function.
773+
* @param {number} [smoothTime=0.3] Approximately the time it will take to reach the target. A smaller value will reach the target faster.
774+
* @param {number} [maxSpeed=1e7] Optionally allows you to clamp the maximum speed.
775+
*/
776+
Vector2.prototype.smoothDamp = (function () {
777+
var tempVec2 = new Vector2();
778+
return function (target, currentVelocity, deltaTime, smoothTime, maxSpeed) {
779+
tempVec2.y = currentVelocity.x;
780+
this.x = MathUtils.smoothDamp(this.x, target.x, tempVec2, deltaTime, smoothTime, maxSpeed);
781+
currentVelocity.x = tempVec2.y;
782+
783+
tempVec2.y = currentVelocity.y;
784+
this.y = MathUtils.smoothDamp(this.y, target.y, tempVec2, deltaTime, smoothTime, maxSpeed);
785+
currentVelocity.y = tempVec2.y;
786+
};
787+
})();
788+
768789
//!schteppe: not shimming Vector2.prototype.seta, it's been warned about forever
769790
//!schteppe: not shimming Vector2.prototype.setd, it's been warned about forever
770791
//!schteppe: not shimming Vector2.prototype.setv, it's been warned about forever

src/goo/math/Vector3.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var ObjectUtils = require('../util/ObjectUtils');
22
var MathUtils = require('./MathUtils');
33
var Vector = require('./Vector');
4+
var Vector2 = require('./Vector2');
45
var Vector4 = require('./Vector4');
56

67
/**
@@ -732,6 +733,31 @@ Vector3.prototype.setArray = function (array) {
732733
return this;
733734
};
734735

736+
/**
737+
* Gradually changes the vector value value towards a desired goal over time. See MathUtils.smoothDamp.
738+
* @param {Vector3} target The position we are trying to reach.
739+
* @param {Vector3} currentVelocity An object to store the current position and velocity in.
740+
* @param {number} deltaTime The time since the last call to this function.
741+
* @param {number} [smoothTime=0.3] Approximately the time it will take to reach the target. A smaller value will reach the target faster.
742+
* @param {number} [maxSpeed=1e7] Optionally allows you to clamp the maximum speed.
743+
*/
744+
Vector3.prototype.smoothDamp = (function () {
745+
var tempVec2 = new Vector2();
746+
return function (target, currentVelocity, deltaTime, smoothTime, maxSpeed) {
747+
tempVec2.y = currentVelocity.x;
748+
this.x = MathUtils.smoothDamp(this.x, target.x, tempVec2, deltaTime, smoothTime, maxSpeed);
749+
currentVelocity.x = tempVec2.y;
750+
751+
tempVec2.y = currentVelocity.y;
752+
this.y = MathUtils.smoothDamp(this.y, target.y, tempVec2, deltaTime, smoothTime, maxSpeed);
753+
currentVelocity.y = tempVec2.y;
754+
755+
tempVec2.y = currentVelocity.z;
756+
this.z = MathUtils.smoothDamp(this.z, target.z, tempVec2, deltaTime, smoothTime, maxSpeed);
757+
currentVelocity.z = tempVec2.y;
758+
};
759+
})();
760+
735761
// SHIM START
736762
Object.defineProperty(Vector3.prototype, 'data', {
737763
get: ObjectUtils.warnOnce('The .data property of Vector3 was removed. Please use the .x, .y and .z properties instead.', function () {

test/unit/math/Vector3-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,14 @@ describe('Vector3', function () {
377377
});
378378
});
379379

380+
describe('smoothDamp', function () {
381+
it('can smoothDamp', function () {
382+
var target = new Vector3(1,1,1);
383+
var currentVelocity = new Vector3();
384+
expect(new Vector3().smoothDamp(target, currentVelocity, 0.1, 0.3, 1e7)).not.toEqual([0, 0, 0]);
385+
});
386+
});
387+
380388
describe('deprecated shim added 2015-10-07 (v1.0)', function () {
381389
describe('.data', function () {
382390
it('has working getters', function () {

0 commit comments

Comments
 (0)