Particle Physics of Falling Sand

I am working on an iOS application with a simulation of sand particles. Here is an alpha video showing the logic of his http://www.youtube.com/watch?v=cYgw6jHx6QE

But I have a problem with the performance of the particle algorithm.

Now I do it as follows (the application is made on cocos2d, but it does not matter):

Every 0.03 seconds I get pixel pixel data with

glReadPixels(0,0,WindowSize.width,WindowSize.height,GL_RGBA,GL_UNSIGNED_BYTE,&ScreenBuffer);

I also have a function to compare pixel colors with background color

- (BOOL) GetColorAtPoint: (int) GetX : (int)GetY
{
    int YSize = (int)WindowSize.width*4;

    Byte R = ScreenBuffer[(YSize*GetY) + 0 + (4 * GetX)];
    Byte G = ScreenBuffer[(YSize*GetY) + 1 + (4 * GetX)];
    Byte B = ScreenBuffer[(YSize*GetY) + 2 + (4 * GetX)];
    Byte A = ScreenBuffer[(YSize*GetY) + 3 + (4 * GetX)];

    return (R==255 && G == 0 && B == 0 && A == 255);
}

I define a structure for particles and have an array for them

struct SandParticle
{
    CGPoint Position;

    BOOL CMTop;
    BOOL CMBottom;
    BOOL CMLeft;
    BOOL CMRight;
};

struct SandParticle SandMatrix[5000];

int ParticlesCounter;

, . 0,03 SandParticle SandMatrix ParticlesCounter. SandMatrix ( GetColorAtPoint, ) Y-1 , Y-1 X-1 , Y-1 X + 1 . , , .

https://gist.github.com/8e6c4710950d17ed3d3c#L122 ( , )

, iOS ( Simulator) ± 800. ( UpdateParticles). , -. - ?

+3
1

, algo, ; , , , ,

       int Randomized = precomputed_arc4rand_unif2[CPLoop];

, , , , , , , CurrentParticle, , , (, , ), ..

      struct SandParticle *CurrentParticle = &SandMatrix[CPLoop];

CurrentParticle-> CurrentParticle. "" CurrentParticle, .

, .

+1

All Articles