diff --git a/deploy b/deploy
index d2dda9e..6ac6c01 100755
--- a/deploy
+++ b/deploy
@@ -15,4 +15,4 @@ elif [[ $(hostname) == "lentilz" ]]; then
fi
echo >&2
-echo >&2 "Deployed to https://laddhoffman.com/gravity/"
+echo >&2 "Deployed to https://gravity.laddhoffman.com"
diff --git a/object.js b/object.js
index ab403e9..77bd2d7 100644
--- a/object.js
+++ b/object.js
@@ -31,6 +31,9 @@ export class MassObject {
forces = []; // [{x, y}]
history = [];
alive = true;
+ kineticEnergy = 0;
+ workDoneByPointer = 0;
+ workDoneByForces = 0;
currentPosition = undefined;
currentAcceleration = undefined;
@@ -56,7 +59,8 @@ export class MassObject {
color: this.color,
timeCreated: this.timeCreated,
alive: this.alive,
- // TODO: optional export history
+ workDoneByPointer: this.workDoneByPointer,
+ workDoneByForces: this.workDoneByForces,
};
}
@@ -69,8 +73,9 @@ export class MassObject {
this.color = obj.color;
this.timeCreated = obj.timeCreated;
this.alive = obj.alive;
- // TODO: optional import history
this.history = [];
+ this.workDoneByPointer = obj.workDoneByPointer;
+ this.workDoneByForces = obj.workDoneByForces;
}
get age() {
diff --git a/panning.js b/panning.js
index d53463b..6f7dbb2 100644
--- a/panning.js
+++ b/panning.js
@@ -30,13 +30,12 @@ export class Panning {
toJSON() {
return {
velocity: this.velocity,
- paused: this.paused,
};
}
- fromJSON({velocity, paused}) {
+ fromJSON({velocity}) {
this.velocity = copy(velocity);
- this.paused = paused;
+ this.paused = true;
}
handlePointerDown({x, y}) {
diff --git a/pointer.js b/pointer.js
index 2aaeca1..496a51d 100644
--- a/pointer.js
+++ b/pointer.js
@@ -46,10 +46,10 @@ export class Pointer {
});
window.addEventListener('focus', () => {
- console.log('window focus');
+ // console.log('window focus');
});
window.addEventListener('blur', () => {
- console.log('window blur');
+ // console.log('window blur');
});
}
diff --git a/simulator.js b/simulator.js
index d63fea7..7c54673 100644
--- a/simulator.js
+++ b/simulator.js
@@ -141,6 +141,7 @@ export class Sim {
}
fromJSON(state) {
+ this.pause();
this.system.fromJSON(state.system);
this.panning.fromJSON(state.panning);
this.display.fromJSON(state.display);
diff --git a/sync b/sync
index 0825721..9361e58 100755
--- a/sync
+++ b/sync
@@ -16,9 +16,9 @@ do_rsync() {
}
do_rsync ~/code/gravity-dev/ lentilz:code/gravity-dev/
-do_rsync lentilz:code/gravity-dev/ ~/code/gravity-dev/
+# do_rsync lentilz:code/gravity-dev/ ~/code/gravity-dev/
git status
echo >&2
-echo >&2 "Synced with https://laddhoffman.com/gravity-dev/"
+echo >&2 "Synced with https://gravity.dev.laddhoffman.com"
diff --git a/system.js b/system.js
index 90f7317..6699336 100644
--- a/system.js
+++ b/system.js
@@ -68,7 +68,11 @@ export class System {
const obj = this.getSelectedOrCreating();
if (obj === undefined) return;
const start = this.selectedObjectStart;
- obj.position = add(start, sub(r, start.pointer));
+ const delta = sub(r, start.pointer);
+ // TODO: Calculate work done by pointer here?
+ // Either interpolate the acceleration and use m*a, or
+ // measure the change in the object's kinetic energy
+ obj.position = add(start, delta);
obj.velocity = zero;
}
@@ -171,8 +175,8 @@ export class System {
const delta = sub(obj.currentPosition, obj.position);
const netForce = mult(obj.acceleration, obj.mass);
const work = dot(netForce, delta);
- console.log('work', work);
obj.position = obj.currentPosition;
+ obj.workDoneByPointer += work;
}
// Append to object history
@@ -408,11 +412,20 @@ export class System {
// Equal and opposite forces
A.forces.push({x: Fx, y: Fy});
B.forces.push({x: -Fx, y: -Fy});
+ if (A.currentPosition && B.currentPosition) {
+ A.workDoneByForces += dot({x: Fx, y: Fy}, sub(A.position, A.currentPosition));
+ B.workDoneByForces += dot({x: Fx, y: Fy}, sub(B.position, B.currentPosition));
+ }
}, {alive: true, startWith: i + 1});
});
- // Also compute acceleration
+
+ // Additional computations
this.forEachObject(obj => {
+ // Acceleration
obj.acceleration = obj.getAcceleration();
+
+ // Kinetic Energy
+ obj.kineticEnergy = obj.mass * square(obj.velocity) / 2;
});
}
diff --git a/tool/objects.js b/tool/objects.js
index b9c4585..a01a9bb 100644
--- a/tool/objects.js
+++ b/tool/objects.js
@@ -1,6 +1,5 @@
import {hide, show} from '../helper.js';
import {Tool} from '../tool.js';
-import {add, magnitude, sub} from '../vector.js';
export class ObjectsTool extends Tool {
objects = [];
@@ -58,17 +57,22 @@ export class ObjectsTool extends Tool {
obj.objectsToolEl = objectEl;
const {r, g, b} = obj.color;
// Distance from center of screen
- const distance = magnitude(sub(obj.position, add(this.sim.display.viewOrigin, {
- x: this.sim.display.width / 2,
- y: this.sim.display.height / 2,
- })));
- objectEl.innerHTML = `
- ` +
- ' ' +
- `${obj.mass.toPrecision(3)} ` +
- `${distance.toPrecision(3)}`;
+ // const distance = magnitude(sub(obj.position, add(this.sim.display.viewOrigin, {
+ // x: this.sim.display.width / 2,
+ // y: this.sim.display.height / 2,
+ // })));
+ objectEl.innerHTML =
+ ` ` +
+ `Ek: ${obj.kineticEnergy.toFixed(0)}` +
+ `
Wp: ${obj.workDoneByPointer.toFixed(0)}` +
+ `
Wf: ${obj.workDoneByForces.toFixed(0)}`;
+
+ // `${obj.mass.toPrecision(3)} ` +
+ // `${distance.toPrecision(3)}`;
+
// `${magnitude(obj.velocity).toExponential(0)} ` +
// `${-degrees(direction(obj.velocity)).toFixed(0)}°`;
+
if (!obj.hidden) {
show({
items: this.objects,
diff --git a/tool/state.js b/tool/state.js
index e7ab3ba..6c7dd01 100644
--- a/tool/state.js
+++ b/tool/state.js
@@ -56,7 +56,6 @@ export class StateTool extends Tool {
throw new Error('state query parameter does not match digest query parameter');
}
// Tools in this system can be very powerful
- this.sim.pause();
this.sim.fromJSON(state);
}
}
@@ -99,7 +98,6 @@ export class StateTool extends Tool {
load.addEventListener('click', (e) => {
e.preventDefault();
// Tools in this system can wield great power
- this.sim.pause();
this.sim.fromJSON(state);
});