Bouncing ball does not always touch the floor

Jan 25, 2012 at 4:28 PM

Hi.

I made a simple thing for some little testing. I put a rectangle to imitate the floor, and then the circural object to imitate bouncing ball. The problem I have is that if you watch carefully you will notice the object(bouncing ball) does not always touch the floor. Watch(if you can't catch the ones that show the ball not touching the floor, then notice the last bounce):

http://youtu.be/eB6-3we4L3E

 

These 2 are just body projections using RenderXNA, not textures. Everything is done very simple, and according to samples. Like this:

 

//From Draw a little snipet
//initializing physics world
            _physicsWorld = new World(new Vector2(0, 40.0f))
            {
                ContactManager =
                {
                    BeginContact = BeginContact,
                    EndContact = EndContact,
                    PreSolve = PreSolve,
                    PostSolve = PostSolve
                }
            };

            Body ground = BodyFactory.CreateRectangle(_physicsWorld, ConvertUnits.ToSimUnits(ScreenManager._screenWidth), ConvertUnits.ToSimUnits(20), 0f, 
                new Vector2(ConvertUnits.ToSimUnits(ScreenManager._screenWidth / 2), ConvertUnits.ToSimUnits(ScreenManager._screenHeight - 10)));

            ground.BodyType = BodyType.Static;
            ground.IsSensor = false;

            Body ball = BodyFactory.CreateCircle(_physicsWorld, ConvertUnits.ToSimUnits(10), 0f,
                new Vector2(ConvertUnits.ToSimUnits(ScreenManager._screenWidth / 2), ConvertUnits.ToSimUnits(30)));

            ball.BodyType = BodyType.Dynamic;
            ball.IsSensor = false;
            ball.Restitution = 1f;

//Whole Update method
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            //physics world next step - variable time step but never less then 30 Hz
            try
            {
                PhysicsWorld.Step(Math.Min((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f, (1f / 30f)));
            }
            catch(Exception ex1)
            {
                var a = ex1;
            }

            _frameRateCounter.Update(gameTime);
            //time += gameTime.ElapsedGameTime.TotalMilliseconds;

#if RENDER
                RenderHelper.Update(gameTime);
#endif

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

Any reason why this is happening?

Lucas

Jan 25, 2012 at 5:39 PM

Also, how it is that this:

 

            Body ground = BodyFactory.CreateRectangle(_physicsWorld, ConvertUnits.ToSimUnits(ScreenManager._screenWidth), ConvertUnits.ToSimUnits(30), 0f, 
                new Vector2(ConvertUnits.ToSimUnits(ScreenManager._screenWidth / 2), ConvertUnits.ToSimUnits(ScreenManager._screenHeight - 15)));

            ground.BodyType = BodyType.Static;
            ground.IsSensor = false;

            Body ball = BodyFactory.CreateCircle(_physicsWorld, ConvertUnits.ToSimUnits(BallTexture.Height / 2), 0f,
                new Vector2(ConvertUnits.ToSimUnits(ScreenManager._screenWidth / 2), ConvertUnits.ToSimUnits(40)));

            ball.BodyType = BodyType.Dynamic;
            ball.IsSensor = false;
            ball.Restitution = 1f;

Causes the ball to bounce off IDEALLY right of the floor body, but if I change the ball position that it drops from, to say 30px from top instead of 40, then the ball "bounces" off of the air?

 

Lucas

Jan 25, 2012 at 9:59 PM

Can  you slow the ball down a bit (reduce gravity) and test again. Also increase engine precision - more velocity interations, more position iterations. Might be a tunnelling problem.

Also why does your ball have 0 density? That surely doesn't seem right.

Jan 25, 2012 at 10:16 PM

Thank you jerrysb for the willingness to help!

 

I changed the codes in question to:

//initializing physics world
            _physicsWorld = new World(new Vector2(0, 10.0f))
            {
                ContactManager =
                {
                    BeginContact = BeginContact,
                    EndContact = EndContact,
                    PreSolve = PreSolve,
                    PostSolve = PostSolve
                }
            };

            BallTexture = Content.Load("SpriteItems/football");
            GrassTexture = Content.Load("SpriteItems/grass");

            Body ground = BodyFactory.CreateRectangle(_physicsWorld, ConvertUnits.ToSimUnits(ScreenManager._screenWidth), ConvertUnits.ToSimUnits(30), 1f, 
                new Vector2(ConvertUnits.ToSimUnits(ScreenManager._screenWidth / 2), ConvertUnits.ToSimUnits(ScreenManager._screenHeight - 15)));

            ground.BodyType = BodyType.Static;
            ground.IsSensor = false;

            Body ball = BodyFactory.CreateCircle(_physicsWorld, ConvertUnits.ToSimUnits(BallTexture.Height / 2), 1f,
                new Vector2(ConvertUnits.ToSimUnits(ScreenManager._screenWidth / 2), ConvertUnits.ToSimUnits(40)));






        /// 
        /// The number of velocity iterations used in the solver.
        /// 
        public static int VelocityIterations = 16;

        /// 
        /// The number of position iterations used in the solver.
        /// 
        public static int PositionIterations = 6;

        /// 
        /// Enable/Disable Continuous Collision Detection (CCD)
        /// 
        public static bool ContinuousPhysics = true;

No change. Still The ball is bouncing off the air here and then.

 

ps. Forgot to mention before. Using XNA 4.0 with silverlight.

Lucas

Jan 26, 2012 at 4:34 PM
jerrysb wrote:

Can  you slow the ball down a bit (reduce gravity) and test again. Also increase engine precision - more velocity interations, more position iterations. Might be a tunnelling problem.

Also why does your ball have 0 density? That surely doesn't seem right.

I looked today very closely into differences between my code and the code from the samples, as one thing that seemed curious to me was why I have 27-28 fps, and in the farseer samples it's closed to 60, when I am not doing anything that requires a lot of processing power. I also noticed I had exactly same farseer settings as the samples project, so it must have been something else.

 

After long search I found out I was making very silly mistake of using a Fixed Time Step. When I set it to false, the fps speeded up tremendously, which helped the behaviour of the whole physics model and also the behaviour of the ball. It does not bounce off from the air anymore, at least not that visably.

 

Hopefully that is the problem that caused it :)

 

ps. jerrysb, thanks for your help.