【第六浅】PinConstraint or Composite.pin
发布在九浅一深 verlet-js 物理引擎【完结撒花】2015年6月14日view:4839图形学,动画BrettBat物理引擎
在文章任何区域双击击即可给文章添加【评注】!浮到评注点上可以查看详情。

联系与渊源

在 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;
}
评论
发表评论
暂无评论
WRITTEN BY
BoltDoggy
A doggy named Bolt.
TA的新浪微博
PUBLISHED IN
九浅一深 verlet-js 物理引擎【完结撒花】

verlet-js 【译】

原文取自官方示例首页

转载请声明出处:[前端乱炖-Bolt 个人专栏]http://www.html-js.com/article/column/4527

About 关于

verlet-js,一个简单 verlet integration(混合) 物理引擎,javascript 实现,作者 Sub Protocol。Verlet 发音为 “ver-ley”。

Examples 示例

  1. Shapes(形状) (Hello world)
  2. Fractal(不规则碎片形) Trees
  3. Cloth(布)
  4. Spiderweb(蜘蛛网)

Features 特性

以下是 verlet-js 内部的可用实体分层:

  • 模拟器(Simulation):拥有复合实体,驱动所在一个场景内所有物理、动画的根对象
  • 复合材料(Composites):所在场景内的可用高阶对象(球,桥,布等)
  • 颗粒(Particles):仅仅是一个在空间内可以响应重力的点
  • 约束(Constraints):互相联结颗粒,这样他们就可以彼此互动
    1. 钉(Pin):将一个颗粒绑定在空间内的一个静态(static/fixes)位置上
    2. 距离(Distance):将两个颗粒互相固定在线性距离上
    3. 角(Angle):通过一个锐角彼此固定 3 个颗粒

License 许可证

你可以在非常宽松的 MIT 许可证下使用 verlet-js。

Source Code 源代码

[View project on GitHub](View project on GitHub)

版权声明

原文

Copyright 2013 Sub Protocol and other contributors.

http://subprotocol.com/

译文

原文取自官方示例首页

转载请声明出处:[前端乱炖-Bolt 个人专栏]http://www.html-js.com/article/column/4527

友情链接 大搜车前端团队博客
我的收藏