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.
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.
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;
{
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.