the-world-engine
Version:
three.js based, unity like game engine for browser
44 lines (40 loc) • 1.36 kB
JavaScript
import { b2Controller } from "./b2_controller.js";
import { b2Mat22, b2Vec2, b2Max } from "../common/b2_math.js";
import { b2_epsilon } from "../common/b2_settings.js";
export class b2TensorDampingController extends b2Controller {
constructor() {
super(...arguments);
this.T = new b2Mat22;
this.maxTimestep = 0;
}
Step(t) {
let e = t.dt;
if (e <= b2_epsilon) {
return;
}
if (e > this.maxTimestep && this.maxTimestep > 0) {
e = this.maxTimestep;
}
for (let t = this.m_bodyList; t; t = t.nextBody) {
const o = t.body;
if (!o.IsAwake()) {
continue;
}
const s = o.GetWorldVector(b2Mat22.MulMV(this.T, o.GetLocalVector(o.GetLinearVelocity(), b2Vec2.s_t0), b2Vec2.s_t1), b2TensorDampingController.Step_s_damping);
o.SetLinearVelocity(b2Vec2.AddVV(o.GetLinearVelocity(), b2Vec2.MulSV(e, s, b2Vec2.s_t0), b2Vec2.s_t1));
}
}
Draw(t) {}
SetAxisAligned(t, e) {
this.T.ex.x = -t;
this.T.ex.y = 0;
this.T.ey.x = 0;
this.T.ey.y = -e;
if (t > 0 || e > 0) {
this.maxTimestep = 1 / b2Max(t, e);
} else {
this.maxTimestep = 0;
}
}
}
b2TensorDampingController.Step_s_damping = new b2Vec2;