Talking about Actionscript 3, Box2D, Flash and Game development.
Ok, time to learn once for all how does Box2D manages object sizes, and how to set them right when working with AS3
In previous step I used a 90×90 pixel crate.
The problem is Box2D does not works with pixels because it does not have a native pixel render engine. In other words, Box2D does not display anything on the screen, it just calculates position and rotation of all bodies. There is a built-in debug renderer but, as the name suggests, it should be used only for debugging purposes.
That’s why in the Update
function (lines 63-72) I have to “manually” place every crate in the right place according to its position and rotation (lines 67-69).
Box2D works in meters, and there is an unwritten rule saying 1 meter = 30 pixels. That’s why at line 16 I have the pixels_in_a_meter
variable.
So when I think about the crate, I don’t have to think about it as a 90px crate but a 90/30 = 3 meters crate.
But if you look at lines 47 – 48 you’ll see I am assigning 1.5 (meters) to crate_width
and crate_height
variables. Why?
Because Box2D to create a box (but you will find the same concept applied to other shapes) uses SetAsBox
(line 50) that wants width and height relative to the registration point placed in its center.
So that’s how you should think about pixels when you work with BOX2D:
I hope this will help you saving some time…
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.