pointer history size

This commit is contained in:
Ladd 2025-12-28 19:52:17 -06:00
parent 25796a5ec3
commit 1da0be3657
4 changed files with 12 additions and 10 deletions

View File

@ -29,7 +29,7 @@ export const PATH_TRACES_DASHED_OPACITY = 1.0;
// SCALING FACTORS // SCALING FACTORS
export const MASS_CREATION_RATE = 1E1; export const MASS_CREATION_RATE = 1E1;
export const POINTER_HISTORY_SIZE = 15; export const POINTER_HISTORY_SIZE = 10;
export const MOTION_TIME_SCALE = 1E-4; export const MOTION_TIME_SCALE = 1E-4;
export const PAN_ACCELERATION = 1E1; export const PAN_ACCELERATION = 1E1;
export const ARROWHEAD_LENGTH = 7; export const ARROWHEAD_LENGTH = 7;

View File

@ -143,18 +143,18 @@ export class Objects {
handlePointerMove({x, y, vx, vy}) { handlePointerMove({x, y, vx, vy}) {
// If the cursor moves while creating an object, or while an object is selected, // If the cursor moves while creating an object, or while an object is selected,
// update the position and velocity of the object // update the position and velocity of the object
// update the position using the pointer motion but the velocity using the pointer velocity
const obj = this.getSelectedOrCreating(); const obj = this.getSelectedOrCreating();
if (obj === undefined) return;
if (this.sim.pointer.panning?.velocity) { if (this.sim.pointer.panning?.velocity) {
vx += this.sim.pointer.panning.velocity.x; vx += this.sim.pointer.panning.velocity.x;
vy += this.sim.pointer.panning.velocity.y; vy += this.sim.pointer.panning.velocity.y;
} }
if (obj !== undefined) {
obj.position.x = x; obj.position.x = x;
obj.position.y = y; obj.position.y = y;
obj.velocity.x = vx; obj.velocity.x = vx;
obj.velocity.y = vy; obj.velocity.y = vy;
} }
}
// cb: (obj, idx) => {} // cb: (obj, idx) => {}
// TODO: Reducer // TODO: Reducer

View File

@ -59,12 +59,12 @@ export class Pointer {
}); });
} }
getPointerVelocity(points) { getPointerVelocity(points = POINTER_HISTORY_SIZE) {
// Average over pointer history // Average over pointer history
if (this.pointerHistory.length < 2) { if (this.pointerHistory.length < 2) {
return {x: 0, y: 0, dt: 1}; return {x: 0, y: 0, dt: 1};
} }
points = points || this.pointerHistory.length; points = Math.min(points, POINTER_HISTORY_SIZE, this.pointerHistory.length);
const start = this.pointerHistory[this.pointerHistory.length - points]; const start = this.pointerHistory[this.pointerHistory.length - points];
const end = this.pointerHistory[this.pointerHistory.length - 1]; const end = this.pointerHistory[this.pointerHistory.length - 1];
const dt = (end.t - start.t) / 1000; const dt = (end.t - start.t) / 1000;
@ -122,7 +122,7 @@ export class Pointer {
if (!dt) { if (!dt) {
this.panning = undefined; this.panning = undefined;
} else { } else {
const v = this.getPointerVelocity(10); const v = this.getPointerVelocity();
// Convert pointer velocity to simulation scale // Convert pointer velocity to simulation scale
v.x /= this.sim.display.scale; v.x /= this.sim.display.scale;
v.y /= this.sim.display.scale; v.y /= this.sim.display.scale;

2
sync
View File

@ -18,5 +18,7 @@ do_rsync() {
do_rsync ~/code/gravity-dev/ lentilz:code/gravity-dev/ 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
echo >&2 "Synced with https://laddhoffman.com/gravity-dev/" echo >&2 "Synced with https://laddhoffman.com/gravity-dev/"