Units and lengths

Topics: User Forum
Jan 6, 2011 at 1:49 PM
Edited Jan 6, 2011 at 1:54 PM

Hi

I can't properly draw my sprites using XNA + Farseer..

For converting units(and I think that its here my error is) I got this code

 

public float ToMeters(float pixels)
{
      return pixels / 50;
}

public float ToScreenPosition(float meters)
{
      return meters * 50;
}

 

1m = 50pixels.

And here is the code setting up the phyics bodies

 

// create ground
_rectFix = FixtureFactory.CreateRectangle(_world, ToMeters(572), ToMeters(59), 1, new Vector2(0, ToMeters(200)));
_rectFix.Body.IsStatic = true;
_rectFix.Restitution = 0.3f;
_rectFix.Friction = 0.5f;

// create crate
_crateFix = FixtureFactory.CreateRectangle(_world, ToMeters(100), ToMeters(100), 1, Vector2.Zero);
_crateFix.Body.BodyType = BodyType.Dynamic;
_crateFix.Restitution = 0.3f;
_crateFix.Friction = 0.5f;

 

and at last, here is how I draw them;

 

ground.Position.X = ToScreenPosition(_rectFix.Body.Position.X);
ground.Position.Y = ToScreenPosition(_rectFix.Body.Position.Y);
ground.Draw();

crate.Position.X = ToScreenPosition(_crateFix.Body.Position.X);
crate.Position.Y = ToScreenPosition(_crateFix.Body.Position.Y);
crate.Draw();

 

The problem is that there is an empty space between the ground and the crate when they collide. So it looks like the crate is just haning in the air above the ground...

 

Edit:

This is how I calculate the sprites origin in my "engine";

this._origin.X = this._img.Width / 2;
this._origin.Y = this._img.Height / 2;
Where "this._img" is the Texture2D loaded from Content.Load<Texture2D>

Jan 6, 2011 at 6:50 PM

Why are you converting the units? Are they doing this in the samples?

Jan 6, 2011 at 7:39 PM

No, this isnt from the sample. 

But I read that Box2D / Farseer can't handle dynamic-bodies with a size without the range of 0.1m to 10m soo I was thinking like;

1pixel = 1m - that makes a screen 800pixels wide. See my problem?

Developer
Jan 6, 2011 at 9:42 PM

You are right about the scaling and the Samples do that too btw.

Also your code looks good to me. Seeing the rest of the code might help. Can you post it somewhere or send it to me? Then I'll have a look at your drawing code.