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

  • Home
  • SEARCH
  • 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 9003283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:41:33+00:00 2026-06-16T00:41:33+00:00

Well, after looking this problem up three times and finding nothing really relevant to

  • 0

Well, after looking this problem up three times and finding nothing really relevant to my question, I will now ask.


I got curious and decided to experiment to see if I could make a very basic falling sand toy in canvas.

I did successfully do it, as the “sand” builds up on the ground like it should, but it slows down very quickly because of the hit detection between airborne particles and ground particles.

See for yourself Edit: Possibly dead link, here’s a JsFiddle instead.

Particles are spawned by clicking and holding the mouse button. And after you’ve spawned approximately 200 particles, it gets brought to its knees.

for(i in P){
    if(P[i].Y<canvas.height-1){
        P[i].Y++
        for(j in G){//This loop seems to cause the lag
            if(P[i].X==G[j].X&&P[i].Y==G[j].Y-1){
                G[G.length]={X:P[i].X,Y:P[i].Y}
            }
        }
    }else{
        G[G.length]={X:P[i].X,Y:P[i].Y}
    }
}

I’m just wondering if there’s something I’m doing horribly wrong; I’m pretty sure it’s not supposed to lag that badly. I’m also wondering if there’s a way to do it without nested loops, but it seems the only way I’ve been able to make this work is by checking every individual airborne particle against every individual ground particle, which makes it lag.

If the link is broken, say the word and I’ll post the entire code here.

Thanks

  • 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-16T00:41:34+00:00Added an answer on June 16, 2026 at 12:41 am

    It is easier to remember the height of each pile of sand (eg for each column or x-coordinate in the canvas) and check against that. You can directly use the index (should be the same as the x-coordinate), so you do not require to loop through all ground positions to find the correct one.

    You need only one check for each airborne sand piece, namely the check with the height of the pile of the corresponding column.

    Additionally when the sand has hit the pile, remove it from the ‘active particle’ list, so you do not need to check it each time, keeping the outer for-loop as small/short as possible.

    Redraw each pile using the height of the pile (does not work well when the sand grains have separate colors…) or put the sand that is not falling anymore in a separate memory structure to redraw the piles properly.

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

Sidebar

Related Questions

I ran into this quite often stated problem but even after looking up nearly
I may well be looking at this problem backwards but I am curious none
Well looks too simple a question to be asked but i asked after going
Tomcat has got that well-known memory leak problem that causes PermGen space errors after
I recently ran into a quite complex problem and after looking around a lot
I am looking for a Javascript solution to this problem: I have an XML
I'm looking for a solution to a strange problem I stumbled upon this afternoon.
Well after a while of scratching my head and going huh? trying to figure
Well how could I change the before image to the after image by using
After much searching and googling I am coming back to the well. I have

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.