50 lines
1.5 KiB
JavaScript
50 lines
1.5 KiB
JavaScript
class Particle {
|
|
constructor(x, y) {
|
|
this.mass = 1;
|
|
this.position = createVector(x, y);
|
|
this.velocity = createVector(0, 0);
|
|
this.gravity = createVector(0, 10); // G = 9.81
|
|
this.hue = 0;
|
|
this.saturation = 0;
|
|
this.brightness = 100;
|
|
this.alpha = 1;
|
|
this.size = 8;
|
|
this.lifetime = 0;
|
|
}
|
|
|
|
move(force) {
|
|
if (this.position.y > 600) return;
|
|
// 计算加速度
|
|
const acceleration = createVector(0, 0);
|
|
acceleration.add(this.gravity);
|
|
if (force) acceleration.add(force.div(this.mass));
|
|
// 计算时间差
|
|
const duration = deltaTime / 1000;
|
|
this.lifetime += duration;
|
|
// 计算速度差及位移
|
|
const deltaVelocity = acceleration.mult(duration);
|
|
const averageVelocity = deltaVelocity.copy().div(2).add(this.velocity);
|
|
const movement = averageVelocity.mult(duration);
|
|
// 更新速度及位置
|
|
this.velocity.add(deltaVelocity);
|
|
this.position.add(movement);
|
|
const report = {
|
|
time: this.lifetime.toFixed(3),
|
|
height: (height - this.position.y).toFixed(3),
|
|
velocity: this.velocity.mag().toFixed(3),
|
|
};
|
|
print(report);
|
|
}
|
|
|
|
show() {
|
|
noStroke();
|
|
fill(this.hue, this.saturation, this.brightness, this.alpha);
|
|
circle(this.position.x, this.position.y, this.size);
|
|
}
|
|
}
|
|
|
|
class Particles {
|
|
constructor() {}
|
|
show() {}
|
|
}
|