Notifications
Clear all
[Closed] Simple divide Box in to 8 smaller Cubes?
Aug 28, 2016 4:36 am
Hi, I’m trying to do a simple subdivide cube by 2 aka creating 8 smaller cubes that are 1/2 size of the original selected cube, and I want to do it in the simplest way possible, basically like the code that I have made but working regardless where the center pos of the selected cube is.
(It’s a cube so all sides will always be equal length)
fn fn_createBox inCenter inSize = ( -- create the smaller box
local theBox = box length:inSize width:inSize height:inSize wirecolor:[160,160,160]
theBox.center = inCenter
)
fn fn_divideBox inCenter inSize = ( -- the divide function
local halfSize = (inSize / 2.0)
local halfCenter = (inCenter /2.0)
for z = 1 to 2 do (
for y = 1 to 2 do (
for x = 1 to 2 do (
fn_createBox [(inCenter.x * x) - halfCenter.x ,(inCenter.y * y) - halfCenter.y,(inCenter.z * z) - halfCenter.z] halfSize
)
)
)
)
box name:"RootBox" length:1 width:1 height:1 wirecolor:[255,255,0] pos:[0.5,0.5,0] --creates the main cube to be divided
select $RootBox
fn_divideBox $.center $.length -- runs the divide function
thanks in advance.
1 Reply
Aug 28, 2016 4:36 am
Here is one solution I came up with that works but feels like it maybe could be cleaned up slightly:
fn fn_createBox inCenter inSize = ( -- create the smaller box
local theBox = box length:inSize width:inSize height:inSize wirecolor:[160,160,160]
theBox.center = inCenter
)
fn fn_divideBox inCenter inSize = ( -- the divide function
local halfSize = (inSize / 2.0)
local qSize = (inSize / 4.0)
for z = 1 to 2 do (
for y = 1 to 2 do (
for x = 1 to 2 do (
fn_createBox (inCenter + [(halfSize* x) - qSize,(halfSize * y) - qSize,(halfSize * z) - qSize] - halfSize) halfSize
)
)
)
)
box name:"RootBox" length:1 width:1 height:1 wirecolor:[255,255,0] pos:[(random -10 10),(random -10 10),(random -10 10)] --creates the main cube at random pos to be divided
select $RootBox
fn_divideBox $.center $.length -- runs the divide function