Optimizing updates and rendering for thousands of tables
Posted: Tue Nov 20, 2012 5:12 am
I'm making a falling sand like game. If you've never played one, you can play a good one here. The idea is that everything is made up of tiny particles. These particles react to each other in different ways depending on what elements they are.
Right now, I have a very basic demo working very slowly. I'm using tables to represent particles. A particle's table stores data like x and y position, color, and a function called "update" that decides what it does every update based on the particles around it.
Originally, I was going to store every particle in a 1d array (actually a table, but I'd be using it like an array.) The problem I saw with this method is collision detection. You would have to loop for every single particle in the array and check if they are adjacent multiple times, which I imagine would be very slow.
So I rejected that method, and now I'm using a 2d array (again, actually a table,) with a width and height equal to screen width and height (300x300.) If there is nothing in a particular cell, the value in the table is 0. If there is a particle in a cell, the value is another table representing the particle. The reason I chose this is because if I want to know if there is a particle directly below the particle checking for collisions, I don't have to loop every particle. I can just use:
This hasn't proven very fast at all, because I still have to loop through the gigantic 300x300 (90,000!) array.
On top of the updating problem, I also have to figure out how to efficiently render it. I figured drawing each individual pixel for every particle would be very slow, so my current method is to create a new ImageData every frame, use the setPixel method for every particle, and then draw the whole image. That was all I could think of, and I have no idea if it's any faster than the alternative.
So, I was wondering if anyone knows any more efficient ways to update and render the game. Thanks a lot for reading!
If you want to look at the code, it's all in main.lua here.
Right now, I have a very basic demo working very slowly. I'm using tables to represent particles. A particle's table stores data like x and y position, color, and a function called "update" that decides what it does every update based on the particles around it.
Originally, I was going to store every particle in a 1d array (actually a table, but I'd be using it like an array.) The problem I saw with this method is collision detection. You would have to loop for every single particle in the array and check if they are adjacent multiple times, which I imagine would be very slow.
So I rejected that method, and now I'm using a 2d array (again, actually a table,) with a width and height equal to screen width and height (300x300.) If there is nothing in a particular cell, the value in the table is 0. If there is a particle in a cell, the value is another table representing the particle. The reason I chose this is because if I want to know if there is a particle directly below the particle checking for collisions, I don't have to loop every particle. I can just use:
Code: Select all
if matrix[self.y + 1][self.x] ~= 0 then (...)
On top of the updating problem, I also have to figure out how to efficiently render it. I figured drawing each individual pixel for every particle would be very slow, so my current method is to create a new ImageData every frame, use the setPixel method for every particle, and then draw the whole image. That was all I could think of, and I have no idea if it's any faster than the alternative.
So, I was wondering if anyone knows any more efficient ways to update and render the game. Thanks a lot for reading!
If you want to look at the code, it's all in main.lua here.