联系与渊源
在 verlet-js 中,pin 住一点是复合材料的一个通用方法
verlet.js 部分源码
Composite.prototype.pin = function(index, pos) {
pos = pos || this.particles[index].pos;
var pc = new PinConstraint(this.particles[index], pos);
this.constraints.push(pc);
return pc;
}
更方便的使用
shapes.html 部分源码
var segment = sim.lineSegments([new Vec2(20,10), new Vec2(40,10), new Vec2(60,10), new Vec2(80,10), new Vec2(100,10)], 0.02);
var pin = segment.pin(0);
var pin = segment.pin(4);
lineSegments 的实体原型中就没必要提供(重写) pin 方法了
原型定义时也可以这样用
objects.js 部分源码
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;
}