If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

# 多个对象的运动

``````var movers = [];

for (var i = 0; i < 20; i++) {
movers[i] = new Mover();
}``````

``````var Mover = function() {
this.mass = 1;
this.position = new PVector(30, 30);
this.velocity = new PVector(0, 0);
this.acceleration = new PVector(0, 0);
};``````
……我们发现每个 `Mover` 对象都会是完全一样的。我们想要的是有 不同 质量和初始位置的 `Mover` 对象。现在我们需要通过添加参数来提高构造函数的复杂性。
``````var Mover = function(m, x, y) {
this.mass = m;
this.position = new PVector(x, y);
this.velocity = new PVector(0, 0);
this.acceleration = new PVector(0, 0);
};``````

``````// 窗口左侧的一个大弹球
var m1 = new Mover(10, 0, height/2);
// 窗口右侧的一个小弹球
var m2 = new Mover(0.1, width, height/2);``````

``````for (var i = 0; i < 20; i++) {
movers[i] = new Mover(random(0.1, 5), 0, 0);
}``````

``````draw = function() {
background(50, 50, 50);

for (var i = 0; i < movers.length; i++) {
var wind = new PVector(0.01, 0);
var gravity = new PVector(0, 0.1);
movers[i].applyForce(wind);
movers[i].applyForce(gravity);
movers[i].update();
movers[i].display();
movers[i].checkEdges();
}
};``````

“自然模拟”系列课程是由 Daniel Shiffman 的 "编程的本质" 衍生而来，基于 知识共享 著名-非商用性 3.0 本地化许可协议