Why shouldn't I use pixels as unit with Box2D?

心不动则不痛 提交于 2020-02-04 04:52:26

问题


In the manual it says that I should use small units (0.1-10 meters). It's discouraged to use pixels as the unit of measure. But why would Box2D be working better, and have better simulation than when I'd use small units?


回答1:


Box2D is a simulation framework which internally uses the MKS system of units. If you want a reliable and predictable simulation, you should express the simulation systems you create in reasonable values within this system of units. You want a box to behave like a box, a rock to behave like a rock and a ball to behave like a ball.

If you take pixels as a unit and you have a box of 20 by 20 pixels with a scalar mass value of 10, you really create a box with sides of 20 meters long, which has a mass of a mere 10 kilograms. In essence, your simulation becomes difficult to set up and be made to behave realistically.

There is nothing stopping you from using pixels as units. But for it to behave reliably, naturally and predictably, it is better and easier to express the values fed to the simulation framework in the units it expects and within reasonable boundaries.




回答2:


Probably because the number of pixels can change between devices/displays so it is not reliable. For instance, if you moved 10 pixels on an iPad1 you would move further than if you moved 10 pixels on an iPad3 due to the higher resolution (more pixels per inch) on the iPad3. Using meters provides the physics engine with a reliable unit of measurement that will be consistent across all devices/displays.




回答3:


Taken from here

Box2D physics objects are defined with meters for length and width. Box2D is tuned for a 1x1 meter sized objects. The reason you want PTM (Pixels to Meters) is that you do NOT want to have a 1 to 1 correlation between pixel size and meters size.

For example if you had a car that was 200 pixels long and 50 pixels tall, and you did not use a PTM conversion you would end up creating a Box2D car of 200 meters by 50 meters. That's 656 feet long or about the length of 12 semi trucks. Imagine the force you would have to apply to this car to move it.

What you want is to scale down your pixel sizes into meter dimensions for Box2D, most times I end up using a PTM of 30 or 32 pixels to 1 meter.

I believe the reason that Box2D is tuned best for 1 meter is that calculations on the number 1 are faster than operations on other numbers. e.g. root(1) = 1. (And roots are a common calculation in a physics engine). Thus you should tweak your PTM ratio so that your most common object is 1X1 meter in size.

The Box2D documentation Section 1.7   also provides some insight:

Box2D works with floating point numbers and tolerances have to be used to make Box2D perform well. These tolerances have been tuned to work well with meters-kilogram-second (MKS) units. In particular, Box2D has been tuned to work well with moving objects between 0.1 and 10 meters. So this means objects between soup cans and buses in size should work well. Static objects may be up to 50 meters big without too much trouble.



来源:https://stackoverflow.com/questions/10656588/why-shouldnt-i-use-pixels-as-unit-with-box2d

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