General improvements
I forgot what I actually changed. It may not even be playable, I just want to get this up there.
This commit is contained in:
parent
8a0bf0ada9
commit
c73130e3ff
25 changed files with 584 additions and 274 deletions
|
@ -72,13 +72,15 @@ export default class Body {
|
|||
}
|
||||
|
||||
tickGravity(bodies, speed = 1) {
|
||||
bodies.forEach(b => {
|
||||
let force = b.mass / (this.distanceTo(b) ** 2) * G;
|
||||
let [[ax, ay], [bx, by]] = [this.com, b.com];
|
||||
for (let body of bodies) {
|
||||
const distanceSquared = this.distanceToSquared(body);
|
||||
if (distanceSquared > (1000 ** 2)) continue;
|
||||
let force = body.mass / distanceSquared * G;
|
||||
let [[ax, ay], [bx, by]] = [this.com, body.com];
|
||||
let angle = Math.atan2(by - ay, bx - ax);
|
||||
this.xvel += Math.cos(angle) * force * speed;
|
||||
this.yvel += Math.sin(angle) * force * speed;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
distanceTo(body) {
|
||||
|
@ -87,6 +89,12 @@ export default class Body {
|
|||
((by - ay) ** 2)), 1);
|
||||
}
|
||||
|
||||
distanceToSquared(body) {
|
||||
let [[ax, ay], [bx, by]] = [this.com, body.com];
|
||||
return Math.max(((bx - ax) ** 2) +
|
||||
((by - ay) ** 2), 1);
|
||||
}
|
||||
|
||||
angleTo(ax, ay, bx, by) {
|
||||
return Math.atan2(by - ay, bx - ax);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue