Hi guys,
About one year ago, i was working on a little project called "My Farm Factory", it's a game "Terraria like" with a mixture of Harvest moon.
Today, i opened the project again and i tried to solve a problem that i got some months ago, the update of a huge map.
My map is stored in a matrix 200x200, each element is a block, that has a body,fixture,some variables and bla bla bla. Right, but, the game is too slow, if i change the map size (something about 50x50), game runs well. I tried to "draw" blocks based on its position (if block is inside the camera area, then, draw it).
But the problem wasn't solved, i'm thinking the problem is with bodies update, but, i don't know how can i update only the blocks inside the screen, i tried to set the bodies to setActive = false but it does not work.
I attached my project if anyone wants to help me and see the code, to change the map size, just take a look to GameManager, line 19... The map is created in objectFactory file (getNewMethod map).
I think that is necessary some "patern" to make a dynamic load... but, i really don't have any idea how to do this...
Hope u can help me guys!
Thx ...
Slow game, a huge map
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Slow game, a huge map
- Attachments
-
- MyFarmFactory2.rar
- (151.58 KiB) Downloaded 165 times
Re: Slow game, a huge map
If I get it correctly, you have a tile based map but you are using love.physics for the tiles. If the tiles never move, you probably waste a lot of cpu time on these.
I don't know your design concept for this game, but the first thing you can try is not using love.physics.
However if you really want to implement it as it is, you can make the tiles bigger. That way you get fewer tiles for the same map size (in terms of pixels).
I don't know your design concept for this game, but the first thing you can try is not using love.physics.
However if you really want to implement it as it is, you can make the tiles bigger. That way you get fewer tiles for the same map size (in terms of pixels).
Check out my blog on gamedev
- slime
- Solid Snayke
- Posts: 3183
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: Slow game, a huge map
Use a SpriteBatch to draw your dirt blocks. Some more discussion about how to do that is here.
-
- Citizen
- Posts: 65
- Joined: Sat Dec 22, 2012 8:17 am
Re: Slow game, a huge map
Box2D has no issue with running a lot of static objects.
The real issue here is your rendering code. It is REALLY not efficient to render thousands upon thousands of tiles one by one. Your graphics card hates you because you do that, and takes revenge by killing your frames per second.
Use a SpriteBatch object. Granted, this adds some complexity to your code (like making a texture atlas out of all your terrain images), but seeing your nicely structured code, I am assuming you can do it.
I have uploaded a naive texture atlas generator I wrote quite some time ago, so feel free to look at it if you need it.
Edit: duh, too slow.
The real issue here is your rendering code. It is REALLY not efficient to render thousands upon thousands of tiles one by one. Your graphics card hates you because you do that, and takes revenge by killing your frames per second.
Use a SpriteBatch object. Granted, this adds some complexity to your code (like making a texture atlas out of all your terrain images), but seeing your nicely structured code, I am assuming you can do it.
I have uploaded a naive texture atlas generator I wrote quite some time ago, so feel free to look at it if you need it.
Edit: duh, too slow.
- Attachments
-
- textureAtlas.lua
- Naive texture atlas generator
- (1.82 KiB) Downloaded 181 times
Re: Slow game, a huge map
Hi guys,
I've changed my code using SpriteBatch, it seems to be working... but, i set a method to update SpriteBatch into love.update (removing every image and set it again) and FPS up to 25 ~ 40.. i need it because when player hits a block, map must be update. Without this update, the framerate up to 120, and it's fine...
I think it's all i needed to know, ty u all, i'm going to left this project and start a new one... I think it's much complex game to "have fun", i think i'll create something like a apocalyptic zombie survivor, it seems to be a good simple project
Ty again, i hope SpriteBatch can help me hereafter...
Hugs!
I've changed my code using SpriteBatch, it seems to be working... but, i set a method to update SpriteBatch into love.update (removing every image and set it again) and FPS up to 25 ~ 40.. i need it because when player hits a block, map must be update. Without this update, the framerate up to 120, and it's fine...
I think it's all i needed to know, ty u all, i'm going to left this project and start a new one... I think it's much complex game to "have fun", i think i'll create something like a apocalyptic zombie survivor, it seems to be a good simple project

Ty again, i hope SpriteBatch can help me hereafter...
Hugs!
-
- Citizen
- Posts: 65
- Joined: Sat Dec 22, 2012 8:17 am
Re: Slow game, a huge map
Why would you update the whole batch every update?
When the player removes a block, update only that block (as in, remove it from the batch).
This can be done easily by mapping tile coordinates to actual sprite IDs in the batch, such that you can now access any sprite in the batch using random access.
Here's code demonstrating it (yet again an old snippet of mine):
In short, every time I add a quad to the batch, I save its ID in a mapping table with the coordinates as the key.
Using a mapping table, instead of updating your whole level you can just directly access the sprite you want and remove it (for example, you can directly access the first tile with tileMap["0x0"] and the last tile with tileMap[width .. "x" .. height]).
Good luck on your new project.
When the player removes a block, update only that block (as in, remove it from the batch).
This can be done easily by mapping tile coordinates to actual sprite IDs in the batch, such that you can now access any sprite in the batch using random access.
Here's code demonstrating it (yet again an old snippet of mine):
Code: Select all
for y = 1, size.y do
for x = 1, size.x do
local id = self.layer:addq(self.tilePack:map(fillType), x, y)
self.tileMap[x .. "x" .. y] = {id = id, tile = fillType}
end
end
Using a mapping table, instead of updating your whole level you can just directly access the sprite you want and remove it (for example, you can directly access the first tile with tileMap["0x0"] and the last tile with tileMap[width .. "x" .. height]).
Good luck on your new project.

- Hexenhammer
- Party member
- Posts: 175
- Joined: Sun Feb 17, 2013 8:19 am
Re: Slow game, a huge map
This is a good idea / explanation. You should add it to the wiki IMO.spectralcanine wrote:Why would you update the whole batch every update?
When the player removes a block, update only that block (as in, remove it from the batch).
This can be done easily by mapping tile coordinates to actual sprite IDs in the batch, such that you can now access any sprite in the batch using random access.
Here's code demonstrating it (yet again an old snippet of mine):In short, every time I add a quad to the batch, I save its ID in a mapping table with the coordinates as the key.Code: Select all
for y = 1, size.y do for x = 1, size.x do local id = self.layer:addq(self.tilePack:map(fillType), x, y) self.tileMap[x .. "x" .. y] = {id = id, tile = fillType} end end
Using a mapping table, instead of updating your whole level you can just directly access the sprite you want and remove it (for example, you can directly access the first tile with tileMap["0x0"] and the last tile with tileMap[width .. "x" .. height]).
Who is online
Users browsing this forum: Amazon [Bot] and 7 guests