manuelVo / foundryvtt-drag-ruler

A Foundry VTT module that shows a ruler when dragging tokens so you can see how far you've dragged them
MIT License
40 stars 55 forks source link

getMovedDistanceFromToken() fails on gridless scenes #200

Closed n0q closed 1 year ago

n0q commented 2 years ago

dragRuler.getMovedDistanceFromToken() only seems to work correctly on gridded scenes. Without a grid, results are somewhat consistent, but seemingly very wrong. Moving east or south by 5 feet measures something close to 3.5, while west or north is more like 7.9.

n0q commented 2 years ago

For what it's worth, this has done well as a hacky temporary workaround.

const movedToken = tokens[0];
const moveHistory = foundry.utils.duplicate(getMovementHistory(movedToken));
moveHistory.push(movedToken.center);
const rays = [];
for (let i = 0; i < moveHistory.length -1; i++) {
    rays.push({ray: new Ray(moveHistory[i], moveHistory[i+1])});
}
const distances = game.canvas.grid.measureDistances(rays, {gridSpaces: true});
const moved = distances.reduce((sum, a) => sum + a, 0);