Skip to content

Instantly share code, notes, and snippets.

@miketucker
Created November 16, 2013 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miketucker/7506124 to your computer and use it in GitHub Desktop.
Save miketucker/7506124 to your computer and use it in GitHub Desktop.
Abstracted from the SvgVerlet.js class
// Generated by CoffeeScript 1.6.3
var RubberPoint, VerletPoint, VerletStick,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
VerletPoint = (function() {
VerletPoint.prototype.down = false;
VerletPoint.prototype.oldX = null;
VerletPoint.prototype.oldY = null;
VerletPoint.prototype.originalX = 0;
VerletPoint.prototype.originalY = 0;
VerletPoint.prototype.x = 0;
VerletPoint.prototype.y = 0;
VerletPoint.prototype.forceX = 0;
VerletPoint.prototype.forceY = 0;
VerletPoint.scene = null;
function VerletPoint(x, y, locked) {
this.x = x;
this.y = y;
this.locked = locked != null ? locked : false;
this.originalX = this.oldX = this.x;
this.originalY = this.oldY = this.y;
}
VerletPoint.prototype.setPos = function(x, y) {
this.x = this.oldX = x;
return this.y = this.oldY = y;
};
VerletPoint.prototype.force = function(x, y) {
this.forceX += x;
return this.forceY += y;
};
VerletPoint.prototype.update = function() {
var tempX, tempY;
if (this.dead || this.down || this.locked) {
return null;
}
tempX = this.x;
tempY = this.y;
this.x += this.x - this.oldX + this.forceX;
this.y += this.y - this.oldY + this.forceY;
this.oldX = tempX;
this.oldY = tempY;
this.forceX = 0;
this.forceY = 0;
return null;
};
return VerletPoint;
})();
RubberPoint = (function(_super) {
__extends(RubberPoint, _super);
RubberPoint.prototype.rubberAmplitude = 0.1;
RubberPoint.prototype.rubberDamping = 0.1;
RubberPoint.prototype.rubberForceX = 0;
RubberPoint.prototype.rubberForceY = 0;
RubberPoint.maxAmp = 0.02;
RubberPoint.minAmp = 0.005;
function RubberPoint(x, y, locked) {
this.x = x;
this.y = y;
this.locked = locked != null ? locked : false;
RubberPoint.__super__.constructor.call(this, this.x, this.y, this.locked);
this.rubberAmplitude = RubberPoint.minAmp + (RubberPoint.maxAmp - RubberPoint.minAmp);
}
RubberPoint.prototype.update = function() {
if (this.dead || this.down || this.locked) {
return null;
}
this.rubberForceX = (this.originalX - this.x) * this.rubberAmplitude;
this.rubberForceY = (this.originalY - this.y) * this.rubberAmplitude;
this.x += this.rubberForceX;
this.y += this.rubberForceY;
return RubberPoint.__super__.update.call(this);
};
RubberPoint.resetDefaults = function() {
RubberPoint.minAmp = 0.02;
return RubberPoint.maxAmp = 0.005;
};
return RubberPoint;
})(VerletPoint);
VerletStick = (function() {
function VerletStick(a, b) {
var dx, dy,
_this = this;
this.pointa = a;
this.pointb = b;
this.torn = false;
this.tearResist = 9999;
dx = a.x - b.x;
dy = a.y - b.y;
this.hypotenuse = Math.sqrt(dx * dx + dy * dy);
if (this.pointa.locked && this.pointb.locked) {
this.contract(function() {
return null;
});
} else if (this.pointa.locked) {
this.contract = function() {
var diff, h, offx, offy;
dx = _this.pointb.x - _this.pointa.x;
dy = _this.pointb.y - _this.pointa.y;
h = Math.sqrt(dx * dx + dy * dy);
if (h > _this.tearResist) {
_this.torn = true;
}
diff = _this.hypotenuse - h;
offx = diff * dx / h;
offy = diff * dy / h;
_this.pointb.x += offx;
_this.pointb.y += offy;
return null;
};
} else if (this.pointb.locked) {
this.contract = function() {
var diff, h, offx, offy;
dx = _this.pointb.x - _this.pointa.x;
dy = _this.pointb.y - _this.pointa.y;
h = Math.sqrt(dx * dx + dy * dy);
if (h > _this.tearResist) {
_this.torn = true;
}
diff = _this.hypotenuse - h;
offx = diff * dx / h;
offy = diff * dy / h;
_this.pointa.x -= offx;
_this.pointa.y -= offy;
return null;
};
} else {
this.contract = function() {
var diff, h, offx, offy;
dx = _this.pointb.x - _this.pointa.x;
dy = _this.pointb.y - _this.pointa.y;
h = Math.sqrt(dx * dx + dy * dy);
if (h > _this.tearResist) {
_this.torn = true;
}
diff = _this.hypotenuse - h;
offx = (diff * dx / h) * .5;
offy = (diff * dy / h) * .5;
_this.pointa.x -= offx;
_this.pointa.y -= offy;
_this.pointb.x += offx;
_this.pointb.y += offy;
return null;
};
}
}
return VerletStick;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment