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

How to apply a texture / sprite on the body

Dec 6, 2010 at 3:42 PM

Hi all, Sorry for my english, I'm from Russia.

How can you apply the texture on the body in XNA? I searched everywhere but could not find. Tried draws the sprite on the coordinates of the body,

spriteBatch.Draw(Stone, _circleFixture.Body.Position, Color.White);

but it appears in the upper left corner.

Maybe it is possible to paint the body?

Dec 6, 2010 at 3:51 PM

The circle you see on the screen and the spritebatch use different methods to render on the screen. 

For example, you set the circle at the position (1,1). If you're using spritebatch, that 1,1 will be drawn in the upper left corner. But if you're using the camera to render it, if the camera points at (0,0), (0,0) will be the center of the screen, and the circle will be drawn around there.

Dec 6, 2010 at 4:11 PM

Sorry for the stupid question, but what a camera? May be necessary to transfer meters into pixels?

Dec 6, 2010 at 4:21 PM

Exactly ;)

The body's position is in meters whereas spritebatch coordinates are in pixels. Therefore if your body moves 1 meter your sprite just moves one pixel. If you copy pasted the Hello World sample i think 1 meter corresponds to 100 pixels. So for starters try multiplying the body's position by 100. You also have to take a look at whether the y-axis is pointing upwards or downwards and were your point of origin is in both systems.

Dec 6, 2010 at 4:26 PM

As Elsch said, you have to convert it first (I forgot to tell you about that. LOL.)


For camera, take a look at this tutorial.