Skip to content

Instantly share code, notes, and snippets.

@copygirl
Created October 13, 2023 15:25
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/607ed2d28bb26d7e21dcd1b1b9e0ede6 to your computer and use it in GitHub Desktop.
Save copygirl/607ed2d28bb26d7e21dcd1b1b9e0ede6 to your computer and use it in GitHub Desktop.
-- Gets the active `selection_box` for the specified `node` which
-- has abs `paramtype2` of `facedir`, based on its `param2` value.
local function get_node_active_selection_box(node)
local def = minetest.registered_nodes[node.name]
local box = def.selection_box and def.selection_box.fixed
if not box then box = DEFAULT_SELECTION_BOX
elseif box[1] then box = box[1] end
box = table_copy(box) -- Don't modify the original.
local param2 = node.param2
if param2 == 0 then return box end
local axis = ({ 5, 6, 3, 4, 1, 2 })[math_floor(param2 / 4) + 1]
local other_axis_1, other_axis_2 = (axis % 3) + 1, ((axis + 1) % 3) + 1
local rotation = (param2 % 4) / 2 * math_pi
local flip = axis > 3
if flip then axis = axis - 3; rotation = -rotation end
local sin, cos = math_sin(rotation), math_cos(rotation)
if axis == 2 then sin = -sin end
for _, off in ipairs({ 0, 3 }) do
local axis_1, axis_2 = other_axis_1 + off, other_axis_2 + off
local value_1, value_2 = box[axis_1], box[axis_2]
box[axis_1] = value_1 * cos - value_2 * sin
box[axis_2] = value_1 * sin + value_2 * cos
end
if not flip then box[axis], box[axis + 3] = -box[axis + 3], -box[axis] end
for _, coord in ipairs({ other_axis_1, other_axis_2 }) do
if box[coord] > box[coord + 3] then
box[coord], box[coord + 3] = box[coord + 3], box[coord]
end
end
return box
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment