verlet
Version:
simple 2d physics for js.
112 lines (85 loc) • 3.68 kB
JavaScript
/*
Copyright 2013 Sub Protocol and other contributors
http://subprotocol.com/
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// generic verlet entities
var VerletJS = require('./verlet')
var Particle = VerletJS.Particle
var constraints = require('./constraint')
var DistanceConstraint = constraints.DistanceConstraint
VerletJS.prototype.point = function(pos) {
var composite = new this.Composite();
composite.particles.push(new Particle(pos));
this.composites.push(composite);
return composite;
}
VerletJS.prototype.lineSegments = function(vertices, stiffness) {
var i;
var composite = new this.Composite();
for (i in vertices) {
composite.particles.push(new Particle(vertices[i]));
if (i > 0)
composite.constraints.push(new DistanceConstraint(composite.particles[i], composite.particles[i-1], stiffness));
}
this.composites.push(composite);
return composite;
}
VerletJS.prototype.cloth = function(origin, width, height, segments, pinMod, stiffness) {
var composite = new this.Composite();
var xStride = width/segments;
var yStride = height/segments;
var x,y;
for (y=0;y<segments;++y) {
for (x=0;x<segments;++x) {
var px = origin.x + x*xStride - width/2 + xStride/2;
var py = origin.y + y*yStride - height/2 + yStride/2;
composite.particles.push(new Particle(new Vec2(px, py)));
if (x > 0)
composite.constraints.push(new DistanceConstraint(composite.particles[y*segments+x], composite.particles[y*segments+x-1], stiffness));
if (y > 0)
composite.constraints.push(new DistanceConstraint(composite.particles[y*segments+x], composite.particles[(y-1)*segments+x], stiffness));
}
}
for (x=0;x<segments;++x) {
if (x%pinMod == 0)
composite.pin(x);
}
this.composites.push(composite);
return composite;
}
VerletJS.prototype.tire = function(origin, radius, segments, spokeStiffness, treadStiffness) {
var stride = (2*Math.PI)/segments;
var i;
var composite = new this.Composite();
// particles
for (i=0;i<segments;++i) {
var theta = i*stride;
composite.particles.push(new Particle(new Vec2(origin.x + Math.cos(theta)*radius, origin.y + Math.sin(theta)*radius)));
}
var center = new Particle(origin);
composite.particles.push(center);
// constraints
for (i=0;i<segments;++i) {
composite.constraints.push(new DistanceConstraint(composite.particles[i], composite.particles[(i+1)%segments], treadStiffness));
composite.constraints.push(new DistanceConstraint(composite.particles[i], center, spokeStiffness))
composite.constraints.push(new DistanceConstraint(composite.particles[i], composite.particles[(i+5)%segments], treadStiffness));
}
this.composites.push(composite);
return composite;
}