This project has moved and is read-only. For the latest updates, please go here.

physics problem with joints

Topics: Developer Forum
Sep 7, 2012 at 6:46 PM
Edited Sep 7, 2012 at 6:55 PM

Okay, so the point of this class is to make a scarf.

I am new to Farseer, so I had to work through a couple hiccups to get this working. Sort of.

I succeeded in making an array of joints and bodies that swing around the right way, but for some reason, the very last body (no matter how many bodies/joints I put) always tilts to the left, and swings the rest of the 'scarf' along with it (picture here).

I'm putting the code below, hopefully someone can point out the problem at first glance. I realize I'm new to this, so I'm happily accepting some feedback on organization and functionality of the code.

Thank you guys so much :)


using FarseerPhysics;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Joints;
using FarseerPhysics.Factories;
using FarseerPhysics.Collision;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;

namespace ShowYourColours_PC_
    class Scarf
        ContentManager content;
        World world;

        private AngleJoint[] joints = new AngleJoint[8];
        private Texture2D scarfTex;
        private Texture2D scarfWeightTex;
        private Body [] scarfBodies = new Body [9];
        private Vector2 scarfPos;
        private Vector2 scarfSize = new Vector2(0.2f, 0.15f);

        public Scarf(World world, Vector2 scarfPos, ContentManager content)
            this.content = content;
            this.scarfPos = scarfPos;
   = world;

            //Create all scarf pieces 
            for (int i = 0; i < scarfBodies.Length; i++)
                scarfBodies[i] = BodyFactory.CreateRectangle(world, scarfSize.X, scarfSize.Y, 1f, scarfPos + new Vector2(i * (scarfSize.X * (10f/20f)), 0f)); //20f is the X size of the scarf piece.
                scarfBodies[i].BodyType = BodyType.Dynamic;
                scarfBodies[i].CollisionCategories = Category.Cat1;
                scarfBodies[i].CollidesWith = Category.Cat2;

            scarfBodies[0].BodyType = BodyType.Static;
            //Create Joints (again, 2 for testing, planning array)
            for (int i = 0; i < joints.Length; i++)
                JointFactory.CreateRevoluteJoint(world, scarfBodies[i], scarfBodies[i + 1], new Vector2(i * (scarfSize.X * (10f / 20f)) * 0.01f, scarfSize.Y * 0.5f)); //20f is the X size of the scarf piece.


        private void LoadContent()
            scarfTex = content.Load<Texture2D>("scarfpixel");
            scarfWeightTex = content.Load<Texture2D>("circle");

        public void Update(GameTime gameTime)

        public void Draw(SpriteBatch spriteBatch)

            for (int i = 0; i < scarfBodies.Length; i++)
                spriteBatch.Draw(scarfTex, ConvertUnits.ToDisplayUnits(scarfBodies[i].Position), null, Color.White, scarfBodies[i].Rotation, new Vector2(scarfSize.X, scarfSize.Y) * 0.5f, 1f, SpriteEffects.None, 0f);


Sep 9, 2012 at 11:58 PM

I've not looked at your code, but usually this happens when you create your bodies too close to each other and then attach a revolute joint between them. The revolute joint tries to pull itself together and it results in tension to the left (because they are solved in a particular order).

I would recommend that you use the PathManager to create chains of bodies. See AdvancedDemo2 from the XNA samples on how to do this.