45 lines
951 B
JavaScript
45 lines
951 B
JavaScript
export class MassObject {
|
|
mass = 0;
|
|
density = 1;
|
|
position = {x: undefined, y: undefined};
|
|
velocity = {x: 0, y: 0};
|
|
acceleration = {x: 0, y: 0};
|
|
color = {r: undefined, g: undefined, b: undefined};
|
|
created = undefined;
|
|
forces = []; // [{x, y}]
|
|
history = [];
|
|
alive = true;
|
|
|
|
constructor(x, y) {
|
|
this.position.x = x;
|
|
this.position.y = y;
|
|
this.color.r = Math.random() * 256;
|
|
this.color.g = Math.random() * 256;
|
|
this.color.b = Math.random() * 256;
|
|
this.created = document.timeline.currentTime;
|
|
}
|
|
|
|
get age() {
|
|
return document.timeline.currentTime - this.created;
|
|
}
|
|
|
|
get radius() {
|
|
// radius should be proportional to cube root of mass
|
|
return Math.pow(this.mass / this.density, 1/3);
|
|
}
|
|
|
|
getAcceleration() {
|
|
let ax = 0;
|
|
let ay = 0;
|
|
for (let {x, y} of this.forces) {
|
|
ax += x;
|
|
ay += y;
|
|
}
|
|
return {
|
|
x: ax / this.mass,
|
|
y: ay / this.mass,
|
|
};
|
|
}
|
|
}
|
|
|