Optimizing updates and rendering for thousands of tables

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
RylandAlmanza
Prole
Posts: 2
Joined: Tue Nov 20, 2012 3:25 am

Optimizing updates and rendering for thousands of tables

Post by RylandAlmanza »

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:

Code: Select all

if matrix[self.y + 1][self.x] ~= 0 then (...)
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.
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Optimizing updates and rendering for thousands of tables

Post by dreadkillz »

It's unlikely that you'll get good performance for a sand game without using shaders. Love can't handle that many particles (90000!?)

See: viewtopic.php?f=4&t=3733&p=37873&hilit=sand#p37873

I take it back. Someone apparently made a nice demo (I even commented!):

viewtopic.php?f=5&t=9510&hilit=sand#p58946
RylandAlmanza
Prole
Posts: 2
Joined: Tue Nov 20, 2012 3:25 am

Re: Optimizing updates and rendering for thousands of tables

Post by RylandAlmanza »

I'm not thinking that the rendering is the main problem. While it probably is part of the reason the game is so slow, the game ran very fast before I started using tables to represent particles. I used to be using numbers. sand was 1 and wall was 2. I might have to go back to that system, but the if/else statements in the update function might get kind of crazy.
Post Reply

Who is online

Users browsing this forum: No registered users and 51 guests