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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:59:33+00:00 2026-05-28T15:59:33+00:00

In this program I simulate the gravity. All works but when there is no

  • 0

In this program I simulate the gravity. All works but when there is no more movement for the ball it keep bouncing 1-5 pixels depending on the gravity value I set. How could i stop the ball when the energy is lost? I want the xSpeed to become 0 and the ball to stay on a fix position.

Edit: The gravity variate from 1 to 100. The user can change the gravity.

energyLoss = 0.9 and dt = 0.2

// right and left wall collision
        if (x + xSpeed > this.getWidth() - radius - 1) {
            x = this.getWidth() - radius - 1;
            xSpeed = -xSpeed;
        } else if (x + xSpeed < 0 + radius) {
            x = 0 + radius;
            xSpeed = -xSpeed;
        } else
            x += xSpeed;

        if (y == this.getHeight() - radius - 1) {

        }

        if (y > this.getHeight() - radius - 1) {
            y = this.getHeight() - radius - 1;
            ySpeed *= energyLoss;
            ySpeed = -ySpeed;

            // friction with the ground
            xSpeed *= xFriction;
            if (Math.abs(xSpeed) < .4)
                xSpeed = 0;

        } else {
            ySpeed += gravity * dt; // velocity formula
            y += ySpeed * dt + .5 * gravity * dt * dt; // position formula
        }

        repaint();
  • 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-28T15:59:34+00:00Added an answer on May 28, 2026 at 3:59 pm

    The simplest way to fix this problem is to detect a special case where the ball is on the wall with 0 (or very small) Y velocity. This is basically what DwB is suggesting.

    However, you then need to go further and make sure that you stop applying gravity when you’re in that situation.

    Something like this:

    // right and left wall collision
        if (x + xSpeed > this.getWidth() - radius - 1) {
            x = this.getWidth() - radius - 1;
            xSpeed = -xSpeed;
        } else if (x + xSpeed < 0 + radius) {
            x = 0 + radius;
            xSpeed = -xSpeed;
        } else
            x += xSpeed;
    
        if (y == this.getHeight() - radius - 1 
            && ySpeed == 0) { // Check speed too here!
            // Do nothing for Y.
            // friction with the ground
            xSpeed *= xFriction;
            if (Math.abs(xSpeed) < .4)
                xSpeed = 0
        }
        else if (y > this.getHeight() - radius - 1) {
            y = this.getHeight() - radius - 1;
            ySpeed *= energyLoss;
            if (Math.abs(ySpeed) < SomeMinimumValue)
                ySpeed = 0;
            else // invert speed, i.e. change direction.
                ySpeed = -ySpeed;
    
            // friction with the ground
            xSpeed *= xFriction;
            if (Math.abs(xSpeed) < .4)
                xSpeed = 0;
    
        } else {
            ySpeed += gravity * dt; // velocity formula
            y += ySpeed * dt + .5 * gravity * dt * dt; // position formula
        }
    
        repaint();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program I'm doing is about a social network, which means there are users
I am trying to simulate my small program and I keep getting error messages
I have a program that simulates mouse click. Code is something like this: [DllImport(user32.dll,
This program I use has it's own variables to set when you run it,
This program stores pairs in a map, counting the number of times a word
This program reads emails (really just a .txt file structured like an email) and
This program is meant to generate a dynamic array, however it gives an access
For this program #include <iostream> using std::cout; struct C { C() { cout <<
Consider this program int main() { float f = 11.22; double d = 44.55;
In this program when I debug, it is showing the nil for fileNameString. I

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.