开发者

2D Game Actor coordinates vs screen coordinates

I'm developing a game in XNA which uses the Farseer physics engine, essentially a 2d platformer. I'm having trouble resolving the method of drawing sprites vs my desired units for the physics engine.

For example, I have a sprite for a person, the physics shape is simply 0.8 x 1.8 (metres), and is the position is also in metres. Makes gravity and real-world scaling very simple. (I like my origin at the bottom left too, but I can handle thinking upsidedown)

However, the standard way of rendering in 2d is SpriteBatch.Draw, which work on a 1:1 pixel ratio, with the origin on the top left.

What's the standard way of reconciling this? I initially just manually worked out the scaled p开发者_如何转开发osition/size of the rectangle, and used the source/destination params of SpriteBatch, but it seems dodgy and slow. Ideally I'd like to set up a projection matrix and then just draw everything using the info from the physics objects. Alas, all the rectangles it takes are ints, not floats, so it doesn't work so well...

In another project (OpenGL), I simple set up my view matrix to be the appropriate size, and rendered quads with textures on them. Is that how I should approach it in XNA as well? Or is there some secret method that people using XNA have?


I think what you're looking for is "camera virtualization", there is a good tutorial here > http://www.david-amador.com/2009/10/xna-camera-2d-with-zoom-and-rotation/


I ended up just creating a set vertices to make a textured quad from (-0.5, -0.5) to (0.5, 0.5), and scaling/translating it to the appropriate position when I draw it.

This gives 1:1 draw/physics object ratio, makes everything easy. I'll run into problems with sprite size at different resolutions, but that's a small price to pay I think - and circumvented by being careful with the sprite size.

I'll mark this as the answer if someone doesn't come up with a better solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜