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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:24:12+00:00 2026-06-11T03:24:12+00:00

I’m using the processing java library to simulate a ball bouncing between two walls

  • 0

I’m using the processing java library to simulate a ball bouncing between two walls using Newtonian physics; horizontal velocity is a constant, as there is no acceleration. I got the simulation to work, however I would like to specify the height of the floor. However in my equations when I attempt to change direction when the ball is 10 pixels from the bottom of the screen, after every bounce, the ball gets lower and lower below the “floor”. If I raise the floor to >20 pixels, the ball does not come to a halt and instead indefinitely bounces. Here is the pertinent code:
Note that processing’s coordinate system begins at the top and runs down and right. Thanks for the help in advance.

public class Testing extends PApplet {

boolean inFloor=false; 
float xPosition=500;
float yPosition=200;
float xVelocity=25;
float yVelocity=-80.0f;
float yAccelleration=+10.0f;
float elasticity=0.80f;

public void setup (){
  size(displayWidth,displayHeight);
  noStroke();
  ellipseMode(RADIUS);
  frameRate(35);
}
 public boolean sketchFullScreen() {
  return true;
 }



public void draw(){
    background(0);
    //Changes direction of motion when hitting a wall
    if(xPosition>=displayWidth||xPosition<0){
        xVelocity=-xVelocity;
    }
    //supposed to change direction of motion when the ball hits the floor
    if(yPosition>=displayHeight-20){
        yPosition=(displayHeight-20);    
        yVelocity=-(yVelocity)*elasticity;
        if(yVelocity>=-1 && yVelocity<=0){
            xVelocity=xVelocity*elasticity;
            yVelocity=0; 
            yAccelleration=0;
        }    
    }
    else{
        yVelocity=yVelocity+yAccelleration;
    }

    yPosition=yVelocity+yPosition;
    xPosition=xPosition+xVelocity;
    ellipse(xPosition,yPosition,10,10);
}

Edit: Could this possibly be a timing issue?

Edit: Thank you for all the responses. Unfortunately I can’t upvote any of them, (only 6 rep). I combined a mixture of @tobius_k’s answer, @Roberto_Mereghetti’s answer and some sample code from OpenProcessing.org and that solved it. In the solution provided below, because the canvas is measured in pixels (integer values), the use of floats to specify co-ordinates was causing graphical glitches in processing. So I implemented a system where float values were rounded, with decimal values being added to an accumulator (“xRounder” and “yRounder”) which when were great than -1 or 1 were rounded and added to the Ball’s current position. This gave me a floor!

Final Code:

    import processing.core.*; 
    //import processing.xml.*; 

    import java.applet.*; 
    import java.awt.Dimension; 
    import java.awt.Frame; 
    import java.awt.event.MouseEvent; 
    import java.awt.event.KeyEvent; 
    import java.awt.event.FocusEvent; 
    import java.awt.Image; 
    import java.io.*; 
    import java.net.*; 
    import java.text.*; 
    import java.util.*; 
    import java.util.zip.*; 
    import java.util.regex.*; 

    public class Testing extends PApplet {
    int xPosition=500;
    int yPosition=200;
    float xRounder=0;
    float yRounder=0;
    float xVelocity=25;
    float yVelocity=-80.0f;
    float yAccelleration=+10.0f;
    float elasticity=0.80f;
    public void setup (){
    size(displayWidth,displayHeight);
    noStroke();
    ellipseMode(RADIUS);
    frameRate(15);
    }
    public boolean sketchFullScreen() {
      return true;
    }

     /* (non-Javadoc)
     * @see processing.core.PApplet#draw()
     */
    public void draw(){
    background(0);

    yPosition=round(yVelocity)+yPosition;
    yRounder+=(yVelocity-round(yVelocity));
    xPosition=round(xVelocity)+xPosition;
    xRounder+=(xVelocity-round(xVelocity));

    if(xRounder>=1||xRounder<=-1){
        xPosition=xPosition+round(xRounder);
        xRounder=xRounder-round(xRounder);
    }
    if(yRounder>=1||yRounder<=-1){
        yPosition+=round(yRounder); 
        yRounder=yRounder-round(yRounder);
    }

    if(yPosition>displayHeight-50 && yVelocity>0){
        yPosition=displayHeight-50;
        yVelocity=-(yVelocity)*elasticity;  
        xVelocity=xVelocity*elasticity;


    }

    if(xPosition>=displayWidth||xPosition<0){
    xVelocity=-xVelocity;
    }
    yVelocity=yVelocity+yAccelleration;  




    ellipse(xPosition,yPosition,10,10);


    }

        static public void main(String args[]) {
           PApplet.main(new String[] { "--bgcolor=#ECE9D8", "Testing" });
    //      new Testing().setVisible(true);
        }
    }
  • 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-11T03:24:14+00:00Added an answer on June 11, 2026 at 3:24 am

    I am not 100% sure, but I think that by resetting the ball to the bottom line when it actually arrives a few pixels below the bottom line you exactly compensate the elasticity factor, so that the ball bounces on indefinitely.

    Try removing the line yPosition=(displayHeight-20);, then it should work better.

    This way, the ball will eventually come to a rest, but this is still not correct, as the time the ball falls through the ground is not counted at all.

    Update Okay, I think now I’ve got it. Here are the relevant bits:

    // first update position, then update velocity
    yPosition=yVelocity+yPosition;
    if (yPosition >= floor) {
        // invert velocity and position w/ elasticity applied
        yVelocity = -yVelocity * elasticity;
        yPosition = floor - (yPosition - floor) * elasticity;
    }
    if(Math.abs(yVelocity) <= 2 && Math.abs(yPosition - floor) <= 2){
        // stop everything when close to rest
        yPosition=floor;
        yVelocity=0; 
    } else {
        // otherwise accelerate, even after bounce
        yVelocity=yVelocity+yAccelleration;
    }
    

    So the main changes w.r.t. your code are:

    1. first to update the position, and then the velocity
    2. instead of resetting the ball to the floor, reverting the amount it fell through the floor, and also applying the elasticity factor to that
    3. always to accelerate the ball, even when it bounces off the floor

    And here how it looks like (floor being at 500). Not perfect, but close.

    enter image description here

    Update 2: Just found this Processing example, which is actually very close to your original code, with the difference that they always apply acceleration, and that they do so before the collision-checks. Seems like this alone does the trick.

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

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.