Skip to content

Instantly share code, notes, and snippets.

@copygirl
Last active October 13, 2023 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save copygirl/4eba97936dd02182e6eb1af155b1943a to your computer and use it in GitHub Desktop.
Save copygirl/4eba97936dd02182e6eb1af155b1943a to your computer and use it in GitHub Desktop.
-- Axis lookup table based on how Minetest's `facedir` operates.
local AXIS_LOOKUP = {
vector.new( 0, 1, 0), -- +Y
vector.new( 0, 0, 1), -- +Z
vector.new( 0, 0, -1), -- -Z
vector.new( 1, 0, 0), -- +X
vector.new(-1, 0, 0), -- -X
vector.new( 0, -1, 0), -- -Y
}
-- Lookup table to find out how to rotate for each `facedir` axis.
local AXIS_ROTATION = {
nil,
{ vector.new(1, 0, 0), 90 },
{ vector.new(1, 0, 0), -90 },
{ vector.new(0, 0, 1), -90 },
{ vector.new(0, 0, 1), 90 },
{ vector.new(0, 0, 1), 180 },
}
local function rotate_box_by_facedir(box, facedir)
if facedir == 0 then return end
local axis_index = math.floor(facedir / 4)
if axis_index ~= 0 then
local axis, degrees = unpack(AXIS_ROTATION[1 + axis_index])
box.min = box.min:rotate_around_axis(axis, math.rad(degrees))
box.max = box.max:rotate_around_axis(axis, math.rad(degrees))
end
local axis_rot = facedir % 4
if axis_rot ~= 0 then
local axis = AXIS_LOOKUP[1 + axis_index]
local degrees = axis_rot * 90
box.min = box.min:rotate_around_axis(axis, math.rad(degrees))
box.max = box.max:rotate_around_axis(axis, math.rad(degrees))
end
-- Recalculate the proper minimum and maximum bounds, since we just rotated these vectors.
box.min.x, box.max.x = math.min(box.min.x, box.max.x), math.max(box.min.x, box.max.x)
box.min.y, box.max.y = math.min(box.min.y, box.max.y), math.max(box.min.y, box.max.y)
box.min.z, box.max.z = math.min(box.min.z, box.max.z), math.max(box.min.z, box.max.z)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment