Farseer Meter/Pixel Ratio

心不动则不痛 提交于 2019-12-08 10:26:47

问题


I've done a lot with box2d in c++, and am giving C# a try. It looks like Farseer is generally used in place of Box2D (I'm aware of Box2DXNA, but it seems a little outdated.) So, Farseer is what I've been using. When I was using C++ and Box2D, everyone always advised against using a 1pixel/meter ratio (For what reason, I don't know,) and usually suggested using somewhere around 30pixels/meter. As I've been researching Farseer, I've seen a lot of conflicting statements. Some say to use 1pixel/meter, others say to use a scale, others say to use ConvertUnits.ToSimUnits and ConvertUnits.ToDisplayUnits, etc.

So what should I use? Right now, I'm using ConvertUnits, but everything renders in weird places. Is there a de-facto standard or anything that I should go by? Thanks in advance.


回答1:


Farseer 3.x is based on Box2D. And the Box2D FAQ says:

Box2D is tuned for meters-kilograms-seconds (MKS). Your moving objects should be between 0.1 - 10 meters. Do not use pixels as units! You will get a jittery simulation.

In other words, assuming a "normal" physics world, you should probably have 1 physics unit = 1m. But any scale that causes the majority of your moving objects to be in the 0.1 to 10 unit range should be ok.

So, say you're making a model of a car. A really, really simple model that happens to be a rectangle. You'd create it using something like:

float width = 4.1f; // average car length in meters
float height = 1.4f; // average car height in meters
// Note: this method takes half-sizes:
var carVertices = PolygonTools.CreateRectangle(width / 2f, height / 2f);
// Then pass carVertices into PolygonShape, etc...

The separate problem is how to then render your world at the correct size.

The way this is generally done is at render time using a camera or view matrix (in the standard world/view/projection system). The two places you could do this are BasicEffect.View (MSDN), or the transformation matrix parameter to SpriteBatch.Begin (MSDN, see also).

IMO, the Farseer ConvertUnits class is a very ugly way of handling the conversion.




回答2:


I use int MetersPixel = 64; for all my base calcs. So i have little enemys that their textures are 16px wide. So when creating their bodys with Farseer the width is 0.25.

16 / 64 = 0.25

Most importantly the feel, size / scale and weight is right for my current game, i dont believe there is any golden rule or reason why you should have to jump through hoops just to make your code 10 = 1 etc.

Its working perfectly for me so far. I never thought to try convert 10px to 1m but with my current game build i would not see that as useful.



来源:https://stackoverflow.com/questions/7733088/farseer-meter-pixel-ratio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!