8 Replies - 3026 Views - Last Post: 22 October 2011 - 03:32 PM

#1 ConBon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-October 11

Ball bouncing under gravity- should be simple?

Posted 21 October 2011 - 07:04 AM

The code I have written should, by my logic, allow the ball to be given a horizontal/vertical velocity and bounce inside the bounds of the window off each "wall" and come to rest on the ground. There seem to be a number of problems which I just can't seem to reason out and fix.

Firstly, if given a vertical velocity upwards(a minus value), the ball just rapidly gains momentum and disappears out of the screen. This shouldn't be allowed according to the lines:

if (position.Y < MinY)
	//Move the ball out of collisionposition.Y = MinY;


Secondly, when given a minus horizontal velocity the ball bounces to the left hand side of the screen but gets stuck there and bounces on the x axis until coming to rest.

Finally, whenever the ball is given a horizontal velocity and describes an arc the ball graphic is very jittery back and forth.

 public void Update(GameTime gameTime, GraphicsDeviceManager graphics)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                released = true;
                initialVel = 0f; // set a velocity of 0 pixels per second
            }
            if (released == true)
            {
                //Calls the method to animate the sprite;
                SpriteSheetAnim(gameTime, graphics); 

                if (initialVelHoriz > 0)
                {
                    if (position.X > MaxX) 
                    {
                        //Move the ball out of collision
                        position.X = MaxX;

                        //Multiply the velocity by the coefficient of resitution of the bouncing ball
                        initialVelHoriz = initialVelHoriz*horizRestitution;

                    }
                    //velocity equation (s = ut + 1/2at^2) for HORIZONTAL component
                    //since there is no acceleration, the second half of the equation is left out
                    position.X += initialVelHoriz * (timer / 1000);
                }
                else if (initialVelHoriz < 0)
                {
                    if (position.X < MinX)
                    {

                        //Move the ball out of collision
                        position.X = 0;

                        //Multiply the velocity by the coefficient of resitution of the bouncing ball
                        initialVelHoriz = initialVelHoriz * horizRestitution;

                    }

                    //velocity equation (s = ut + 1/2at^2) for HORIZONTAL component
                    //since there is no acceleration, the second half of the equation is left out
                    position.X += initialVelHoriz * (timer / 1000);
                    
                }

                if (initialVel >= 0)
                {
                    if (position.Y > MaxY)
                    {

                        //Move the ball out of collision
                        position.Y = MaxY;

                        //Multiply the velocity by the coefficient of resitution of the bouncing ball
                        finalVel = finalVel*restitution;

                        //Everytime ball hits ground the horizontal speed will be reduced due to friction
                        initialVelHoriz *= 0.9f;

                        if (finalVel > 0)
                        {
                            //using velocity equation with negative factor on gravity because of going in opposite direction
                            //finalVel will decrease in value
                            //velocity equation (v = u + at) for VERTICAL component
                            finalVel -= initialVel + (-accelDueToGrav*(timer/1000));
                            position.Y -= finalVel;
                        }
                    }
                    else
                    {
                        //velocity equation (v = u + at) for VERTICAL component
                        finalVel += initialVel + (accelDueToGrav * (timer / 1000));
                        position.Y += finalVel; 
                    }
                }
                if(initialVel < 0)
                {
                    if (position.Y < MinY) 
                    {
                        //Move the ball out of collision
                        position.Y = MinY;

                        //Multiply the velocity by the coefficient of resitution of the bouncing ball
                        finalVel = finalVel * restitution;
                        
                        //Everytime ball hits the roof the horizontal speed will be reduced due to friction
                        initialVelHoriz *= 0.9f;

                        if (finalVel > 0)
                        {
                            //using velocity equation with negative factor on gravity because of going in opposite direction
                            //finalVel will decrease in value
                            //velocity equation (v = u + at) for VERTICAL component
                            finalVel += initialVel + (accelDueToGrav * (timer / 1000));
                            position.Y += finalVel;
                        }
                    }
                    else
                    {
                        //velocity equation (v = u + at) for VERTICAL component
                        //Gravity will be in the opposing direction so is given a negative value
                        finalVel += initialVel + (-(accelDueToGrav)*(timer/1000));
                        position.Y += finalVel;
                    }
                }
            }
        }




Is This A Good Question/Topic? 0
  • +

Replies To: Ball bouncing under gravity- should be simple?

#2 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1357
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Ball bouncing under gravity- should be simple?

Posted 21 October 2011 - 07:19 AM

Nothing really jumps out at me as being done incorrectly. Have you placed breakpoints to check the values of all of your velocities and restitution?

Also, your second point you said that if there was a negative horizontal velocity, the ball just moves to the left side of the screen and bounces against the wall and stays there once it stops bouncing. This sounds like intended behavior to me as a negative horizontal velocity tells the object to move left along the X axis. Perhaps I'm misunderstanding what you meant. Can you clarify?
Was This Post Helpful? 0
  • +
  • -

#3 ConBon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-October 11

Re: Ball bouncing under gravity- should be simple?

Posted 21 October 2011 - 08:54 AM

No, I haven't tried breakdowns yet. Should probably do that. But with regards to theinitial horizontal velocitybeing given a negative value. This is to 'shoot' the ball to the left and it should then fall in a curve under gravity, bouncing off the wall and and so making the velocity a positive number, bouncing the ball to the right until it comes to rest due to friction and gravity.
Thanks for the input. I know the solution will be simply but i just can't see it :)
Was This Post Helpful? 0
  • +
  • -

#4 bonyjoe  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 178
  • View blog
  • Posts: 548
  • Joined: 08-September 10

Re: Ball bouncing under gravity- should be simple?

Posted 21 October 2011 - 10:21 AM

When you throw a ball up in the air gravity does not turn negative, so why does your gravity turn negative?
Was This Post Helpful? 0
  • +
  • -

#5 ConBon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-October 11

Re: Ball bouncing under gravity- should be simple?

Posted 22 October 2011 - 04:04 AM

When travelling upwards gravity is acting in the opposite direction... negative?
Was This Post Helpful? 0
  • +
  • -

#6 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1357
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Ball bouncing under gravity- should be simple?

Posted 22 October 2011 - 02:57 PM

Gravity still pulls down on the object, no matter which direction it is traveling. The upward force needs to be enough to overcome the power of the gravity, otherwise you wouldn't actually go up. Simulating realistic gravity in a simulation can get quite complicated.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 11315
  • View blog
  • Posts: 42,632
  • Joined: 27-December 08

Re: Ball bouncing under gravity- should be simple?

Posted 22 October 2011 - 03:02 PM

I have a tutorial on the math behind 2D Gravity that you may find helpful. :)
Was This Post Helpful? 2
  • +
  • -

#8 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1357
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Ball bouncing under gravity- should be simple?

Posted 22 October 2011 - 03:05 PM

Great idea mac, I had forgotten about that one.
Was This Post Helpful? 0
  • +
  • -

#9 ConBon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-October 11

Re: Ball bouncing under gravity- should be simple?

Posted 22 October 2011 - 03:32 PM

Thanks for all the replies guys.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1