added mass

This commit is contained in:
nora 2021-06-17 10:27:36 +02:00
parent 1888dca4e8
commit 10ca04b98e
5 changed files with 19 additions and 66 deletions

View file

@ -7,11 +7,13 @@ const particles: Particle[] = [];
export function particlesInit() {
const chargeToMass = [0.1, 1, 10];
for (let i = 0; i < 200; i++) {
const charge = Math.random() < 0.3 ? 0 : Math.random() < 0.5 ? 1 : -1;
particles.push(new Particle(new Vector(
Math.random() * (CANVAS_WIDTH - 100) + 50,
Math.random() * (CANVAS_HEIGHT - 100) + 50
), Math.random() < 0.3 ? 0 : Math.random() < 0.6 ? -1 : 1));
), charge, chargeToMass[charge - 1]));
}
}

View file

@ -12,18 +12,18 @@ const RANDOM_ACCELERATION = 2;
export default class Particle implements SimObject {
private _position: Vector;
private _velocity: Vector;
// private _color: FillStyle;
private _mass: number;
private _charge: number;
constructor(position: Vector, /*color = "black", */charge = 0) {
constructor(position: Vector, charge = 0, mass = 1) {
this._position = position;
this._velocity = new Vector();
//this._color = color;
this._charge = charge;
this._mass = mass;
}
public applyForce(force: Vector) {
this._velocity = this._velocity.add(force);
this._velocity = this._velocity.add(force.scaleInverse(this._mass));
}
public draw(ctx: Ctx): void {

View file

@ -11,15 +11,19 @@ export default class Vector {
return new Vector(this.x + b.x, this.y + b.y);
}
public sub(b: Vector) {
public sub(b: Vector): Vector {
return new Vector(this.x - b.x, this.y - b.y);
}
public scale(number: number) {
return new Vector(this.x * number, this.y * number);
public scale(n: number): Vector {
return new Vector(this.x * n, this.y * n);
}
public magnitude() {
public scaleInverse(n: number): Vector {
return new Vector(this.x / n, this.y / n);
}
public magnitude(): number {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
@ -27,12 +31,12 @@ export default class Vector {
return Math.sqrt((this.x - b.x) ** 2 + (this.y - b.y) ** 2)
}
public negated() {
public negated(): Vector {
return new Vector(-this.x, -this.y);
}
public normalized() {
public normalized(): Vector {
const factor = this.magnitude();
return new Vector(this.x / factor, this.y / factor);
}
}
}