I'm starting from scratch and trying to figure out how to do some simple things like get the falling crates demo to drop crates of different dimensions. My 1st question (and many more will follow) is how do I change the size of a Bitmap or Sprite to match the size of a b2body? I can create different size crates but the costume for these b2bodies doesn't match what it's behind. Help!
Comments
The Physics/SleepingBodies example might be doing exactly what you describe?
The "onTimer" function randomly chooses the x/y scales (sx,sy) then scales the x/y dimensions of both the box2d body [see the line with: shape:setAsBox(34.5 * sx, 34.5 * sy)] and the associated sprite [see the line with: sprite:setScale(sx, sy)].
Likes: Cliff76
Box2d does work in MKS. That example is just making very large bodies. To make smaller objects in the example:
* scale down the size and positions of the objects in the box2d world (e.g., by 1/10)
* when you use body:getPosition() to get the position of a body, scale the position back up (e.g., by 10) to position the sprite associated with the body
There is a funciton b2.setScale(scale) using which you can scale all pixel units, by default it's value is 30, so all your objects are already scaled. As I understood, then it's done internally and nothing additional is required from you, then simply provide scale.
@ndoss Correct me if I'm wrong, but I think you don't need to scale input nor output by yourself, it is done internally.
By the way, how do you determine it's not working for you?
You should use it once, just right after calling require "box2d" (Well actually before using anything else from box2d, but you get the point)
For example if your largest object is 300px and your smallest object is 3px, then by setting scale to 30, box2d would handle them as they were 10 units and 0.1. It won't change the actual size. But simulations may behave differently. Try setting scale 0 and look and dynamic object moving and collisions. They should be pretty jigged.
(as a side note, only the dimensions and positions are scaled internally. Other units such as velocity, gravity or speed are kept as same.)
For example you set position
body:setPosition(120, 200)
inside set position function 120 and 200 are divided by 20 (120/20 = 6, 200/20 = 10) and values 6 and 10 are used.
And if you use get position function
local x,y = body:getPosition() then inside getPosition function values 6 and 10 are taken and multiplied by scale (6*20 = 120, 10*20 = 200) and returned back to you.
As you see, you don't even notice the scaling, it's all done internally, you operate with same pixels sized you have for your objects. Think of it as automatic optimization.
Although not very logical, you can change the scale between setPosition and getPosition to get the internal values:
http://appcodingeasy.com/Gideros-Mobile/Gideros-Box2D-basics
Likes: Cliff76
Lua can return multiple values and self.body:getPosition() return two values: x and y. You can write:
Likes: ___