Bouncing Problem.

Topics: Developer Forum, User Forum
Mar 6, 2010 at 12:29 PM
Edited Mar 6, 2010 at 12:30 PM

Hi all, this is the error I am having with my bouncing effect.

As you can see by this video, it will bounce once high once, then bounce low once. I cannot figure it out.

Here is the class for the platform.


using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Collisions;
using FarseerGames.FarseerPhysics.Factories;
using FarseerGames.FarseerPhysics.Dynamics.Springs;

namespace SimpleGame
    public class Platform
        public Body Body { get; set; }
        public float Width { get; set; }
        public float Height { get; set; }
        public Texture2D Texture { get; set; }
        public Geom Geom { get; set; }
        public Vector2 BounceForce { get; set; }

        public Platform(PhysicsSimulator PhysicsSimulator, Texture2D texture, Vector2 position, float width, float height, bool isStatic)
            if (width == 0) width = texture.Width;
            if (height == 0) height = texture.Height;

            this.Texture = texture;
            Body = BodyFactory.Instance.CreateRectangleBody(PhysicsSimulator, width, height, 1f);
            Body.Position = position;
            Body.IsStatic = isStatic;
            this.Geom = GeomFactory.Instance.CreateRectangleGeom(PhysicsSimulator, Body, width, height);

            this.Width = width;
            this.Height = height;


        public void Draw(SpriteBatch spriteBatch)
            if (Texture != null)
                spriteBatch.Draw(Texture, Body.Position, Color.White);

        public bool OnCollision(Geom geom1, Geom geom2, ContactList list)
            //geom1.Body.Position = new Vector2(geom1.Body.Position.X, 200);
            geom1.Body.ApplyImpulse( BounceForce);
 //geom1.Body.ApplyImpulse(BounceForce); return true; } } }

And here is how it is initialized.
        protected override void LoadContent()
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            P1 = new Platform(simulator, Content.Load<Texture2D>("Ground"), new Vector2(150, 400), 0, 0, true);
            P1.BounceForce = new Vector2(0, -500);

            P1.Geom.CollisionGroup = 1;
            P1.Geom.CollisionEnabled = true;

            //Load the circle texture
            circleTexture = Content.Load<Texture2D>("Circle");
            circleOrigin = new Vector2(circleTexture.Width / 2, circleTexture.Height / 2);

            //Create the circle body
            circleBody = BodyFactory.Instance.CreateCircleBody(circleTexture.Width / 2, 1);
            circleBody.Position = new Vector2(400, 300);


            //Create the circle geometry
            circleGeom = GeomFactory.Instance.CreateCircleGeom(circleBody, circleTexture.Width / 2, 48);
            circleGeom.RestitutionCoefficient = 0f;
            circleGeom.FrictionCoefficient = 0.0f;

            circleGeom.CollisionGroup = 2;
            circleGeom.CollisionEnabled = true;

            circleGeom.OnCollision += P1.OnCollision;

I would be grateful if anyone could figure it out.

Thanks all!


Mar 6, 2010 at 3:02 PM

Drakeshe, I see an onCollision event where you manually trigger an upwards bounce.

I'm not sure if you are going for a realistic bounce, but if you are, you should not be applying your own impulse upon OnCollision() and rely on the RestitutionCoefficient. The RestitutuionCoefficient is set to 0f in your code, that means the physicsSimulator is not applying any kind of bounce to your ball.


Mar 7, 2010 at 2:08 AM

I don't want a realistic bounce. I purposely want a set amount of bounce force.


Mar 7, 2010 at 10:49 PM

The problem is that you are applying a constant impulse, so when the ball drops from a high altitude most of the impulse goes into stopping it and there is not much leftover energy to bounce it very high, and when it drops from a low altitude the ball has a lower velocity and most of the impulse goes into the actual bounce.

What you need to do is take the velocity of the ball into account when applying the impulse like so:

geom1.Body.ApplyImpulse(BounceForce - (geom1.Body.Mass * geom1.Body.Velocity));

This way the ball will always bounce the same height, and if you want the ball to maintain it's horizontal velocity you can do:

geom1.Body.ApplyImpulse(BounceForce - (geom1.Body.Mass * new Vector2(0,geom1.Body.Velocity.Y)));
Mar 7, 2010 at 10:57 PM


And is there a way to stop rotation of the ball?


Mar 8, 2010 at 12:11 AM

thanks vulgivagus, I had a similar problem and was just about to ask about it, but Im working on a platform game, so for jumping I use apply impulse, but I noticed that sometimes he jumps like its supposed to be but sometimes it would jump lower, thanks. And about the rotation of the ball, sorry, no idea, not sure if you can restrict the angle with a fixed angle joint

Mar 8, 2010 at 11:39 AM

to prevent rotation, simply set the

ballGeom.MomentOfInertia = float.PositiveInfinity;

or something like that. It works great

Mar 8, 2010 at 11:45 AM


Now I just gotta figure out cameras and Im set.