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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:10:54+00:00 2026-05-11T07:10:54+00:00

This is probably a really basic problem but I can’t seem to find any

  • 0

This is probably a really basic problem but I can’t seem to find any other articles on it.

Anyway, I have written a small bouncing ball program in Java to try and expand my basic skills. The program is just a simple bouncing ball that will drop and hopefully bounce for a while. The original program worked fine but now I have tried to add gravity into the program. The gravity actually works fine for a while but then once the bounces get really small the animation becomes erratic for a very short time then the position of the ball just constantly decreases. I’ve tried to figure out the problem but I just can’t see it. Any help would be most welcome.

public final class Ball extends Rectangle { float xspeed = 1.0f; float yspeed = 1.0f; float gravity = 0.4f;   public Ball(float x, float y, float width, float height) {     super(x, y, width, height); }  public void update(){     yspeed += gravity;      move(xspeed, yspeed);      if(getX() < 0){         xspeed = 1;     }     if(getX() + getWidth() > 320){         xspeed = -1;     }     if(getY() < 0){         yspeed = 1;     }     if(getY() + getHeight() > 200 && yspeed > 0){         yspeed *= -0.98f;     }     if(getY() + getHeight() > 200 && yspeed < 0){         yspeed *= 0.98f;     }  }  public void move(float x, float y){     this.setX(getX()+x);     this.setY(getY()+y); } 

}

EDIT: Thanks that seems to have sorted the erratic movement. I’m still struggling to see how I can stop my ball moving down when it has stopped bouncing. Right now it will move stop bouncing then continue moving down passed the ‘floor’. I think it’s to do with my yspeed += gravity line. I just can’t see how I’d go about stopping the movement down.

  • 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. 2026-05-11T07:10:55+00:00Added an answer on May 11, 2026 at 7:10 am

    When you do

    yspeed += gravity; 

    you are assuming that the ball has space move through a distance dx = v_i * t + 1/2 (-g) t^2. When you are very near the floor this may not be true. It fail if:

    • You are near enough the the floor and moving down
    • You are very near the floor and have low velocity (like when the ball has lost most of it’s energy)

    This bug causes your simulation to stop conserving energy, resulting in the erratic behavior you see at low amplitude.

    You can reduce the problem by using smaller time steps, and you can get rid of it outright if you do test computation to notice when you’re out of room and to select a safe time step for that iteration (i.e. always use your default unless there is a problem, then calculate the best time step).

    However, the basic approximation you’re using here has other problems as well. Look in any numeric analysis text for a discussion of solving differential equations numerically.

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I've discovered that one option is to set the control's… May 11, 2026 at 11:53 pm
  • Editorial Team
    Editorial Team added an answer My best suggestion is to use nxml-mode (available as packages… May 11, 2026 at 11:53 pm
  • Editorial Team
    Editorial Team added an answer Looks like like the answer is yes: $ echo 'print… May 11, 2026 at 11:53 pm

Related Questions

This is probably a really basic problem but I can't seem to find any
I'm using two different libraries in my project, and both of them supply a
I know the topic of removing www.experts-echange.com has been beaten to death but having
I have an interesting problem which I've been looking into and would appreciate some
I routinely run into this problem, and I'm not sure how to get past

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.