Jump to content

[1.7.10] Generating a sphere


The_SlayerMC

Recommended Posts

Hey, im trying to generate spheres for my dimension, but i cant quite get the sphere shape, all i can get is this: http://prntscr.com/4xam3y

 

As you can tell, its making a circle shape but its turning out to be just a 1x block thick platform

 

This is the code i wrote:

int radius = 8;
		for(float i = 0; i < radius; i += 0.5) {
			for(float j = 0; j < 2 * Math.PI * i; j += 0.5)
				w.setBlock((int)Math.floor(x + Math.sin(j) * i), y + radius, (int)Math.floor(z + Math.cos(j) * i), Blocks.bedrock);
		}

 

Whats the most efficient way to generate a sphere (a whole depth sphere)

Former developer for DivineRPG, Pixelmon and now the maker of Essence of the Gods

Link to comment
Share on other sites

Hi

 

The easiest way is to iterate over a cube  and for each [x,y,z], calculate the squared distance to the centre of the circle.  If it's less than the squared radius, it's in the sphere.  If more, it's outside.

 

eg

for x = centre minus radius to centre plus radius

  for y = centre minus radius to centre plus radius

    for z = centre minus radius to centre plus radius {

squareDistance = (x-centre)^2 + (y-centre)^2 + (z-centre)^2

if squareDistance <= (sphere radius)^2 then set block stone

 

}

 

Don't worry about being efficient.  Java is so fast you'll never notice the difference no matter how much you optimise it.

 

 

-TGG

Link to comment
Share on other sites

Don't worry about being efficient.  Java is so fast you'll never notice the difference no matter how much you optimise it.

 

I used to think that, but I've been doing a lot with structure generation lately and found that with very simple code (just loop through an array and place bocks indicated) it can create perceptible lag on structures of any decent size.  I actually changed my code to only do one layer per tick in order to improve this.

 

I think a sphere the size he's showing could actually show a bit of lag.  Generally though that won't be too noticeable if it happens during chunk generation because players are used to some lag from that, but if you generate the structure in response to player action (like using a tool), it can be noticeable.

 

Anyway, I was surprised that it could lag, but I think the thing is that the volume of a structure gets large very fast -- a sphere as large as shown in the OP's picture has 5000 blocks!  And I think the block placement requires fair bit of processing as well as client server sync notification.  Just giving my observation...

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

I agree a bit, using the code he provided I tried to create a 15 radius sphere, took 2 secs to generate. But yeah, even with the most optimized code snippet ever, that amount of blocks is still going to take a bit to generate.. this is what I think of it, so yeah, you both are right

I try my best, so apologies if I said something obviously stupid!

Link to comment
Share on other sites

I agree a bit, using the code he provided I tried to create a 15 radius sphere, took 2 secs to generate. But yeah, even with the most optimized code snippet ever, that amount of blocks is still going to take a bit to generate.. this is what I think of it, so yeah, you both are right

 

Yeah. What I ended up doing was to actually have my Y index increment over each tick, rather than loop through all Y in one tick.  That kind of spreads the lag.  I previously made it only do one dimension at a time per tick (i.e. both the Y and Z loops were incremented over ticks rather than within a tick) but at that point the total time to make the structure gets pretty long -- a 20 diameter sphere would take 400 ticks = 20 seconds.  Which was too long, but the good thing was there was no lag at all.  So it seems to be a balance.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Noob question, I haven't tried this before: could you generate a structure in a separate thread?

 

I think I tried that and got concurrency errors.  I'm not expert at threads, but unless you design the threads to work together you get into trouble if two threads are trying to operate on the same data -- i.e. think of one thread trying to read the array of the blocks in the world at the same time the other thread is trying to place a new block.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Hi

 

The easiest way is to iterate over a cube  and for each [x,y,z], calculate the squared distance to the centre of the circle.  If it's less than the squared radius, it's in the sphere.  If more, it's outside.

 

eg

for x = centre minus radius to centre plus radius

  for y = centre minus radius to centre plus radius

    for z = centre minus radius to centre plus radius {

squareDistance = (x-centre)^2 + (y-centre)^2 + (z-centre)^2

if squareDistance <= (sphere radius)^2 then set block stone

 

}

 

Don't worry about being efficient.  Java is so fast you'll never notice the difference no matter how much you optimise it.

 

 

-TGG

 

Thanks! But can i make it so it generates random sized ones aswell?

Former developer for DivineRPG, Pixelmon and now the maker of Essence of the Gods

Link to comment
Share on other sites

I tried this just for a normal one:

 

int radius = 8;
		int centre = radius / 2, squareDistance = 0;
		for(x = centre - radius; x < centre + radius; x++)
			for(y = centre - radius; y < centre + radius; y++)
				for(z = centre + radius; z < centre + radius; z++) {
					squareDistance = (x-centre)^2 + (y-centre)^2 + (z-centre)^2;
					/*if(squareDistance <= (radius)^2)*/ w.setBlock(x, y, z, Blocks.bedrock);

				}

 

But nothing is seeming to generate at all

Former developer for DivineRPG, Pixelmon and now the maker of Essence of the Gods

Link to comment
Share on other sites

Don't worry about being efficient.  Java is so fast you'll never notice the difference no matter how much you optimise it.

 

I used to think that, but I've been doing a lot with structure generation lately and found that with very simple code (just loop through an array and place bocks indicated) it can create perceptible lag on structures of any decent size.  I actually changed my code to only do one layer per tick in order to improve this.

 

I think a sphere the size he's showing could actually show a bit of lag.  Generally though that won't be too noticeable if it happens during chunk generation because players are used to some lag from that, but if you generate the structure in response to player action (like using a tool), it can be noticeable.

 

Anyway, I was surprised that it could lag, but I think the thing is that the volume of a structure gets large very fast -- a sphere as large as shown in the OP's picture has 5000 blocks!  And I think the block placement requires fair bit of processing as well as client server sync notification.  Just giving my observation...

Hi Jabelar

I agree with you, it will take a long time to place that many blocks.  I'm just about to release a mod that copies up to 256x256x256 blocks and it can take up to a minute or more.

 

What I meant was - I've seen other folks try to optimise out some of the "squareDistance" checks.  That's a total waste of mental effort.

 

-TGG

 

 

Link to comment
Share on other sites

I tried this just for a normal one:

 

int radius = 8;
		int centre = radius / 2, squareDistance = 0;
		for(x = centre - radius; x < centre + radius; x++)
			for(y = centre - radius; y < centre + radius; y++)
				for(z = centre + radius; z < centre + radius; z++) {
					squareDistance = (x-centre)^2 + (y-centre)^2 + (z-centre)^2;
					/*if(squareDistance <= (radius)^2)*/ w.setBlock(x, y, z, Blocks.bedrock);

				}

 

But nothing is seeming to generate at all

 

Ah, I think I might have confused you.

 

When I wrote ^2 I meant 'squared', but that's not what the ^ operator does in Java.

 

Try this instead

squareDistance = (x-centre)*(x-centre) + (y-centre)*(y-centre) + (z-centre)*(z-centre);

 

You've got a typo here which is probably causing your problem

 

for(z = centre + radius

 

-TGG

 

Link to comment
Share on other sites

So now i've got this:

 

int radius = 8;
		int centre = radius / 2, squareDistance = 0;
		for(x = centre - radius; x < centre + radius; x++)
			for(y = centre - radius; y < centre + radius; y++)
				for(z = centre - radius; z < centre + radius; z++) {
					squareDistance = (x-centre)*(x-centre) + (y-centre) * (y-centre) + (z-centre) * (z-centre);
					if(squareDistance <= radius) w.setBlock(x, y, z, Blocks.bedrock);

				}

 

And it's still not making any sort of generation, even if i remove the 'if' statment

Former developer for DivineRPG, Pixelmon and now the maker of Essence of the Gods

Link to comment
Share on other sites

Wow, i derped really hard ahhaha....

 

So now im using this:

 

int radius = 8;
		int centre = radius / 2, squareDistance = 0;
		int x1, y1, z1;
		for(x1 = centre - radius; x1 < centre + radius; x1++)
			for(y1 = centre - radius; y1 < centre + radius; y1++)
				for(z1 = centre - radius; z1 < centre + radius; z1++) {
					squareDistance = (x-centre) * (x-centre) + (y-centre) * (y-centre) + (z-centre) * (z-centre);
					/*if(squareDistance <= (radius) * (radius))*/ w.setBlock(x + x1, y + y1, z + z1, Blocks.bedrock);
				}

 

And its generating a big square

Former developer for DivineRPG, Pixelmon and now the maker of Essence of the Gods

Link to comment
Share on other sites

  • 7 years later...

If anyone needs a working one for 1.18:

int radius = 9;
int centre = radius / 2, squareDistance;
int x1, y1, z1;

for(x1 = centre - radius; x1 < centre + radius; x1++){
	for(y1 = centre - radius; y1 < centre + radius; y1++){
		for(z1 = centre - radius; z1 < centre + radius; z1++) {
			squareDistance = (x1 -centre) * (x1 -centre) + (y1-centre) * (y1-centre) + (z1-centre) * (z1-centre);
			if(squareDistance <= (radius) * (radius))
				context.level().setBlock(new BlockPos(x + x1, y + y1, z + z1), Blocks.BEDROCK.defaultBlockState(), 11);
		}
	}
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • DAFTAR KLIK DISINI DAFTAR KLIK DISINI SENJU33☘️ Depo 100 Bonus 100 Link Alternatif Bonus 100% Senju33. SENJU33, seperti kakek Zeus yang berkuasa, menonjolkan dirinya sebagai pemimpin dalam industri perjudian online. Fitur RTP (Return to Player) yang disediakan oleh SENJU33 mencerminkan komitmen untuk memberikan peluang yang adil kepada pemainnya, sebagaimana Ouranos memberikan keadilan di dunia mitologi. Senju33 dengan Akses Login Situs SENJU33 Slot Gacor Hari Ini Gampang menang tanpa pola rtp slot, ada bonus untuk member baru mendaftar di situs senju33 slot gacor hari ini 30%, mencari pengalaman bermain slot yang tak terlupakan di Indonesia? SENJU33 adalah jawabannya! Sebagai situs super gacor paling terdepan di tahun ini, SENJU33 menawarkan ID VIP Pro, akun resmi, dan link login terkini untuk memastikan Anda meraih pengalaman bermain slot yang luar biasa.
    • SUHU77 Situs Cara Deposit Lewat Dana Merupakan Situs Dengan Permainan Terlengkap Dan Login & Daftar Termudah Yang Bisa Anda Gunakan. Selain Itu, Untuk Deposit Juga Sangat Mudah Dan Lengkap Semua Payment Di Terima Dari Deposit Bank Local, E-Wallet / E-Money (Dana, Ovo, Gopay, Shopeepay, Sakuku Dll), Deposit Pulsa Tanpa Potongan Terlengkap DAFTAR KLIK DISINI DAFTAR KLIK DISINI Untuk Bermain Disini Sangat Lah Mudah Dan Untuk Situs Juga Sudah Di Percaya Sejak Lama. Menang Berapapun Pasti Di Bayar Lunas Tanpa Pending. Jadi Jangan Lewatkan Bermain Di SUHU77 Situs Cara Deposit Lewat Dana. Sistem Permainan Yang Ditawarkan Telah Mendapatkan Kepercayaan Dari Berbagai Member Yang Sudah Mendapatkan Keuntungan Dari Hal Itu Yang Berasal Dari Permainan Slot Online. SUHU77 SUHU77 Login SUHU77 Daftar SUHU77 Rtp Slot Scatter SUHU77 Link Alternatif SUHU77 Agen SUHU77 Situs SUHU77
    • OSG888 Merupakan Link Slot Gacor OSG888 Terbaru Dan Gampang Menang Dengan Minimal Deposit 10,000 Dapat Dimainkan Semua Jenis Permainan ▶️▶️▶️ DAFTAR KLIK DI SINI ◀️◀️◀️ ▶️▶️▶️ DAFTAR KLIK DI SINI ◀️◀️◀️ ▶️▶️▶️ DAFTAR KLIK DI SINI ◀️◀️◀️ Dalam dunia taruhan online yang semakin berkembang, mencari platform yang dapat diandalkan dan menyenangkan untuk bermain merupakan hal yang sangat penting bagi para pecinta taruhan. Salah satu opsi teratas yang muncul adalah OSG888 dan jaringan link alternatifnya yang menyediakan tingkat pengalaman taruhan yang tinggi. Mari kita gali lebih dalam tentang apa yang membuat OSG888 dan daftar situs link alternatif OSG888 RTP begitu menarik bagi para penggemar taruhan online. 1. Keandalan dan Kredibilitas OSG888 dikenal karena keandalan dan kredibilitasnya. Dengan lisensi resmi dan regulasi yang ketat, situs ini menawarkan lingkungan taruhan yang aman dan terjamin bagi para pemainnya. Pengguna dapat yakin bahwa setiap taruhan yang mereka tempatkan dilakukan dalam kerangka yang adil dan teratur. 2. Ragam Permainan yang Luas Salah satu daya tarik utama dari OSG888 adalah ragam permainan yang ditawarkan. Mulai dari slot online yang menghibur hingga permainan meja klasik seperti blackjack dan roulette, para pemain memiliki banyak pilihan untuk memilih permainan sesuai dengan preferensi mereka. Selain itu, fitur live casino juga menambahkan dimensi interaktif yang memungkinkan pemain untuk merasakan pengalaman kasino langsung dari kenyamanan rumah mereka. 3. Tingkat Pengembalian (RTP) yang Kompetitif OSG888 menawarkan tingkat pengembalian (RTP) yang kompetitif untuk sebagian besar permainannya. Ini berarti bahwa pemain memiliki peluang lebih baik untuk meraih kemenangan dan menghasilkan keuntungan dari taruhan mereka. Tingkat pengembalian yang tinggi juga menambah keseruan permainan dan membuat pengalaman taruhan menjadi lebih memuaskan. 4. Kemudahan Akses melalui Link Alternatif OSG888 menyediakan daftar situs link alternatif OSG888 RTP, yang memungkinkan para pemain untuk mengakses situs tersebut dengan mudah bahkan jika situs utama mereka diblokir oleh penyedia layanan internet tertentu. Ini memberikan fleksibilitas tambahan bagi para pemain untuk terus menikmati pengalaman taruhan mereka tanpa hambatan. 5. Promosi dan Bonus yang Menarik Situs ini juga terkenal dengan berbagai promosi dan bonus yang menarik bagi para pemainnya. Mulai dari bonus selamat datang hingga promosi loyalitas, ada banyak kesempatan untuk meningkatkan pengalaman taruhan Anda dan menghasilkan keuntungan tambahan dari bermain di OSG888. Kesimpulan OSG888 dan daftar situs link alternatif OSG888 RTP menawarkan pengalaman taruhan online yang luar biasa bagi para penggemar taruhan di seluruh dunia. Dengan keandalan, ragam permainan, tingkat pengembalian yang kompetitif, dan promosi menarik, tidak mengherankan bahwa OSG888 telah menjadi salah satu destinasi utama bagi para pecinta taruhan online. Bagi mereka yang mencari platform yang dapat diandalkan dan menyenangkan untuk bermain, OSG888 adalah pilihan yang sulit untuk dikalahkan Keyword Terkait : OSG888 OSG888 Slot OSG888 Daftar OSG888 Login OSG888 Link Alternatif OSG888 RTP AGEN OSG888 SITUS OSG888
    • Selamat datang di PROTOGEL salah satu situs slot gacor gampang menang hari ini di Indonesia yang sangat menjajikan. Slot gacor adalah adalah suatu istilah yang digunakan untuk menjelaskan sebuah permainan slot gampang menang di situs slot online. Situs slot gacor PROTOGEL ini bisa menjadi populer walaupun terbilang baru karena RTP slot online yang disajikan begitu tinggi. Seiring dengan perkembangan zaman situs slot gacor terbaru ini juga sudah update dari segi teknologi yang menggunakan HTML5, inilah yang membuat grafis permainan terlihat begitu modern, audio lebih jernih, dan user interface yang smooth. Tidak dipungkiri grafis yang kami memiliki sudah menarik banyak sekali pendatang baru yang ingin merasakan terbawa dalam suasana tema permainan mesin slot. Kehadiran slot gacor menjadi angin segar bagi para pecinta judi online, memberikan alternatif permainan yang seru dan menguntungkan. Tak heran jika popularitas slot gacor terus meningkat, menarik minat para pemain baru untuk mencoba peruntungan mereka di situs slot gacor hari ini PROTOGEL.
    • Selamat datang di DEWAGACOR salah satu situs slot gacor gampang menang hari ini di Indonesia yang sangat menjajikan. Slot gacor adalah adalah suatu istilah yang digunakan untuk menjelaskan sebuah permainan slot gampang menang di situs slot online. Situs slot gacor DEWAGACOR ini bisa menjadi populer walaupun terbilang baru karena RTP slot online yang disajikan begitu tinggi. Seiring dengan perkembangan zaman situs slot gacor terbaru ini juga sudah update dari segi teknologi yang menggunakan HTML5, inilah yang membuat grafis permainan terlihat begitu modern, audio lebih jernih, dan user interface yang smooth. Tidak dipungkiri grafis yang kami memiliki sudah menarik banyak sekali pendatang baru yang ingin merasakan terbawa dalam suasana tema permainan mesin slot. Kehadiran slot gacor menjadi angin segar bagi para pecinta judi online, memberikan alternatif permainan yang seru dan menguntungkan. Tak heran jika popularitas slot gacor terus meningkat, menarik minat para pemain baru untuk mencoba peruntungan mereka di situs slot gacor hari ini DEWAGACOR.
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.