Skip to content

Instantly share code, notes, and snippets.

@Varriount
Last active March 18, 2021 08:00
Show Gist options
  • Save Varriount/16832ce9d8821593f761ae71054566ef to your computer and use it in GitHub Desktop.
Save Varriount/16832ce9d8821593f761ae71054566ef to your computer and use it in GitHub Desktop.
(function (flipX=true, flipY=true){
// Note, Wall PlaceableObjects
const walls = canvas.walls.controlled;
if (walls.length === 0){
return;
}
let xBounds = [walls[0].data.c[0], walls[0].data.c[2]];
let yBounds = [walls[0].data.c[1], walls[0].data.c[3]];
for (let wall of walls){
if (flipX){
xBounds[0] = Math.min(wall.data.c[0]);
xBounds[0] = Math.min(wall.data.c[0]);
xBounds[1] = Math.max(wall.data.c[2]);
xBounds[1] = Math.max(wall.data.c[2]);
}
if (flipY){
yBounds[0] = Math.min(wall.data.c[1]);
yBounds[0] = Math.min(wall.data.c[1]);
yBounds[1] = Math.max(wall.data.c[3]);
yBounds[1] = Math.max(wall.data.c[3]);
}
};
// Flipping a point in local space can be thought of like this:
// For an arbitrary point 'X', we want to transform X such that it is
// "flipped" between the center of the space defined by the minimum and
// maximum bounds.
//
// Now, lets define some terms, and walk through our problem.
//
// Let "input point" be the original point
// Let "result point" be the flipped point.
//
// Let "local center" be the center point between the minimum and
// maximum bounds.
// local center = (minimum bound + maximum bound) / 2
//
// Looking at the input point, we can see that it can be defined using
// the local center and a number, which we will call the "distance".
// input point = local center + distance
//
// The distance can also be defined by rearranging this formula:
// distance = input point - local center
//
// By observing the relationship between the input point, the
// local center, and the result point, we can see that the result point
// is always the same distance from the local center as the input point,
// but the distance is reversed.
// result point = local center + (-1 * distance)
//
// Now that our result point is defined, lets expand our terms.
// result point = local center + (-1 * (input point - local center))
// result point = local center + (-input point + local center))
// result point = local center + local center - input point
// result point = (2 * local center) - input point
// result point = (2 * (minimum bound + maximum bound / 2)) - input point
// result point = (minimum bound + maximum bound) - input point
//
// Thus,
// result point = minimum bound + maximum bound - input point
//
const updates = []
for (let wall of walls){
let points = wall.data.c.slice();
if (flipX){
points[0] = xBounds[0] + xBounds[1] - wall.data.c[0];
points[2] = xBounds[0] + xBounds[1] - wall.data.c[2];
}
if (flipY){
points[1] = yBounds[0] + yBounds[1] - wall.data.c[1];
points[3] = yBounds[0] + yBounds[1] - wall.data.c[3];
}
updates.push({
'_id': wall.data._id,
'c': points
});
};
canvas.scene.updateEmbeddedEntity("Wall", updates);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment