the-world-engine
Version:
three.js based, unity like game engine for browser
69 lines (65 loc) • 2.45 kB
JavaScript
import { b2Controller } from "./b2_controller.js";
import { b2_epsilon } from "../common/b2_settings.js";
import { b2Sqrt, b2Vec2 } from "../common/b2_math.js";
export class b2GravityController extends b2Controller {
constructor() {
super(...arguments);
this.G = 1;
this.invSqr = true;
}
Step(t) {
if (this.invSqr) {
for (let t = this.m_bodyList; t; t = t.nextBody) {
const o = t.body;
const s = o.GetWorldCenter();
const n = o.GetMass();
for (let r = this.m_bodyList; r && r !== t; r = r.nextBody) {
const t = r.body;
const c = t.GetWorldCenter();
const i = t.GetMass();
const e = c.x - s.x;
const l = c.y - s.y;
const b = e * e + l * l;
if (b < b2_epsilon) {
continue;
}
const f = b2GravityController.Step_s_f.Set(e, l);
f.SelfMul(this.G / b / b2Sqrt(b) * n * i);
if (o.IsAwake()) {
o.ApplyForce(f, s);
}
if (t.IsAwake()) {
t.ApplyForce(f.SelfMul(-1), c);
}
}
}
} else {
for (let t = this.m_bodyList; t; t = t.nextBody) {
const o = t.body;
const s = o.GetWorldCenter();
const n = o.GetMass();
for (let r = this.m_bodyList; r && r !== t; r = r.nextBody) {
const t = r.body;
const c = t.GetWorldCenter();
const i = t.GetMass();
const e = c.x - s.x;
const l = c.y - s.y;
const b = e * e + l * l;
if (b < b2_epsilon) {
continue;
}
const f = b2GravityController.Step_s_f.Set(e, l);
f.SelfMul(this.G / b * n * i);
if (o.IsAwake()) {
o.ApplyForce(f, s);
}
if (t.IsAwake()) {
t.ApplyForce(f.SelfMul(-1), c);
}
}
}
}
}
Draw(t) {}
}
b2GravityController.Step_s_f = new b2Vec2;