Hey folks,
I could use a wee bit of help on this one...
At the moment I'm trying to code something of a fluid effect using a collection of variably transparent rectangles and an two dimensional array of "densities" expressed in an integer from 0 - 255.
Simple enough – however, when it comes time to move the density values from one position to another I run into problem because of the loop I use to traverse the array.
What happens is that by looping through each of the integers in the Density array and moving their values accordingly I end up risking moving the same value two, thee, or more times.
For example :
If Density [10,3] has a value of 27, and Density[10,4] has a value of 3, then I (to simulate a sort of diffusion) would a portion of Density[10,3] into Density[10,4]. At that point the loop would arrive at the next index of Density[,]…that being Density[10,4]. Finding that Density[10,5] was smaller than Density[10,4] the diffusion would occur again – meaning Density had potentially “traveled” from Density[10,3] to Density[10,5] in one step of time. Skewing the movement of density to one corner and compromising the "accuracy" of the simulation.
In case that doesn't make sense maybe this will :
int[,] Density = new int[300, 300];
//Representitive of 300x300 Pixel Form
for (int y = 0; y < 300; y++)
{
for (int x = 0; x < 300; x++)
{
if (Density[y, x] > Density[x + 1, y])
{
Density[y, x] -= 1;
Density[y, x + 1] += 1;
}
}
}
The problem is in the approach and not the code - but this issue has been the bane of several of my projects...I need to move things symoltaniously and keep the results accurate.
I fear the solution is obvious- but it's managing to kick my but! :-/
If anyone could suggest a better approach I would really appreciate it. Hope the problem and question are clear! Thankyou!
Oh, also, Hello Everybody! I decided to make an account! ;)