SpriteBatch and slow simulation

Topics: Developer Forum
Feb 2, 2011 at 7:04 PM

Hello everyone,
I'm here with a very newbie question.
I've seen "HelloWorld" example, and this use an orthographic camera for the rendeing, this example works without any problems.
Then I've created a new project from scratch, and instead of an orthographic camera I used the simple SpriteBatch for the rendering. The project works, but very very slowly.
Here some of the project code: 

GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

World world = new World(new Vector2(0, 20));
Fixture barFixture;
Fixture blockFixture;

Texture2D barTexture;
Texture2D blockTexture;

//...

protected override void LoadContent()
{
    spriteBatch = new SpriteBatch(GraphicsDevice);

    barTexture = Content.Load<Texture2D>("barra");
    blockTexture = Content.Load<Texture2D>("blocco");

    barFixture = FixtureFactory.CreateRectangle(world, barTexture.Width, barTexture.Height, 1, new Vector2(50, 180));
    barFixture.Body.IsStatic = true;
    barFixture.Restitution = 0.3f;
    barFixture.Friction = 0.5f;

    blockFixture = FixtureFactory.CreateRectangle(world, barTexture.Width, barTexture.Height, 1, new Vector2(60, 10));
    blockFixture.Body.BodyType = BodyType.Dynamic;

    blockFixture.Restitution = 0.3f;
    blockFixture.Friction = 0.5f;
}

//...

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.Black);

    spriteBatch.Begin();
    spriteBatch.Draw(barTexture, barFixture.Body.Position, Color.White);
    spriteBatch.Draw(blockTexture, blockFixture.Body.Position, Color.White);
    spriteBatch.End();

    base.Draw(gameTime);
}

Any suggestions? Thanks.

Feb 2, 2011 at 7:16 PM

Sorry, I've forgotten an important piece of code:

protected override void Update(GameTime gameTime)
{
    world.Step((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f);

    base.Update(gameTime);
}

Developer
Feb 3, 2011 at 9:52 AM

Actually SpriteBatch uses an orthographic projection for rendering. Your problem is something else. You have to scale all units to MKS (meters kilos seconds) system. It is all over this discussion board, just search for MKS. Essentially that means your physics objects need to be in meters and you need to somehow scale between pixels and meters for your drawing code. In source control there is a new HelloWorld XNA sample, which shows you how it is done using the spritebatch.

Just take a look at this thread:

http://farseerphysics.codeplex.com/Thread/View.aspx?ThreadId=244014

Feb 3, 2011 at 9:54 AM

Hello everyone,
I think that this discussion and the code presented are the solution of my problems.

 

Feb 3, 2011 at 9:56 AM
Edited Feb 3, 2011 at 9:58 AM
Elsch wrote:

Actually SpriteBatch uses an orthographic projection for rendering. Your problem is something else. You have to scale all units to MKS (meters kilos seconds) system. It is all over this discussion board, just search for MKS. Essentially that means your physics objects need to be in meters and you need to somehow scale between pixels and meters for your drawing code. In source control there is a new HelloWorld XNA sample, which shows you how it is done using the spritebatch.

Just take a look at this thread:

http://farseerphysics.codeplex.com/Thread/View.aspx?ThreadId=244014

We wrote at the same time. ;-)
Thanks. 

Feb 9, 2011 at 6:44 AM

A simple solution to the slow simulation problem is to add a private field that holds the pixelToMeter ratio in your class. When you create your bodies you need to divide their width,height with this and when you draw you need to multiply the postions with this. An example/explanation of this approach can be found at http://www.progware.org/Blog/post/XNA-for-Windows-Phone-7-and-Physics.aspx