Size of image being same as size of physics body

In my game I’m spawning randomly big say birds, and as I spawn these birds in different sizes I want to assign each one of them a unique physics body being the same size. So this is my code at the moment:

local function choose1(event)
local choose = math.random(1, 2)

if choose == 1 then
local enemy = display.newImage(“Images/Enemy.png”, math.random(15, 300), -200)
local enemySize = math.random(10, 30)
enemy.yScale= enemySize
enemy.xScale = enemySize
physics.addBody(enemy, { friction=0.5, bounce=0, radius=enemySize})

end

end
timer.performWithDelay(300, choose1, 0)

I tried using scale to achieve my purpose but it won’t work since Scale and Radius are using different measurements. Is there any way I can decide and image’s size by giving it a value with pixels? I tried enemy.ySize / enemy.xSize but it doesn’t work for some reason

Thanks in advance! [import]uid: 14018 topic_id: 5114 reply_id: 305114[/import]

Nobody has a solution to this? [import]uid: 14018 topic_id: 5114 reply_id: 17071[/import]

Bumping one last time. I can’t find a solution to this anywhere [import]uid: 14018 topic_id: 5114 reply_id: 17241[/import]

x scale to 150% is xScale=1.5, not xScale=150.
[import]uid: 6645 topic_id: 5114 reply_id: 17293[/import]

Assuming your image doesn’t have a border of transparent pixels:

  1. Create your display object

  2. Get the width of the object. Assuming a square png file, the radius will be half the width or height. So, this is the value you would use in your physics body constructor if you were NOT scaling the image

  3. Select your random scale multiplier - as mentioned, xScale/yScale of 1 is 100%, so take your random number, divide it by 100, and add 1. That’ll get you a range of 1.1 to 1.3.

  4. Use this value to scale up your image.

  5. Take the radius you determined in step two, above, and multiply it by your random multiplier. Use that in your physics body constructor.

Not tested, but should work.

Darren [import]uid: 1294 topic_id: 5114 reply_id: 17323[/import]

Thanks alot! Will test this as soon as I get home! [import]uid: 14018 topic_id: 5114 reply_id: 17328[/import]

Yes it worked! Thanks alot [import]uid: 14018 topic_id: 5114 reply_id: 17357[/import]