Make simple 2D block world - LibGDX

Hello everybody, whats up?
So… I am learning LibGDX right now, and its really really good, I love all the camera stuff and such…
But here I went into a little trouble, I CAN’T, CAN’T manage to make a simple 2D Block world…

First of all this is how I created the world variable -

Block[] map = new Block[100 * 100];

So this is my world :smiley:
Now the block class is really simple, it just have some variables, size = 100 and texture image = some image.

Simple enough right? NOW HOW THE FUCKK CAN I RENDER IT?!?!!?!?!

I tried… for a week, I am telling you bros it looks impossible for me right now.
I can post what I’ve tried… but its too not working to post haha

So how can I render a map like this, so when i move the player it moves too or something?
at least give me a way or a hint please… i am tired for trying this for a whole week.

Start small. If you cant get this to work, set up a new project and just try to figure out how to render individual shapes. You’ll need a SpriteBatch.

Seriously though, no one is just going to give you that much code. Its not beneficial for you.

Your computer will only do what you tell it to do.

There isn’t some easy ‘render’ method you can use just with what you’ve got.

You have to tell it how to render.

…Uhhh I didn’t mention I am programming for more than 2 years now? I just starting out with game development, which was my dream for long.
Now let me explain the problem…

The idea of rendering a world is just to loop through an array of blocks, like this:

for(int y = 0; y < chunk; y++) {
     for(int x = 0; x < chunk; x++) {
          batch.draw(map[y * width + x].TEXTURE, (x * Block.WIDTH), (y * Block.HEIGHT), Block.WIDTH, Block.HEIGHT);
     }
}

That’s how it should be done, of course not exactly like this, but this is the prototype, the core of map rendering.
As I am starting out, I can’t get this to work, mostly because the camera is moving and the map is rendering always at the same position, am i right?

I hope you get what is my problem and what I need is a way of thinking about it, maybe my prototype is wrong? maybe there is another way of doing this…

Thanks for helping! I am kinda of confused myself so yeah… ::slight_smile:

Wait, so what exactly is the problem here?

Does is draw?
If so, is what it draws correct?
If it isn’t correct, what is wrong with it?

What it does actually, it draws the map on some location ( chunk = 20 and blocksize is = 100, so the location is = 0 - 2000) in the game space (?),
Now when i move my character and my camera moves as well, i can get off that map, because it draws it always at the same location.
you understand? it doesnt expand the map as i move… kinda…

ohhh i am soo aweful at this, am i?

I saw your projects and as I see you use tiled for the map right? well I don’t i make the tile class myself with id and images hahah.

Holy Shit, I am starting to realize it… So LibGDX have already a build-in game space!!!
I figured this out as i move my camera (ortho camera), I couldn’t figure out how the heck i can move in game space without creating one.
So LibGDX have a build-in game space its the answer!

Am I right? any ideas? help? something to feed me will be very appreciated!

Thank you @HeroesGraveDev, for helping. :slight_smile:
Do you have any upvotes here so you get points or shiit like that maybe?

So… this is solved?

Sorry, it’s hard to tell from your post whether you’re asking whether a solution works or saying that it already has worked.

Either way, I haven’t used LibGDX, but the orthographic camera is what you are/were looking for if you need the screen to follow the player around the map.

[quote]…Uhhh I didn’t mention I am programming for more than 2 years now?
[/quote]
I just want to point out that this means nothing. Do you know how to use LibGDX? No, so your two years of programming are not going to help learn the API, besides knowing how the code should logically fit together. If your problem isn’t fixed, I stand by my answer. Start small with new libraries and work on it until you finally have what you originally set out to have. Experience in Java is not going to help you that much, besides the obvious.

Take a look at my collision example. It is written in LWJGL, but it might be kinda similar.

Allright… I will start to work slow and easy, I am starting to realize how this stuff works so… I think i am in a good path.

Thanks anyways folks.
:slight_smile:

Ok, I did it, I figured out and learnt how it works and now i can render the world awesomely great!!

I mean look at this piece of code :DDD

public void RenderMap() {
		for(int y = 0; y < chunk / 2; y++) {
			for(int x = 0; x < chunk / 2; x++) {
				int mapPosY = (y + PosHandle.getBlockY(player.posy));
				int mapPosX = (x + PosHandle.getBlockX(player.posx));
				int mapPos = (mapPosY) * Assets.map.width + (mapPosX);
				if(Assets.map.map[mapPos].TEXTURE != null && mapPosX < Assets.map.width && mapPosY < Assets.map.height && mapPosX > 0 && mapPosY > 0) {
					batch.draw(Assets.map.map[mapPos].TEXTURE, (mapPosX * Block.WIDTH), (mapPosY * Block.HEIGHT), Block.WIDTH, Block.HEIGHT);
				}
				
				int mapPosXA = (PosHandle.getBlockX(player.posx) - x);
				int mapPosA = (mapPosY) * Assets.map.width + (mapPosXA);
				if(Assets.map.map[mapPosA].TEXTURE != null && mapPosXA < Assets.map.width && mapPosY < Assets.map.height && mapPosXA > 0 && mapPosY > 0) {
					batch.draw(Assets.map.map[mapPosA].TEXTURE, (mapPosXA * Block.WIDTH), (mapPosY * Block.HEIGHT), Block.WIDTH, Block.HEIGHT);
				}
			}
		}
	}

Any improvements? I assume you don’t want to read it, but maybe you will learn something, or help me if i did something wrong.

  • Actually the code will be too hard to understand so leave it, just wanted to share
    Thanks for help folks!