I have created a circle body class, but in game my player body just passes through it. the circle will not move, only rotate.

however when i create the circle body just straight up, raw in the game1 load content method, without using a personalised class, it works. so im thinking i have done something wrong but i cant for the life of me spot it.

here are the two different implementations, the first is the class and its instance in the game1 class, THAT doesn't want to move the circle around or collide properly, the second is the classless implementation directly in the load content method of the game1 class.

    class CircleBody
    Texture2D Texture;
    Vector2 Origin;
    Vector2 Position;
    public Body Circle;

    public CircleBody(Texture2D texture, Vector2 position, World world)
        this.Texture = texture;
        this.Origin = new Vector2(this.Texture.Width / 2f, this.Texture.Height / 2f);
        this.Position = new Vector2(ConvertUnits.ToSimUnits(position.X), ConvertUnits.ToSimUnits(position.Y));
        Circle = BodyFactory.CreateCircle(world, ConvertUnits.ToSimUnits(56 / 2), 1f, this.Position);
        Circle.BodyType = BodyType.Dynamic;
        Circle.Restitution = 0.3f;
        Circle.Friction = 0.5f;

    public void Draw(SpriteBatch spriteBatch)
        spriteBatch.Draw(this.Texture, ConvertUnits.ToDisplayUnits(this.Position), null, Color.White, this.Circle.Rotation,
            this.Origin, 1f, SpriteEffects.None, 0f);

here is the above class instanced and implemented in the game1 class:


        CircleBody CircleBody;
        Texture2D CircleTexture;

Load Content Method:

    CircleTexture = Content.Load<Texture2D>("Player");
    CircleBody = new CircleBody(CircleTexture, new Vector2(400, 300), world);



it just doesn't work :(,

HOWEVER it works perfectly when I create it in a different way, as in not through my own class, but raw within the game1 class. here is the code.


     Texture2D playerTex;
     Vector2 playerOrigin;
     Body player;

LoadContent Method

    playerTex = Content.Load<Texture2D>("Player"); // 
    playerOrigin = new Vector2(playerTex.Width / 2f, playerTex.Height / 2f);
    /* Circle */
     Convert screen center from pixels to meters
    Vector2 playerPosition = ConvertUnits.ToSimUnits(screenCenter); 
     Create the player fixture
    player = BodyFactory.CreateCircle(world, ConvertUnits.ToSimUnits(56 / 2), 1f, playerPosition);
    player.BodyType = BodyType.Dynamic;
    player.Restitution = 0.3f;
    player.Friction = 0.5f;


   spriteBatch.Draw(playerTex, ConvertUnits.ToDisplayUnits(player.Position), null, Color.White, player.Rotation,
                playerOrigin, 1f, SpriteEffects.None, 0f);

The first method through the class is obviously the cleaner and perfect way i want to implement the creation of a circle based body sprite combination, but i dont understand why the sprite is not moving, or collidable or seemingly alive in the simulation.

and i really dont see why it works raw in game1 class.

i really hope you can help!!! im sooooo stuck


Change this line:

Vector2 Position;

To this:

Vector2 Position { get { return Circle.Position; } set { Circle.Position = value; } }

And then pass your new Vector2 directly into BodyFactory.CreateCircle.

What is happening here is that Vector2 is a "value type" and not a "reference type".

When you pass a value type (as you do to the BodyFactory.CreateCircle), it takes a copy of that value type, leaving the original alone. So your Position variable is not being modified when the Farseer body moves.

(Whereas, if Vector2 were a reference type, then it is the reference that gets copied - not the object instance the reference refers to. So then you might expect the Body class to keep your object instance updated - although the exact behaviour would be defined by Body.)

In this case, the simplest work-around would be to change Position from being a variable, to a property that simply access the correct Vector2 out of the Body class.

An alternative might be just access Circle.Position directly.

| improve this answer | |
  • \$\begingroup\$ Thank you so much. i dont know why i didn;t notice that but im certainly glad you did and coudl help m. \$\endgroup\$ – Qas Piano Dec 15 '13 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.