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 4621294
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:40:50+00:00 2026-05-22T02:40:50+00:00

I am making a Falling Sand style game in Java, and I’m having weird

  • 0

I am making a Falling Sand style game in Java, and I’m having weird issues with an if-else block that I have. In my doGravity() method, I have an various blocks of conditions that will cause different things to happen, and for some odd reason, one block is NEVER getting hit.

When I have this block count how many times each condition is hit, the left and right blocks are hit almost evenly:

else if(world[x][y+1]==EMPTY && (x-1 >= 0) && world[x-1][y+1] == EMPTY && (x+1 < world.length) && world[x+1][y+1]==EMPTY) {
    int r = rand.nextInt(50);
    if(r == 0) {
        world[x-1][y+1] = world[x][y];
        //System.out.println("GO: right");
        countRight++;
    }
    else if(r == 1) {
        world[x+1][y+1] = world[x][y];
        //System.out.println("GO: left");
        countLeft++;
    }
    else {
        world[x][y+1] = world[x][y];
        countCenter++;
    }
    world[x][y] = EMPTY;
}

Next comes this condition, which also equally distributes left and right.

else if((x-1 >= 0) && world[x-1][y+1] == EMPTY && (x+1 < world.length) && world[x+1][y+1]==EMPTY) {
    if(rand.nextBoolean()) {
        world[x-1][y+1] = world[x][y];
        //countLeft++;
    }
    else {
        world[x+1][y+1] = world[x][y];
        //countRight++;
    }
    world[x][y] = EMPTY;
}

But when I count these blocks, the left block NEVER gets hit, even when the space to the left is open. I feel like its probably just some stupid typo that I can’t see for some reason.

else if((x-1 >= 0) && world[x-1][y+1] == EMPTY) {
    world[x-1][y+1] = world[x][y];
    world[x][y] = EMPTY;
    countLeft++;
    System.out.println("Hit Left");
}
else if((x+1 < world.length) && world[x+1][y+1] == EMPTY) {
    world[x+1][y+1] = world[x][y];
    world[x][y] = EMPTY;
    countRight++;
    System.out.println("Hit Right");
}

UPDATE: If I remark out the left block at the end, absolutely nothing changes. The sand acts exactly the same. If I remark out the right block at the end, it acts the same as if I remark out both blocks. I cannot figure this out. It should work… but it doesn’t.

UPDATE: Here’s the full source code. I have no idea what this could possibly be. It will, in fact, drive me insane. http://pastebin.com/mXCbCvmb

  • 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-05-22T02:40:51+00:00Added an answer on May 22, 2026 at 2:40 am

    Your pastebin code does show “Hit left”, you just need to change the creation of world (line 65 pastebin) to

        world = new Color[worldWidth][worldHeight+1];
    

    Because of the y+1 part i would suppose. Other than that it grows both to the left and to the right.

    EDIT: http://pastebin.com/GVmSzN4z I twiddled a little with your doGravity to make the drops a little more symmetric.

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

Sidebar

Related Questions

I'm making a falling sand game in Java. I want users to be able
I have uncovered another problem in the effort that we are making to port
I am making a 2D Java game and i'm trying to figure out how
I'm having trouble making this simple thing happen. I have this loop adding values
I have some code that is failing tests, but I can't understand why. I
I'm implementing a small game and am having trouble getting the physics working properly.
I'm making a game in C++ and it involves filling tiles with random booleans
I'm looking into making some software that makes the keyboard function like a piano
I'm looking into making some software that makes the keyboard function like a piano
I'm making a game in HTML5's Canvas + JavaScript. My CPU use for the

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.