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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:20:58+00:00 2026-05-10T22:20:58+00:00

I’ve written a fairly simple java application that allows you to drag your mouse

  • 0

I’ve written a fairly simple java application that allows you to drag your mouse and based on the length of the mouse drag you did, it will shoot a ball in that direction, bouncing off walls as it goes.

Here is a quick screenshot:
alt text http://img222.imageshack.us/img222/3179/ballbouncemf9.png

Each one of the circles on the screen is a Ball object. The balls movement is broken down into an x and y vector;

public class Ball {     public int xPos;     public int yPos;     public int xVector;     public int yVector;      public Ball(int xPos, int yPos, int xVector, int yVector) {         this.xPos = xPos;         this.yPos = yPos;         this.xVector = xVector;         this.yVector = yVector;     }      public void step()     {         posX += xVector;         posY += yVector;          checkCollisions();     }      public void checkCollisions()     {         // Check if we have collided with a wall         // If we have, take the negative of the appropriate vector         // Depending on which wall you hit     }      public void draw()     {         // draw our circle at it's position     } } 

This works great. All the balls bounce around and around from wall to wall.

However, I have decided that I want to be able to include the effects of gravity. I know that objects accelerate toward the earth at 9.8m/s but I don’t directly know how this should translate into code. I realize that the yVector will be affected but my experimentation with this didn’t have the desired effect I wanted.

Ideally, I would like to be able to add some gravity effect to this program and also allow the balls to bounce a few times before settling to the ‘ground.’

How can I create this bouncing-elastic, gravity effect? How must I manipulate the speed vectors of the ball on each step? What must be done when it hits the ‘ground’ so that I can allow it to bounce up again, but somewhat shorter then the previous time?

Any help is appreciated in pointing me in the right direction.


Thanks you for the comments everyone! It already is working great!

In my step() I am adding a gravity constant to my yVector like people suggested and this is my checkCollision():

public void checkCollision() {     if (posX - radius < 0)              // Left Wall?     {         posX = radius;              // Place ball against edge         xVector = -(xVector * friction);     }     else if (posX + radius > rightBound) // Right Wall?     {         posX = rightBound - radius;     // Place ball against edge         xVector = -(xVector * friction);     }      // Same for posY and yVector here. } 

However, the balls will continue to slide around/roll on the floor. I assume this is because I am simply taking a percentage (90%) of their vectors each bounce and it is never truly zero. Should I add in a check that if the xVector becomes a certain absolute value I should just change it to zero?

  • 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-10T22:20:58+00:00Added an answer on May 10, 2026 at 10:20 pm

    What you have to do is constantly subtract a small constant (something that represents your 9.8 m/s) from your yVector. When the ball is going down (yVector is already negative), this would make it go faster. When it’s going up (yVector is positive) it would slow it down.

    This would not account for friction, so the things should bounce pretty much for ever.

    edit1: To account for friction, whenever it reverses (and you reverse the sign), lower the absolute number a little. Like if it hits at yVector=-500, when you reverse the sign, make it +480 instead of +500. You should probably do the same thing to xVector to stop it from bouncing side-to-side.

    edit2: Also, if you want it to react to ‘air friction’, reduce both vectors by a very small amount every adjustment.

    edit3: About the thing rolling around on the bottom forever–Depending on how high your numbers are, it could be one of two things. Either your numbers are large and it just seems to take forever to finish, or you are rounding and your Vectors are always 5 or something. (90% of 5 is 4.5, so it may round up to 5).

    I’d print out a debug statement and see what the Vector numbers are like. If they go to somewhere around 5 and just stay there, then you can use a function that truncates your fraction to 4 instead of rounding back to 5. If it keeps on going down and eventually stops, then you might have to raise your friction coefficient.

    If you can’t find an easy ’rounding’ function, you could use (0.9 * Vector) – 1, subtracting 1 from your existing equation should do the same thing.

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

Sidebar

Ask A Question

Stats

  • Questions 90k
  • Answers 90k
  • 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 M-x occur? From the manual: M-x occur Prompt for a… May 11, 2026 at 6:04 pm
  • Editorial Team
    Editorial Team added an answer You should definitely check out Jon Rista's three-part series on… May 11, 2026 at 6:04 pm
  • Editorial Team
    Editorial Team added an answer Similar question How to use multiple tabs when tagging to… May 11, 2026 at 6:04 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

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.