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

Sprite's not Relative to Physics

Topics: Developer Forum
Feb 16, 2011 at 1:22 AM
Edited Feb 16, 2011 at 1:23 AM

What happens is when i move the player or move the camera, all the physics objects move faster then my spritebatch objects so everthing comes out of position.


I guess this is because there not linked up correctly?

I was thinking that it could be to do with my camera and how its setup? but once i added the player and moved him forward the player sprite didn't follow the physics box correctly.

This is proberly a stupid problem, but I'm not a great programmer however i don't want to give up on Farseer as its a great engine and really adds a great dynamic to the game I'm making.



Feb 16, 2011 at 1:40 AM
Edited Feb 16, 2011 at 3:05 AM

Ok, I've looked though a few old forum posts and i think its todo with ConvertUnits.

I think i need to convert my Farseer based objects so that they work the same as Xna's Vectors. I am correct?

However the only post's i can see, make the object based upon the Texture Height and width, and what im tryin to do is, create a physic's box and then make the Sprite render to it position. The reason for this is because my Sprite is an animated Sprite and is not a Texture2D so i can't use the template's i have found.

If there is anyone who knows of a solution it would help me out so much, as once this glitch is sorted i will have a whole engine to use in Xna that use's Gleed2D and Farseer, and can get back making the game.

I really don't know how much sense i am talking to you guys as i've only just picked up c# and Farseer.



Feb 16, 2011 at 4:17 AM
Edited Feb 17, 2011 at 2:21 AM

my last post of the night, i know im just talking to myself however i keep finding out more and im getting closer.

At the moment, i have linked my player to the body, however when the start the game the sprite goes in the oppersite direction.

So the physics object lands on the ground but the sprite floats upwards :(


Here my player code:


 public Player(World _world, Vector2 position, float width, float height, float mass, Texture2D texture)
           this.texture = texture;
            this.origin = new Vector2(texture.Width / 2, texture.Height / 2);
            this.position = position ;
            this.width = width;
            this.height = height;          
            idleAnimation = new Animation(Game1.PlayerIdle, 0.1f, true);
            walkAnimation = new Animation(Game1.PlayerWalk, 0.1f, true);
            jumpAnimation = new Animation(Game1.PlayerJump, 0.1f, false);
            dieAnimation = new Animation(Game1.PlayerDie, 0.1f, false);
            animation = idleAnimation;

            SetupPhysics(_world, position, width, height, mass,texture);

        protected virtual void SetupPhysics(World _world, Vector2 position, float width, float height, float mass, Texture2D texture)

            character = FixtureFactory.CreateRectangle(_world, width, height, mass, position);
            character.Body.BodyType = BodyType.Dynamic;
            character.Body.Position = position / ConvertUnits.DisplayUnits;
            character.Friction = 0f;
            character.Restitution = 0.00f;

            Joint rotationJoint = JointFactory.CreateFixedAngleJoint(_world, character.Body);

            character.OnCollision += new OnCollisionEventHandler(OnCollision);

        public virtual void Draw(GameTime gT, SpriteBatch SB)

            SpriteEffects flip = direction > 0 ? SpriteEffects.FlipHorizontally : SpriteEffects.None;

            sprite.Draw(gT, SB, new Vector2((ConvertUnits.ToDisplayUnits(character.Body.Position.X)) , (ConvertUnits.ToDisplayUnits(character.Body.Position.Y))), SpriteEffects.None);


        public virtual Vector2 Position
                return character.Body.Position;




Feb 17, 2011 at 1:28 AM

Hello Luke,

I'm learning myself but so far this is what I've gathered since you are using the camera... I'm not sure if I'm 100% correct so don't go ripping and replacing without a backup.

To set the body position use:   character.Body.SetTransform(Camera2D.ConvertScreenToWorld(position), 0);

Then when you draw you have to convert back to screen:   sprite.Draw(gT, SB, Camera2D.ConvertWorldToScreen(character.Body.Position), SpriteEffects.None);

I believe that should do it, hope that helps.
If not I'm sure one of the big dogs will jump in and we'll both learn something :)

Best of luck,


Feb 17, 2011 at 2:08 AM
Edited Feb 17, 2011 at 2:09 AM

Cheers Nick,

Thats sorted the player from going up instead of down, however his still out by quiet abit and is slower then the physics box.  I found another post with the problem and tried this

const float pixelsPerMeter = 100.0f;

LoadContent     character = FixtureFactory.CreateRectangle(physicsWorld, width / pixelsPerMeter, height / pixelsPerMeter, 1);
character.Body.Position = new Vector2(character.Body.Position.X / pixelsPerMeter, character.Body.Position.Y / pixelsPerMeter);

Update          playerPosition.X = character.Body.Position.X * pixelsPerMeter;
                playerPosition.Y = character.Body.Position.Y * pixelsPerMeter;

Draw sB.Draw(exture, character.Body.Position, Color.White);

However this didn't seem to work either.


I managed to make my physics move at the same rate as my SpriteBatch's when moving my Camera by adding an Effect

            projection = Matrix.CreateOrthographicOffCenter(-20, 20, 25, -25, 0, 1);

            view = Matrix.CreateRotationZ(0f) * Matrix.CreateTranslation(new Vector3(-Camera.Position,0));

            effect.Projection = projection;
            effect.View = view;
            effect.TextureEnabled = true;
            effect.VertexColorEnabled = true;

            sb.Begin(0, null, null, null, null, effect);

            player.Draw(gameTime, sb);
            _debugView.RenderDebugData(ref projection, ref view);


This did make my physics move at the same speed as my spritebatch's, however its was really zoomed in and my physics boxs were drawn really far away so i think that Matrix.CreateOrthographicOffCenter is quiet off too.


If anyone can see what we're doing wrong or has a sample of Spritebatch's working with Farseer 3.2 that would be amazing.



Feb 17, 2011 at 3:25 AM

Oops, I forgot to mention how I was using Begin()...

spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied, null, null, null, null, Camera2D.View);
I'm also passing the Camera's zoom to the spritebatch.draw scale. 

Everything works for me with the previous code I posted as long as I set the Camera.Zoom to 1f or less.
It works, but I don't feel warm and fuzzy because I don't understand the correlation between ConvertUnits.SetDisplayUnitToSimUnitRatio(whatever) and the draw logic. I want a Rectangle body 26px wide by 30px high to translate 1:1 when the camera's zoom is 1 but I've been fudging the fixture constructor until the body and the sprite are really close. I feel I'm close but so far away :)

Sorry in advance if I'm giving horrible advice...


Feb 17, 2011 at 4:02 AM


Lol at the noob, that fixed it. Your advice is exactly what i needed to fix my problem, Thank You

You proberly already know this, but ConvertUnits i think are the units to which farseer use's which is normally set to 100.0f, So in Farseer 1 pixel equal 100units so thats why object appear off or move quicker.

As for zoom i wouldn't know where to begin sorry.


If i do see anything or think of anything that might relate to your problem ill be sure to post.


Thank You


Feb 17, 2011 at 5:07 AM

Sweet!  Don't worry, I consider myself a noob too!  I'm alot more confortable with Farseer 2.x, I'm still getting my 3.0 legs.   Make sure you download the testbed projects too, they've helped me immensely just by poking around in those.
I didn't know the exact logic behind ConvertUnits, from reading I thought it had to do with converting pixels to millimeters which I thought had more to the equation like DPI and other fun stuff.  I'm still not getting the 1:1 with the unit conversions (although it's close).  Maybe I'm not using the Camera2D class properly?!  I didn't alter any code in it so I'm not sure.  Eventually the light will go on :)

Either way I'm happy you're up an running, nice screens btw.

Happy coding!