Pixel Units as Default?

Hi,
I know there's a comment in b2settings.h

// Use this for pixels:
const float32 b2_lengthUnitsPerMeter = 50.0f;

I'm wondering though why it's not 1000.0f since millimeters seem to be
closer in size to a pixel.
And if I change that to 1000.0f do I
change all the constants that have 1.0f to 1000.0f in the .h file?

const float32 b2_massUnitsPerKilogram = 1.0f;
const float32 b2_timeUnitsPerSecond = 1.0f;

const float32 b2_velocityThreshold = 1.0f * b2_lengthUnitsPerMeter / b2_timeUnitsPerSecond; // 1 m/s

And if I do change to millimeters should the engine behave the same way
as if it were in meters. For example should a 1000mm x 1000mm square respond the same as
a 1m x 1m square?
Sorry for the mundane questions just can't seem to make it look right with pixels.

I wonder if it would make sense to setup version 2 to use pixel units by default?
Just thinking that in 2d unlike 3d I have a feeling most people will be working
with pixels rather than meters?

Mike
I don't really like pixel units. The problem is that it leads to some very large numbers. In general this can lead to numerical errors. For example, consider a box that is 50p x 50p. With a density of 1kg/p^2, the mass is 2500. If 50p is one meter, then gravity is roughly 10 * 50 = 500. Then the force of gravity on the box is 2500 * 500 = 1,250,000. This is not exactly a nice number to work with. Now when we get to torques, we'll have forces on the order of mass times gravity with lever arms on the order of 50p: torque = 2500 * 500 * 50 = 62,500,000. That's huge!

Now if we use 1000 pixels per meter, we'll get torques like: 10,000,000,000,000. Try groking that in a debugger!

I think you misunderstand the conversion factor. The conversion factor means this: if you were to display a 1m x 1m crate in your game, how many pixels would it cover? A better way to understand the conversion factor is that 1.8m is a typical height of a character in your game. How many pixels tall is the character?

It does seem that people really like pixels. Ideally all pixel coordinates would be scaled by 0.01 before being used by the physics engine, and the engine would scale all results by 100. However, this would be tedious an error prone. I'll sleep on this.
I see what you mean about the huge numbers!
Ah thanks for the clarification on the conversion.
Yeah for 2d work i've never seen a graphics library use
meters to display sprites and characters. It's always in pixels 🙂.
Mike
I've not done much 2D graphics (or graphics at all). So a game like Peggle runs windowed and full screen. Is the windowed version the native resolution and the full screen version stretched? Do 2D games often support multiple window sizes? How do they deal with this?
What happens usually is lets say your windowed game is 800x600
If you switch to full screen the graphics card does all the work and still
keeps the 800x600 resolution. Thats for games that don't scale.
For games that allow scaling usually the graphics library does all the
back end work of stretching or compressing so your sprites are always
at fixed dimensions.
I'm not big on the math but how about something like mapping all the
coordinates into a box for all the physics calculations
so the world AABB (minX,minY,maxX,maxY) => (0,0,1,1)
hideous diagram below
1 ------
|.......|
|.......| <== the internal BOX2d world
0 0----1
where everything then gets mapped back into the users space.
That would isolate your engine from any crazy user coordinate preferences like
mine 🙂..

Mike
IMO, things like world to screen transformations are better left to a library or a game engine than the physics engine - the physics engine should need to know nothing about how things are going to be displayed.

Really, all you need to do a full-featured 2d camera is a translation vector and a scale factor (maybe separate x/y scale factors, and rotation if you're so inclined - even the most adventurous can't do anything more nuts than an arbitrary quadrilateral mapping, which is still very simple to calculate), which you use with a few transformation functions to map back and forth from world to screen space (body space, too, if you're being complete). It's probably half an hour's work to get a simple version of this working. Maybe if someone just contributed a file that handled this in a nice way, that could be included as a contributed library and ease this issue a bit?

re: units, to me meters just make a bit more sense, because it's easier to look at a real world object and figure out how to size it in terms of meters than to remember roughly what the pixel units correspond to...it's no big deal either way to me, though, as long as the engine is consistently tuned. But to my knowledge, most engines use meters, so it might be a bit unusual if Box2D standardized on pixels.
That would be great to have.
I wonder if transforming every coordinate would hurt performance much?
I guess my thinking is that 2d is usually a fantasy world where you don't
care much about real world measurements like meters and kilograms..etc.
I usually think of real world measurements like that in 3d worlds.
Mike
There is another thing to consider. If the physics engine uses pixels then it will be quite difficult for people to use empirical properties, such as:
- gravity
- material densities (water, stone, wood, metal, etc).
- wind speed
- engine torque curves
- tire properties

So each project would need to create it's own conversion function for these things. Or just wing it.

I can make two sets of tolerances: one for meters and one for pixels. I guess the big issue for me is the units that I would use in my examples. I only want to work with one set of units.
I would suggest to take ewjordans suggestion and not worry
about the units and leave that to the application.
I was able to get decent results playing with the b2settings.h and
using meters so meters is fine for me as well.
I'm just imagining though that when more and more people start
using it in 2d games most of them will want to use pixels as their
fundamental unit and might have a hard time making it look good.
Mike
mike950 wrote:
I'm just imagining though that when more and more people start
using it in 2d games most of them will want to use pixels as their
fundamental unit and might have a hard time making it look good.
Mike


I did too... but then it took me about 20 minutes to get used to using meters, and now I actually like using 'em. Shouldn't be hard for any decent programmer to 'convert'.

Original thread: https://box2d.org/forum/viewtopic.php?t=171