【第六期(中)】太长不看版

为什么要重复造轮子
直接用库才对嘛

  • 下载Toxi库
  • 复制以下代码
  • 完成
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    import toxi.geom.*;
    import toxi.physics2d.*;
    import toxi.physics2d.behaviors.*;
    VerletPhysics2D physics;
    AttractionBehavior2D mouseAttractor;
    Vec2D mouse;

    void setup() {
    size(500, 500, FX2D);
    noFill();
    physics = new VerletPhysics2D();
    physics.setDrag(0.30f);
    addParticle();
    mouse = new Vec2D(mouseX, mouseY);
    mouseAttractor = new AttractionBehavior2D(mouse, 500, 0.2);
    physics.addBehavior(mouseAttractor);
    }
    void draw() {
    background(255);
    mouse.set(mouseX, mouseY);
    physics.particles.get(0).x = mouseX;
    physics.particles.get(0).y = mouseY;
    if (mousePressed) {
    addParticle();
    }
    physics.update();
    for (VerletParticle2D p : physics.particles) {
    ellipse(p.x, p.y, 20, 20);
    }
    }
    void addParticle() {
    VerletParticle2D p = new VerletParticle2D(Vec2D.randomVector().scale(5).addSelf(mouseX, mouseY));
    physics.addParticle(p);
    physics.addBehavior(new AttractionBehavior2D(p, 20, -1.2f, 0.01f));
    }