Need a Little Help Understanding Scaling

Hey all. I am writing an engine that wraps Box2D (among numerous other things), and I am a bit confused as to how the scaling is supposed to work. Since B2D is tuned to units between 0.1 and 10, that means I have to adapt my rendering code to work with the engine. I don't want that. In my opinion, a good middleware package should adapt to the needs of the user, so how would I get B2D to adapt to me?

One thing I am doing in my engine is providing a Stage class that holds a b2World and at runtime, entities are added and removed as needed. Its header is defined like so.

Code:
class Stage : public object // object provides me some utility functions/fields to use with my resource management implementation. probably should be inherited privately.
{
    public:
        Stage();
        Stage(float width, float height);
        virtual ~Stage();

        void UpdateStage(); // updates held entities
        void AddEntity(Entity ent); // adds an entity into the manager.

        void SetGravity(float x, float y);
        void SetSleep(bool val = true);

    protected:
        // Helps determine the size of the b2World that the stage will hold.
        float stage_width;
        float stage_height;
        // trouble is that pixels aren't meters

        b2World* world;

        pEntityManager entity_manager;
};
typedef boost::shared_ptr<Stage> pStage;


It is similar to the "room" concept in Game Maker. A game could implement hundreds of these stages, but the Application only works on one stage at a time. The rendering engine (I posted about it a while ago, I'll dig up the thread later) acts independently and provides a camera implementation with window and viewport dimension fields (which will probably be moved to the Window class in the engine rather than stay in the renderer) as well as providing adjustment facilities to the viewing volume. What I need to do is pass the meter values returned by the physics updates to the renderer as pixel units, but I am puzzled as to what the meter->pixel ratio is or how I would calculate it. I believe that it would be dependent upon the size of the viewport, but I am not entirely certain. Also, if I have a 800x600 viewport (drawing area, correct me if I'm wrong), wouldn't the resulting calculations (scaling the box2d coordinates to send to the renderer) be off?

My concern with using an OpenGL viewport transform like the testbed does is that it will result in me completely re-writing my rendering code. Wouldn't a viewport transform skew/distort/otherwise mess up the entire rendered image? Am I even looking at this the right way and it's NOT coordinates, but rather object sizes that I need to be concerned about.

This almost makes me wish that Box2D allowed for greater values, but I know that it is tuned to this restriction. I guess this is a small price to pay for efficiency. Any and all help is appreciated.

EDIT:
I found this article on the wiki - http://www.box2d.org/wiki/index.php?title=FAQ#How_do_I_convert_pixels_to_meters_units and I am still a bit puzzled as to how this factors into rendering. I suppose that the scaling factor should also factor into the dimensions off the b2World. What I mean is if we assume a scaling factor of 0.01, an 800x600 rendering area would map to a 8.00x6.00 b2World, and any bodies added to the world subject to that factor would still map out one-to-one with the graphics once scaled back up.

Correct me if I am wrong on any of this theory. In the mean time, I'll put it in and see what happens.
Orm wrote:
Hey all. I am writing an engine that wraps Box2D (among numerous other things), and I am a bit confused as to how the scaling is supposed to work. Since B2D is tuned to units between 0.1 and 10, that means I have to adapt my rendering code to work with the engine. I don't want that. In my opinion, a good middleware package should adapt to the needs of the user, so how would I get B2D to adapt to me?

Unless you're doing something unusual with your rendering, it should be trivial to adjust to Box2D's coordinate and unit system.

Remember, just because the screen resolution is (for example) 800x600 doesn't mean that your simulation must be at that scale. By using OpenGL's various transforms (viewport, projection, modelview), you can set things up however you want. Typically you'll want the aspect ratio of the projection to match that of the window, but otherwise, you can set the projection up however you want. For example, you can make the screen 10 units wide in 'simulation space', regardless of screen resolution. As such, there should be no problem matching the graphics to Box2D's coordinate and unit system.

Again, that's assuming that you're not doing something unusual rendering-wise. If what I described above isn't clear or doesn't seem like it applies in your case, perhaps you could describe your rendering system a bit so that we have a better idea of what the problem is.
It's your typical rendering system. Nothing unusual afaik. I designed it to be pretty basic.
Quote:
It's your typical rendering system. Nothing unusual afaik. I designed it to be pretty basic.

In that case, it really shouldn't be a problem to match your rendering to Box2D's coordinate and unit system - just set up your various transforms appropriately, and you should be good to go.
For what it's worth, I just integrated Box2d with draw scaling into my current renderer, and had no issues.

In fact, it was silly easy. The only change I've had to make so far is multiplying my sprite position by the draw scale.

An interesting side effect of draw scale though... the physics are much faster with a larger scale value (10.0 versus 50.0 for example). Something to keep in mind, you do need to design your scale around the pixel size targets you are shooting for (while maintaining semi-realistic meter lengths, if you are wanting that), and it must be universal obviously. If you plan to change it later, you'll have to resize your sprites, or change the measurements of the bodies; either can be a tricky ordeal, depending on your proficiencies 🙂

Pick it and stick it! So far, finding that balance has been trickier for me 🙂 I've been playing between 10 and 50 though, I'll probably end up around 20 or 30.

At the end of the day, try to visualize your physics simulation as a mini map, and your game as that mini map blown up. All you really need to do to move between the physics state and the actual game state is multiple or divide your positions/sizes and other world-space related units, by your draw scale.
Yeah, right now I'm clamping the Stage size to 1000x1000 max which in turn clamps the max size of the OpenGL viewport and then when I call Actor::Move, I multiply the passed values by 0.01 to pass along to Box2D. I'm doing the same thing with setting the Sprite Bounding boxes. Then when I'm updating the Actor's sprite's position, I am simply doing the reverse (multiplying by 100).

Make sense?
Orm wrote:
Yeah, right now I'm clamping the Stage size to 1000x1000 max which in turn clamps the max size of the OpenGL viewport and then when I call Actor::Move, I multiply the passed values by 0.01 to pass along to Box2D. I'm doing the same thing with setting the Sprite Bounding boxes. Then when I'm updating the Actor's sprite's position, I am simply doing the reverse (multiplying by 100).

Make sense?

I'm still not quite clear on why you're having to jump through these hoops. Why not just set up your projection transform so that you can use the same scale (Box2D's scale) throughout? Then you won't have to do any scaling or correcting at all.

Is it because you're trying to do pixel-perfect graphics in some form or other?
Jesse wrote:
Orm wrote:
Yeah, right now I'm clamping the Stage size to 1000x1000 max which in turn clamps the max size of the OpenGL viewport and then when I call Actor::Move, I multiply the passed values by 0.01 to pass along to Box2D. I'm doing the same thing with setting the Sprite Bounding boxes. Then when I'm updating the Actor's sprite's position, I am simply doing the reverse (multiplying by 100).

Make sense?

I'm still not quite clear on why you're having to jump through these hoops. Why not just set up your projection transform so that you can use the same scale (Box2D's scale) throughout? Then you won't have to do any scaling or correcting at all.

Is it because you're trying to do pixel-perfect graphics in some form or other?


I find pixel perfect easier to work with. My rendering engine assumes that the coordinates being fed to it are the exact coordinates in the viewport. The client can then set the scale using a combination of configuring the projection and using the scale of their artwork.

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