-
-
Save copygirl/4eba97936dd02182e6eb1af155b1943a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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