Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7927729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:28:17+00:00 2026-06-03T19:28:17+00:00

I’m working on iOS app with sand particles simulation. Here is a video of

  • 0

I’m working on iOS app with sand particles simulation. Here is a video of alpha version showing the logic of it http://www.youtube.com/watch?v=cYgw6jHx6QE

But I have a problem with particle algorithm performance.

Now I’m doing it in the following way (app is made on cocos2d, but it does not really matter):

Every 0.03 seconds I get screen pixel data with

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

I also have a function to compare pixel color 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 struct for particles and have array for them

struct SandParticle
{
    CGPoint Position;

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

struct SandParticle SandMatrix[5000];

int ParticlesCounter;

So, my logic is following. Every 0.03 seconds I create new particle with SandParticle struct and add it to the SandMatrix array and increase ParticlesCounter. Then I iterate over SandMatrix array and for each particle I get background color (using GetColorAtPoint method mentioned above) on positions Y-1 for bottom, Y-1 X-1 for left, Y-1 X+1 for right. If background color is red, then particle can go down, left or right.

Full code for that is available here https://gist.github.com/8e6c4710950d17ed3d3c#L122 (do not judge strictly, it is a draft version)

The main problem is, that I have frame-rate drops on iOS device (everything is ok in Simulator) for particles quantity over ±800. The problem is an iteration cycle (UpdateParticles method). But I don’t have any idea how I can make it in another way. Is there anything I can do here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:28:18+00:00Added an answer on June 3, 2026 at 7:28 pm

    I don’t know exactly the algo, but usually good random generators are rather slow; maybe you could avoid that call, doing it at the initialization phase, filling an array and getting the value by index like

           int Randomized = precomputed_arc4rand_unif2[CPLoop];
    

    Supposing optimizations are at their best, I think rearranging code shouldn’t give important benefits in term of performance, except maybe you can check if to continue before copying into CurrentParticle, and in general you could avoid such a copy, if you want a shorter way to write it, use a define or go through a pointer (but maybe optimization saves this too), i.e.

          struct SandParticle *CurrentParticle = &SandMatrix[CPLoop];
    

    and then you do CurrentParticle-> instead of CurrentParticle. and delete the final “copy-back” of CurrentParticle, which is not needed.

    But I don’t know if it would make the performance so much better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am writing an app with both english and french support. The app requests
I am using Paperclip to handle profile photo uploads in my app. They upload
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.