How exactly does random block generation work in 2D/3D worlds?
Hey, I've had this question for a LONG time. I've been searching the internet for tutorials/answers, but no results. What makes this frustrating for me is that I only see unrelated answers, or people putting videos on Youtube showing them testing 2D random world generation. And there's no tutorials?!
Well, sure, you could render a couple blocks in random places. But how to people achieve this rendering 100s, if not 1000s of blocks and the game run smoothly, no lag?! So my questoins are:
Exactly how are they randomly generated at all, and second, how are so many objects in a single class not causing lag? And third, how are they storing the block objects? ArrayLists or Arrays? If someone has the answer, I would love to know! Thanks in advance! :)
Re: How exactly does random block generation work in 2D/3D worlds?
I'm not sure what you mean by "blocks" or what graphics library you're using, so you may wish to clarify this as well as provide any other details that can help us help you.
I will take a stab though with the caveat that I'm not a professional programmer, not by any means.
My thoughts:
- If you mean the Swing GUI library, and you are asking about rendering many fixed objects, this can easily be done by drawing them in a BufferedImage and then using that image as a background image for a GUI, drawing it as either an ImageIcon in a JLabel or drawn in a JComponent's paintComponent method. Then moving sprites can be drawn in the same paintComponent method but with variable position.
- Always remember to separate program logic from view. Although this adds a level of indirection, it really shouldn't slow things too much.
- If you are asking about serious game creation, then likely you'll want to use a different GUI library than Swing and use an already created and optimized game engine.
Re: How exactly does random block generation work in 2D/3D worlds?
By blocks I mean 2D tiles. And I'm using LWJGL/Slick2D, by the way. Check out a video on Youtube named Java 2D Random Tile Generation by Radnyxx Gentleman. It's pretty much what I'm talking about. If you acknowledge a solution, just give me some hints or psuedo code. Remember, I'm wondering about how so many are rendered without lag at all.