I'm making a 2d platformer. I have a table of all the dynamic objects in the game world. Right now I'm naively looping through the table and drawing all the objects in the table. But because the game world is much bigger than the window size most object dont make it on screen.
Am I wasting resources like this? Should I make function that checks if a objects would appear on screen or does love.graphics.draw already handle this efficiently?
Drawing outside window
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Drawing outside window
In general that's not necessary.
If you find yourself having a lot of things to draw, most of which are off-screen anyway, you might want to use something like a spatial hash.
Basically, instead of having one table containing all the entities, you have a table of tables. Each table refers to a bit of space, and contains all the objects that are in that space.
That way, you only have to loop over the tables that are (partially or completely) visible. Note that how big the pieces of space are matters a lot for how it performs. You might want to experiment a bit with that.
If you find yourself having a lot of things to draw, most of which are off-screen anyway, you might want to use something like a spatial hash.
Basically, instead of having one table containing all the entities, you have a table of tables. Each table refers to a bit of space, and contains all the objects that are in that space.
That way, you only have to loop over the tables that are (partially or completely) visible. Note that how big the pieces of space are matters a lot for how it performs. You might want to experiment a bit with that.
Help us help you: attach a .love.
Re: Drawing outside window
I'm not too familiar with the Love2D renderer but yes drawing geometry from Lua would probably cause extra overhead on the backend.Am I wasting resources like this?
Depending on how large your levels are this might or might not be an issue.
Probably a better idea to just iterate your visible tiles and draw them which would be fairly trivial especially if your objects are already stored in a 2D table.Should I make function that checks if a objects would appear on screen or does love.graphics.draw already handle this efficiently?
Re: Drawing outside window
Ok thanks, the spatial hash is a good idea but then of course I have to make sure the object can't leave the region of the hash.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Drawing outside window
Or move them to a different table when they leave the region.
Help us help you: attach a .love.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 2 guests