Skip to content

Instantly share code, notes, and snippets.

@willbailey
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save willbailey/4d99ecc3d0d963d0d9bc to your computer and use it in GitHub Desktop.

Select an option

Save willbailey/4d99ecc3d0d963d0d9bc to your computer and use it in GitHub Desktop.

Revisions

  1. willbailey revised this gist Nov 6, 2014. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions SpringChain.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    'use strict';
    var SpringChain = function(attachmentSpringConfig, mainSpringConfig) {
    this._springSystem = new rebound.SpringSystem();
    this._listeners = [];
    @@ -10,9 +11,9 @@ var SpringChain = function(attachmentSpringConfig, mainSpringConfig) {
    };

    SpringChain.DEFAULT_MAIN_SPRING_CONFIG =
    rebound.SpringConfig.fromOrigamiTensionAndFriction(40, 7);
    rebound.SpringConfig.fromOrigamiTensionAndFriction(255, 11);
    SpringChain.DEFAULT_ATTACHMENT_SPRING_CONFIG =
    rebound.SpringConfig.fromOrigamiTensionAndFriction(80, 9);
    rebound.SpringConfig.fromOrigamiTensionAndFriction(40, 7);

    SpringChain.prototype.addSpring = function(listener) {
    var spring = this._springSystem.createSpring()
    @@ -29,11 +30,12 @@ SpringChain.prototype.getControlSpring = function() {

    SpringChain.prototype.setControlSpringIndex = function(idx) {
    this._controlSpringIndex = idx;
    var controlSpring = this._springs[i];
    var controlSpring = this._springs[idx];
    if (!controlSpring) {
    return null;
    }
    for (var i = 0, len = this._springs.length; i < len; i++) {
    var spring = this._springs[i];
    spring.setSpringConfig(this._attachmentSpringConfig);
    }
    this.getControlSpring().setSpringConfig(this._mainSpringConfig);
  2. willbailey created this gist Nov 6, 2014.
    100 changes: 100 additions & 0 deletions SpringChain.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,100 @@
    var SpringChain = function(attachmentSpringConfig, mainSpringConfig) {
    this._springSystem = new rebound.SpringSystem();
    this._listeners = [];
    this._springs = [];
    this._controlSpringIndex = -1;
    this._attachmentSpringConfig = attachmentSpringConfig ||
    SpringChain.DEFAULT_ATTACHMENT_SPRING_CONFIG;
    this._mainSpringConfig = mainSpringConfig ||
    SpringChain.DEFAULT_MAIN_SPRING_CONFIG;
    };

    SpringChain.DEFAULT_MAIN_SPRING_CONFIG =
    rebound.SpringConfig.fromOrigamiTensionAndFriction(40, 7);
    SpringChain.DEFAULT_ATTACHMENT_SPRING_CONFIG =
    rebound.SpringConfig.fromOrigamiTensionAndFriction(80, 9);

    SpringChain.prototype.addSpring = function(listener) {
    var spring = this._springSystem.createSpring()
    .addListener(this)
    .setSpringConfig(this._attachmentSpringConfig);
    this._springs.push(spring);
    this._listeners.push(listener);
    return this;
    };

    SpringChain.prototype.getControlSpring = function() {
    return this._springs[this._controlSpringIndex];
    }

    SpringChain.prototype.setControlSpringIndex = function(idx) {
    this._controlSpringIndex = idx;
    var controlSpring = this._springs[i];
    if (!controlSpring) {
    return null;
    }
    for (var i = 0, len = this._springs.length; i < len; i++) {
    spring.setSpringConfig(this._attachmentSpringConfig);
    }
    this.getControlSpring().setSpringConfig(this._mainSpringConfig);
    return this;
    };

    SpringChain.prototype.getControlSpring = function() {
    return this._springs[this._controlSpringIndex];
    };

    SpringChain.prototype.getSpringSystem = function() {
    return this._springSystem;
    };

    SpringChain.prototype.getAllSprings = function() {
    return this._springs;
    };

    SpringChain.prototype._proxyEvent = function(evt, spring) {
    var idx = this._springs.indexOf(spring);
    var listener = this._listeners[idx];
    if (!listener) {
    return;
    }
    var handler = listener[evt];
    if (!handler) {
    return;
    }
    handler.call(listener, spring);
    };

    SpringChain.prototype.onSpringActivate = function(spring) {
    this._proxyEvent('onSpringActivate', spring);
    };

    SpringChain.prototype.onSpringEndStateChange = function(spring) {
    this._proxyEvent('onSpringEndStateChange', spring);
    };

    SpringChain.prototype.onSpringUpdate = function(spring) {
    var idx = this._springs.indexOf(spring);
    var listener = this._listeners[idx];

    var above = -1, below = -1;
    if (idx === this._controlSpringIndex) {
    below = idx - 1;
    above = idx + 1;
    } else if (idx < this._controlSpringIndex) {
    below = idx - 1;
    } else if (idx > this._controlSpringIndex) {
    above = idx + 1;
    }
    if (above > -1 && above < this._springs.length) {
    this._springs[above].setEndValue(spring.getCurrentValue());
    }
    if (below > -1 && below < this._springs.length) {
    this._springs[below].setEndValue(spring.getCurrentValue());
    }
    listener.onSpringUpdate(spring);
    };

    SpringChain.prototype.onSpringAtRest = function(spring) {
    this._proxyEvent('onSpringAtRest', spring);
    };