XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

bug in a simple falling ball simulation

Last post 04/07/2009 19:55 by Steve Roe. 4 replies.
  • 04/07/2009 15:51

    bug in a simple falling ball simulation

    hi.
    I'm beginning to learn XNA and just experimenting w/ things so I decided to make a simple falling ballsimulation that will have gravity, friction, collision and other things but in the meanwhile I only have gravity and it doesn't work good, every time it hits the floor the ball loses some speed and height, it becomes very problematic once I introduce a bouncability value-even if I set it to 0.9 it loses all the height after 3-4 jumps.
    the code is:
    protected override void Update(GameTime gameTime) 
            { 
                foreach (Ball ba in balls) 
                { 
                    ba.velocity.Y += gameTime.ElapsedGameTime.Milliseconds * (gravity/1000); 
                    ba.position += ba.velocity; 
                    hitWall(ba); 
                } 
            } 
     
            private void hitWall(Ball ba) 
            { 
                ba.UpdateBounds(); 
                if (!viewPortRect.Contains(ba.bounds)) 
                { 
                    //bounce 
                    ba.velocity.Y = ba.velocity.Y * -1 *ba.bouncability; 
                } 
            } 

    one more thing: when the ball is slow and touches the floor it gets stuck halfway and moves a little up and down, how can I prevent it?
  • 04/07/2009 16:38 In reply to

    Re: bug in a simple falling ball simulation

    Hello,

    My opinion, not enough info provided for help. Obviously a bouncing ball will loose some of its height value as it bounces, and assuming from your brief code snip, you are also using angles for the ball collision against....what the viewport? As far as "bouncability," I suppose you are referring to different properties of say bouncability of a brick vs. a tennis ball? I think more info is needed to correctly provide adequate guidance, again my opinion.
  • 04/07/2009 17:07 In reply to

    Re: bug in a simple falling ball simulation

    probably forgot to mention it, even if I set the boucability to 1.0f (or if I remove this part of the code) it still happens.
    right now game is just bouncing balls that go up and down (just now began doing sideways which leads to the next problem) and nothing more than that.
    I found out that the problem is in the if statement and when I change it to "!viewPortRect.Contains(ba.bounds)" but that means that if the ball hits the side of the windows it resets itself to the bottom and keeps going to the side out of the window. why does the original statement (viewPortRect.Bottom <= ba.position.Y + ba.sprite.Height) doesn't work? or how can I check if the ball hits the side or the bottom?
  • 04/07/2009 17:12 In reply to

    Re: bug in a simple falling ball simulation

    It is generally the case that a first-order integrator (like in the code) will lose or gain energy over time. That's an unavoidable effect of the simple discrete integration. If you want perfect harmonic motion, you need to use a higher-order integrator, like Runge-Kutta-4. Note that with RK4, you also need to be able to come up with the proper derivatives for three points in time during the timestep, not just one; that makes collision detection a lot harder!
    Try this: If you set "bouncability" (also known as "restitution" in physics circles) to 1.1, your ball should gain energy with each bounce. If it doesn't, then you're doing something else wrong, like calling movement more often than integration, or something like that.

    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 04/07/2009 19:55 In reply to

    Re: bug in a simple falling ball simulation

    The ball might be getting stuck to the floor because hitWall reverses the ball velocity whenever the ball is out of bounds, even if it was out of bounds in the previous time tick.  If the ball doesn't bounce all the way back into the viewport in one time tick, hitWall could reverse direction again, pushing the ball farther out of bounds.

    Maybe hitWall could only reverse direction if the ball is out of bounds and if the current velocity is carrying it farther out of bounds.  If the ball is out of bounds, but is heading back in bounds, there's no need to reverse the velocity.

    Going to a higher order Runge-Kutta simulation is the best way to get more accurate motion, but swapping the position update and call to hitWall might help some.  Right now, the code updates velocity, updates position, then updates velocity again.  It would be cleaner to do both velocity updates before the position update.
    Three colors, endless fun ... Primary Attack
Page 1 of 1 (5 items) Previous Next