Author Topic: Constructing a hollow sphere out of bricks  (Read 2992 times)

I'm helping out a friend with a mining mod not all that different from Red_Guy's. Both servers use an incredibly inefficient system for bombs, which basically clears out a hollow sphere in a given radius of 4x4 blocks. They all repeatedly clear out the bricks around the area in a larger and larger radius until you're left with a hollow sphere. This takes 4n3 brick placements and deletions.

Instead of doing that, the only logical way of doing it is to first clear out all of the bricks in the blast radius and then construct the hollow sphere all as one, which would only take 12n2 operations, a significant improvement.

Is there any way to construct the sphere of what would be 4x4 cubes, without having to rely on O(n3) loops or algorithms?

This seems to work pretty well after brief testing.

Code: [Select]
function drawSphere(%origin, %r, %m, %n)
{
%end = %n-1;
for(%m0 = 0; %m0 <= %m; %m0++)
{
%m1 = mSin($pi * %m0 / %m);
%m2 = mCos($pi * %m0 / %m);

for(%n0 = 0; %n0 <= %end; %n0++)
{
%n1 = mCos(2 * $pi * %n0 / %n);
%n2 = mSin(2 * $pi * %n0 / %n);

%pos = %r * (%m1 * %n1) SPC %r * (%m1 * %n2) SPC %r * %m2;

plant(VectorAdd(%origin, %pos));
}
}
}

I imagine there's probably a way to optimise it a bit more; I guess you'll probably have to modify it for your purpose as well.

source (explains use as well): http://stackoverflow.com/questions/4081898/procedurally-generate-a-sphere-mesh

This seems to work pretty well after brief testing.

Code: [Select]
function drawSphere(%origin, %r, %m, %n)
{
%end = %n-1;
for(%m0 = 0; %m0 <= %m; %m0++)
{
%m1 = mSin($pi * %m0 / %m);
%m2 = mCos($pi * %m0 / %m);

for(%n0 = 0; %n0 <= %end; %n0++)
{
%n1 = mCos(2 * $pi * %n0 / %n);
%n2 = mSin(2 * $pi * %n0 / %n);

%pos = %r * (%m1 * %n1) SPC %r * (%m1 * %n2) SPC %r * %m2;

plant(VectorAdd(%origin, %pos));
}
}
}

I imagine there's probably a way to optimise it a bit more; I guess you'll probably have to modify it for your purpose as well.

source (explains use as well): http://stackoverflow.com/questions/4081898/procedurally-generate-a-sphere-mesh
Awesome. Ima try out different M and N variables.


This seems to work pretty well after brief testing.

snip

The math isn't actually that complicated (not insulting Ipquarx at all) and is pretty easy to understand. Sine and Cosine return a circular shaped wave, but cosine is shifted forward a bit. Sine starts at 0 and cos starts at 1, so for degree 0 (straight up) you take the X value of sine and the Y value of cosine (0 and 1 respectively) and put a point there.  For brevity's sake, skip to 45 degrees (half way to pointing right) and you get 0.71 for X and 0.71 for Y too. Easy enough, that makes sense. Skip to 90 degrees, perfectly right, and you get 1 for sine and 0 for cosine, so you place a brick at Y = 1 and x = 0. Plotted out, this looks like:



It's already starting to look like a circle with only 3 points.





"Plant" is a made up function. It's implied that you're supposed to write your own plant function since it'll be different for every single use scenario.
« Last Edit: July 21, 2014, 03:37:53 PM by $trinick »

Ya know, I can't say I ever learned how to plant a brick through script. I know you can do something like %brick = new fxDtsBrick() then missionCleanup.add(%brick); but I believe that spawns ghost bricks if I'm not mistaken.

Ya know, I can't say I ever learned how to plant a brick through script. I know you can do something like %brick = new fxDtsBrick() then missionCleanup.add(%brick); but I believe that spawns ghost bricks if I'm not mistaken.
Not sure, but I think this is it:

%brick = new fxDTSBrick()
{
    position = "0 0 0";
    datablock = "brick2x2Data";
    isPlanted = 1;
};

%brick.plant();
%brick.setTrusted(1);
brickGroup_YOURBLIDHERE.add(%brick);

I'm probably missing a few things as I can't actually test right now, but I think this works.

Cosin


Sin and Cos are abbreviations of sine and cosine respectively.

Cosin isn't a mathematical term - in fact, it appears to be an alternate spelling of cousin.

I mean, you're being just a little bit picky but you're right; I don't know why I wrote Cosin instead of Cos. I think I was trying to be more clear about the relationship between the two or something, but I don't remember why I didn't expand them fully to sine and cosine.
« Last Edit: July 21, 2014, 03:39:50 PM by $trinick »