This project has moved. For the latest updates, please go here.

Dynamic body sticking

Nov 4, 2010 at 3:13 AM

I am trying to apply a force using the left thumbstick.X on a dynamic body that is sitting on a static platform.  The gravity is set to 9.8f.  When I slide it along the platform it slides perfectly some of the time but every so often it is getting stuck on the seam between to other bodies.   Any assistance with this is appreciated. Here is the code,

Loading my objects from my Game1:

protected override void LoadContent()
            spriteBatch = new SpriteBatch(GraphicsDevice);
            world = new World(new Vector2(0.0f, 9.8f));
            platform = new GameObject[6];

            int posX = 0;
            int posY = 250;

            for (int i = 0; i < platform.Count(); i++)
                platform[i] = new GameObject(ref world, Content, "sprites/yellow", 100.0f, 1.0f, new Vector2(posX, posY), false, 0.0f);
                posX += 100;

            blueBlock = new GameObject(ref world, Content, "sprites/blue", 100.0f, 1.0f, new Vector2(250, 50), true, 0.0f);

And here is the GameObject class:

class GameObject
        Fixture fixture;
        Texture2D texture;
        float pixScale;
        Vector2 texturePos;

        public GameObject(ref World world, ContentManager content, string texturePath, float pixelScale, float density, Vector2 position, bool isDynamic, float friction)
            pixScale = pixelScale;
            texture = content.Load<Texture2D>(texturePath);

            Rectangle rect = texture.Bounds;
            Vertices verts = new Vertices();
            verts.Add(ScalePositions(new Vector2(rect.Left, rect.Top)));
            verts.Add(ScalePositions(new Vector2(rect.Right, rect.Top)));
            verts.Add(ScalePositions(new Vector2(rect.Right, rect.Bottom)));
            verts.Add(ScalePositions(new Vector2(rect.Left, rect.Bottom)));

            fixture = FixtureFactory.CreatePolygon(world, verts, density);
            fixture.Body.Position = new Vector2(position.X / pixScale, position.Y / pixScale);

            if (isDynamic)
                fixture.Body.BodyType = BodyType.Dynamic;
                fixture.Body.FixedRotation = true;
                fixture.Body.LinearDamping = 2.0f;
                fixture.Body.BodyType = BodyType.Static;

            fixture.Friction = friction;

        public void Update(GameTime gameTime, GamePadState currState, GamePadState prevState)
            if (fixture.Body.BodyType == BodyType.Dynamic)
                fixture.Body.ApplyForce(new Vector2(currState.ThumbSticks.Left.X, 0.0f));

                if (currState.Buttons.A == ButtonState.Pressed)
                    fixture.Body.ApplyForce(new Vector2(0.0f, -5.0f));

            texturePos = new Vector2(fixture.Body.Position.X * pixScale, fixture.Body.Position.Y * pixScale);

        public void Draw(SpriteBatch sBatch)
            sBatch.Draw(texture, texturePos, null, Color.White, fixture.Body.Rotation, fixture.Body.LocalCenter, 1.0f, SpriteEffects.None, 0);

        public Vector2 ScalePositions(Vector2 rect)
            return rect / pixScale;

Nov 4, 2010 at 4:24 AM
Edited Nov 4, 2010 at 4:24 AM
Before I look at your code, I just want to say that it helps if you provide a video of the problem if you can. On a personal note, I looked into CannedGames because the name is similar to mine coincidentally, and if is yours, then I find it admirable that you are working with your family on games. Your sister (assuming that the one posting this is Adam) is a good artist, which works out great. Good luck!
Nov 4, 2010 at 9:32 AM

Try disabling CCD - it has some issues with snagging. Another solution is to merge the polygons to larger ones or use edge adjacency. Look at the edge test in the Testbed for more info about edges.