48 lines
1.3 KiB
JavaScript
48 lines
1.3 KiB
JavaScript
import {Tool} from '../tool.js';
|
|
import {
|
|
WIDE_CLASSNAME,
|
|
} from '../config.js';
|
|
|
|
export class UtilityTool extends Tool {
|
|
constructor(toolbar) {
|
|
super(toolbar);
|
|
|
|
const zeroVelocity = document.createElement('button');
|
|
const clearTraces = document.createElement('button');
|
|
|
|
this.div.appendChild(clearTraces);
|
|
this.div.appendChild(zeroVelocity);
|
|
|
|
zeroVelocity.classList.add(WIDE_CLASSNAME);
|
|
clearTraces.classList.add(WIDE_CLASSNAME);
|
|
|
|
zeroVelocity.innerHTML = 'Zero Momentum';
|
|
clearTraces.innerHTML = 'Clear Traces';
|
|
|
|
zeroVelocity.addEventListener('click', () => {
|
|
// Determine center of mass and average momentum
|
|
const { totalMass, netMomentum } = this.sim.objects.computeSystemCenter();
|
|
const netVelocity = {
|
|
x: netMomentum.x / totalMass,
|
|
y: netMomentum.y / totalMass,
|
|
};
|
|
|
|
// Apply offset to all object velocities
|
|
this.sim.objects.forEachObject(obj => {
|
|
obj.velocity.x -= netVelocity.x;
|
|
obj.velocity.y -= netVelocity.y;
|
|
});
|
|
|
|
// Cancel panning
|
|
this.sim.panning = undefined;
|
|
});
|
|
|
|
clearTraces.addEventListener('click', () => {
|
|
// Obliterate object histories
|
|
this.sim.objects.forEachObject(obj => {
|
|
obj.history = [];
|
|
}, {alive: null});
|
|
});
|
|
}
|
|
}
|