59 lines
1.6 KiB
JavaScript
59 lines
1.6 KiB
JavaScript
import { Vector } from "./vector.js";
|
|
|
|
export class KepplerObject {
|
|
constructor(name, mass, radius, position, velocity, color) {
|
|
this.mass = mass;
|
|
this.name = name;
|
|
this.radius = radius;
|
|
this.position = position;
|
|
this.velocity = velocity;
|
|
this.color = color;
|
|
this.trails = [];
|
|
}
|
|
|
|
draw(ctx) {
|
|
for (let t of this.trails) {
|
|
ctx.strokeStyle = this.color;
|
|
ctx.moveTo(t.x, t.y);
|
|
ctx.lineTo(t.x + 1, t.y + 1);
|
|
ctx.stroke();
|
|
}
|
|
|
|
ctx.strokeStyle = this.color;
|
|
ctx.fillStyle = this.color;
|
|
ctx.beginPath();
|
|
ctx.arc(this.position.x, this.position.y, this.radius, 0, 2 * Math.PI);
|
|
ctx.fill();
|
|
}
|
|
|
|
track() {
|
|
let maxTrack = 100;
|
|
this.trails.push(this.position.copy());
|
|
|
|
if (this.trails.length > maxTrack) {
|
|
this.trails = this.trails.slice(1, maxTrack);
|
|
}
|
|
}
|
|
|
|
attrackt(kepObj) {
|
|
let f = this.getForce(kepObj);
|
|
|
|
let direction = new Vector(this.position.x - kepObj.position.x, this.position.y - kepObj.position.y).normalize();
|
|
|
|
let velocity = f / this.mass;
|
|
this.velocity.x -= velocity * direction.x;
|
|
this.velocity.y -= velocity * direction.y;
|
|
}
|
|
|
|
move() {
|
|
this.position.x += this.velocity.x;
|
|
this.position.y += this.velocity.y;
|
|
}
|
|
|
|
getForce(o2) {
|
|
let G = 5;
|
|
let dist = this.position.getDistance(o2.position);
|
|
let f = G * (this.mass * o2.mass) / (dist * dist);
|
|
return f;
|
|
}
|
|
} |