Maybe this will help: http://www.box2d.org/manual.html#units
m_physScale in TestBed-Example is set to 1 | Meter-Units ?!
General DiscussionI think that is only in the AS3 version. Matt (skatehead) was trying to do the scaling for you. I would just leave that factor at 1 and do your own physics scaling.
I have to admit i am a little bit confused too. (obviously this is completely wrong, but) As far as i understand, it is ideal to have the size of bodies relatively low. But the thing i cannot get my head around. How the heck do you apply detailed textures to a box with 2x2meter (ends up like 2x2pixels on the screen)? 😕 And whats up with all the talk of 'physics scaling factor' 😳 I am using OpenGL for my drawings (using glOrtho).
Separate the rendering scale from the physics scale. Create a box that is, say, 2x2 meters. You're well within the tolerance limits that ensure a well-behaved simulation. Ok, how big do you want this on the screen? What is the visual representation you want? You might say, "I want this box to be roughly 1/2 the width of my screen." If you're rendering to an 800x600 screen, then you want this box to be about 400x400. Scale accordingly. Maybe you want it smaller, so you render it accordingly.
Forget all this pixel unit stuff 😉 Keep things separate and figure out how you will relate the render scale to the actual physics scale.
Bill
Forget all this pixel unit stuff 😉 Keep things separate and figure out how you will relate the render scale to the actual physics scale.
Bill
Yeah, I'm hoping someone will put together a demo that shows how to simulate pixel based sprites along using meter based physics. This question comes up so often that I'm sure this would help dozens of people.
In principle it is very simple, just use a constant, global scaling factor.
In principle it is very simple, just use a constant, global scaling factor.
Hmm. Is what you are saying, that i should draw all my objects with the extents: the extents specified in the body definitions multiplied by a global constant (the physics factor), it should be O.K?
Here's what I'm saying.
Suppose you have a sprite for a character that is 100x100 pixels. You decide to use a scaling factor that is 0.01. This will make the character physics box 1m x 1m. So go make a physics box that is 1x1. Now suppose the character starts out at pixel coordinate (345,679). So position the physics box at (3.45,6.79). Now simulate the physics world. Suppose the character physics box moves to (2.31,4.98), so move your character sprite to pixel coordinates (231,498).
Now the only tricky part is choosing a scaling factor. This really depends on your game. You should try to get your moving objects in the range 0.1m-10m, with 1m being the sweet spot.
Suppose you have a sprite for a character that is 100x100 pixels. You decide to use a scaling factor that is 0.01. This will make the character physics box 1m x 1m. So go make a physics box that is 1x1. Now suppose the character starts out at pixel coordinate (345,679). So position the physics box at (3.45,6.79). Now simulate the physics world. Suppose the character physics box moves to (2.31,4.98), so move your character sprite to pixel coordinates (231,498).
Now the only tricky part is choosing a scaling factor. This really depends on your game. You should try to get your moving objects in the range 0.1m-10m, with 1m being the sweet spot.
This confuses me, too.
Where do you set the scale?
If you want 16 pixels to equal 1 meter, how do you set it up?
Something like this?:
If you want to put a box sprite in your world:
When updating display:
public function onEnterFrame(e:Event) {
var p:b2Vec2 = b2Math.SubtractVV(body.m_position, b2Math.b2MulMV(body.m_R, body.m_center));
x = p.x * global.getScaleOut();
y = p.y * global.getScaleOut();
rotation = (body.m_rotation * 180) / Math.PI;
}
Is that right? Is there something I'm missing or am I way off track?
Where do you set the scale?
If you want 16 pixels to equal 1 meter, how do you set it up?
Something like this?:
Code:
private const var m_PHScaleOut:Number = 16;
private const var m_PHScaleIn:Number = 1/16;
private const var m_PHScaleIn:Number = 1/16;
If you want to put a box sprite in your world:
Code:
body_def.position.Set(x, y).Mutiply(global.getScaleIn());
var s:b2BoxDef = new b2BoxDef();
s.extents = new b2Vec2(size_x / 2 * scaleX, size_y / 2 * scaleY).Multiply(global.getScaleIn()));
s.localPosition = new b2Vec2(x_pos * scaleX, y_pos * scaleY).Multiply(global.getScaleIn()));
var s:b2BoxDef = new b2BoxDef();
s.extents = new b2Vec2(size_x / 2 * scaleX, size_y / 2 * scaleY).Multiply(global.getScaleIn()));
s.localPosition = new b2Vec2(x_pos * scaleX, y_pos * scaleY).Multiply(global.getScaleIn()));
When updating display:
Code:
public function onEnterFrame(e:Event) {
var p:b2Vec2 = b2Math.SubtractVV(body.m_position, b2Math.b2MulMV(body.m_R, body.m_center));
x = p.x * global.getScaleOut();
y = p.y * global.getScaleOut();
rotation = (body.m_rotation * 180) / Math.PI;
}
Is that right? Is there something I'm missing or am I way off track?
A. Russel: not an AS3 programmer here, but that looks correct.
Yes, it does work. You need to change all your forces too, otherwise you might find, like me, that your bodies will explode off the screen.
You'll find that once you have set the scale like this, you can use more sane figures in your forces. For example, gravity of 9.1 looks pretty realistic now (before I had it set at about 200!) and no more crazy forces needed to move things (like 200,000N to push a small ball on flat surface).
You'll find that once you have set the scale like this, you can use more sane figures in your forces. For example, gravity of 9.1 looks pretty realistic now (before I had it set at about 200!) and no more crazy forces needed to move things (like 200,000N to push a small ball on flat surface).
Original thread: https://box2d.org/forum/viewtopic.php?t=537