Help with body screen coordinates

Topics: Developer Forum, User Forum
Nov 2, 2010 at 5:54 AM

I need a very specific answers to this question, I have found my problem in the discussion boards but unfortunately no one ever gave exact answers.  The answers given where always vague and never specific.


Problem:  Need to take body of object and convert it's position into actual window/screen coords.  This same problem has been posted many times before with vague answers.  Give algorithm please!

The code below does not work.  I used 100 as an arbitrary multiplier,how do I calculate the correct multiplier?

int h = ScreenManager.ScreenHeight / 2;
int w = ScreenManager.ScreenWidth / 2;
if (lev1.IsActive)
                lev1.updateBall();//upates ball position
                spriteBatch.Draw(SpriteTexture, (new Vector2((lev1.BallPostion.X * 100) + h, -((lev1.BallPostion.Y * 100) + w))), null, Color.White, lev1.BallRotation, SpriteTextureOrigin, 1f, SpriteEffects.None,0f);
                spriteBatch.DrawString(Font, ""+ h + ","+w + lev1.BallPostion, new Vector2(20, 45), Color.White);

Nov 3, 2010 at 3:22 AM
Edited Nov 3, 2010 at 3:24 AM

Have you looked at the ConvertUnits class in the Demo Project?   It can be used to help solve your problem.


For example, if you want to put your ball at position (100,200) on the screen with a rotation of 90 degrees, you can do something like this:

ball.Body.SetTransform(ConvertUnits.ToFarseerCoords(new Vector2(100, 200)), MathHelper.ToRadians(90));



Now, you probably want the ball to be drawn wherever the physics engine updates it. For example, gravity might pull it down, or it might bounce off a wall, you want your ball sprite to be drawn at the correct location.

To do this, you must draw your ball where the physics body is. Something like this:


Vector2 position = ConvertUnits.ToDisplayUnits(ball.Body.Positon);

float rotation = MathHelper.ToDegrees(_farseerCollision.Body.Rotation);

spriteBatch.Draw(SpriteTexture, position, null, Color.White, rotation, SpriteTextureOrigin, 1f, SpriteEffects.None,0f);


Try it out. I am using something very similar in my game, except I have a slightly different coordinate system than the default XNA so it is a little bit different (small offset and inverted y-axis)