Hi guys,
I've released two games using Box2D and so have a good idea how it works. However, one thing I've never fully understood are the units.
This is from Box2D's manual:
http://www.box2d.org/manual.htmlBox2D is tuned for MKS units. Keep the size of moving objects roughly between 0.1 and 10 meters. You'll need to use some scaling system when you render your environment and actors. The Box2D testbed does this by using an OpenGL viewport transform. DO NOT USE PIXELS. |
This reads to me as if the units used in Box2D are meters.
Why, then, does this code fail to create a triangle with 1 meter sides?require "box2d"
local sprite = Sprite.new()
-- Sets up a world with -10 gravity and allows sleeping bodies
world = b2.World.new( 0, -10, true )
-- Creates a 1x1 meter triangle
local body = world:createBody{type = b2.STATIC_BODY}
local polygonShape = b2.PolygonShape.new()
polygonShape:set(0,0, 1,0, 1,1)
local fixture = body:createFixture{shape = polygonShape}
-- Adds and shows debug sprite
local sprite_debug_draw = b2.DebugDraw.new()
world:setDebugDraw( sprite_debug_draw )
sprite:addChild(sprite_debug_draw)
stage:addChild( sprite ) |
Instead of a triangle, this is the result:
Now, if I change the pixel scale Gideros use to 10 (from the default 30) it does produce a triangle:
require "box2d"
b2.setScale(10)
local sprite = Sprite.new()
sprite:setScale(100)
-- Sets up a world with -10 gravity and allows sleeping bodies
world = b2.World.new( 0, -10, true )
-- Creates a 1x1 meter triangle
local body = world:createBody{type = b2.STATIC_BODY}
local polygonShape = b2.PolygonShape.new()
polygonShape:set(0,0, 1,0, 1,1)
local fixture = body:createFixture{shape = polygonShape}
-- Adds and shows debug sprite
local sprite_debug_draw = b2.DebugDraw.new()
world:setDebugDraw( sprite_debug_draw )
sprite:addChild(sprite_debug_draw)
stage:addChild( sprite ) |
Like so:
My guess is that the units Box2D use are not meters after all, and when I set the triangle too small, it can't be rendered. If that's true, how big is a Box2D unit in meters? Are the sides of the triangle actually 0.01 meters? 0.1 meters?
Please help shed some light on this, before I stop trying to understand how it works and just make everything very big.
Niclas
Comments
So basically Gideros has "got your back" on this! The advice "DO NOT USE PIXELS" is already being followed internally by Gideros. Just use pixels throughout.
However, if you have a very high res game then you may find 30 is not enough. Eg 3000x3000 pixel body would correspond to 100 m x 100 m in the b2d world which might get a bit hairy. So in that case maybe b2.setScale(100).
https://github.com/gideros/gideros
https://www.youtube.com/c/JohnBlackburn1975
For example here is how I create a square body in Nebula:
https://github.com/gideros/gideros
https://www.youtube.com/c/JohnBlackburn1975
and only the dimensions and positions are scaled internally. Other units such as velocity, gravity or speed are kept as same.
also more info here
http://giderosmobile.com/forum/discussion/1311/box2d-scaling-factor/p1