#[RESOLVED] How can i make a Sphere made of blocks with radius (WorldEdit)
1 messages · Page 1 of 1 (latest)
iterate over all blocks and see if it's in a radius from the spheres center
The easiest way
pseudocode
iterate over all positions from -r,-r,-r to r,r,r {
if x,y,z distance to center is < r {
world.getBlock().setType or smth
}
}
pretty sure you can have it a lil more optimised but don't think it's worth it
use runjob for that
I still don't know how to do it :/
the distance you calculate using the formula sqrt((x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2)
and the iterating is just a for loop on all coordinates from center + (-r,-r,-r) to center + (r,r,r)
Oh thanks, thats now clear to me!
function createSphere(center, radius, blockType, dimension) {
const { x: cx, y: cy, z: cz } = center;
for (let x = -radius; x <= radius; x++) {
for (let y = -radius; y <= radius; y++) {
for (let z = -radius; z <= radius; z++) {
const dx = x;
const dy = y;
const dz = z;
const distance = Math.sqrt(dx * dx + dy * dy + dz * dz);
if (distance <= radius) {
const blockPos = {
x: cx + x,
y: cy + y,
z: cz + z
};
try {
dimension.getBlock(blockPos).setType(blockType);
} catch (e){}
}
}
}
}
}
Can you check if that function is right?
should be fine, yeah
function* createSphere(center, radius, blockType, dimension) {
const { x: cx, y: cy, z: cz } = center;
for (let x = -radius; x <= radius; x++) {
for (let y = -radius; y <= radius; y++) {
for (let z = -radius; z <= radius; z++) {
const distance = Math.sqrt(x * x + y * y + z * z);
if (distance > radius) continue;
yield dimension.getBlock({
x: cx + x,
y: cy + y,
z: cz + z
}).setType(blockType);
}
}
}
}
Final code.
Post resolved ✅