Physics body not scaling properly for all devices

In the game I am developing I have there being balls generated in different sizes. I start out with a large ball and then I scale it down to smaller balls. On the default simulator I was using, I found that for every 10 percent the size of the balls decreases, I can lower its radius by 15 and the physical body would perfectly fit the object. This worked on one simulator I was using but when I switched to other simulators the radius did not always fit the object.

How do I keep the radius of the ball to always fit the ball on any device? Thank you

(Note: I am using PNG’s for the ball not display.newCircle)

It would probably be helpful to see your code.  If you are add the physics object and then scaling, that could be the problem.  Let me see the code and then we might be able to figure this out.

[lua]

local function spawnBall()

local size = mRandom(17,31)/100

local ball = display.newImage(“game_ballred.png”)

if deathScreen == not nil then

deathScreen:toFront()

end

ball.type = “red”

–ball:setReferencePoint( display.BottomCenterReferencePoint )

ball:scale(size, size)

local minus = (1-size) * 247

local ball_box = 247 - minus

local ballX = mRandom(40, 100) * -1

local ballY = mRandom(10,50) -10

ball.x = ballX

ball.y = ballY-ball_box+40

local ball_bounce = mRandom(92,98)/100

physics.addBody(ball, { radius = ball_box, bounce = ball_bounce, filter = collisionFilter } )

xForce = mRandom(20,30)/100

ball:applyLinearImpulse( xForce, .3, ball.x, ball.y )

ball:toBack()

bg:toBack()

group:insert(ball)

local randomYellow = mRandom(1,4)

if randomYellow == 1 then

spawnBallYellow()

end

local randomBlue = mRandom(1,15)

if randomBlue == 1 then

spawnBallBlue()

end

local randomGreen = mRandom(1,30)

if randomGreen == 1 then

spawnBallGreen()

end

spawnTimers[#spawnTimers+1] = timer.performWithDelay(1150, spawnBall)

–250 10% 25

end

[/lua]

messy but here you go

Hi @ytduglepvp,

If you want the body to basically match the size of the ball after it has been scaled, you could probably get good results by just detecting the “contentWidth” or “contentHeight” of the ball (after it has been scaled), dividing it by 2, and applying that as the radius.

For example:

[lua]

local ball = display.newImage( “game_ballred.png” )

ball:scale( 1.2, 1.2 )

physics.addBody( ball, { radius = ball.contentWidth/2 } )

[/lua]

Of course, this assumes that visually, your ball image spans to the very edge of its square image canvas (meaning, you don’t have any significant amount of transparent pixels padding around it).

Hope this helps,

Brent

Thank you this helped

It would probably be helpful to see your code.  If you are add the physics object and then scaling, that could be the problem.  Let me see the code and then we might be able to figure this out.

[lua]

local function spawnBall()

local size = mRandom(17,31)/100

local ball = display.newImage(“game_ballred.png”)

if deathScreen == not nil then

deathScreen:toFront()

end

ball.type = “red”

–ball:setReferencePoint( display.BottomCenterReferencePoint )

ball:scale(size, size)

local minus = (1-size) * 247

local ball_box = 247 - minus

local ballX = mRandom(40, 100) * -1

local ballY = mRandom(10,50) -10

ball.x = ballX

ball.y = ballY-ball_box+40

local ball_bounce = mRandom(92,98)/100

physics.addBody(ball, { radius = ball_box, bounce = ball_bounce, filter = collisionFilter } )

xForce = mRandom(20,30)/100

ball:applyLinearImpulse( xForce, .3, ball.x, ball.y )

ball:toBack()

bg:toBack()

group:insert(ball)

local randomYellow = mRandom(1,4)

if randomYellow == 1 then

spawnBallYellow()

end

local randomBlue = mRandom(1,15)

if randomBlue == 1 then

spawnBallBlue()

end

local randomGreen = mRandom(1,30)

if randomGreen == 1 then

spawnBallGreen()

end

spawnTimers[#spawnTimers+1] = timer.performWithDelay(1150, spawnBall)

–250 10% 25

end

[/lua]

messy but here you go

Hi @ytduglepvp,

If you want the body to basically match the size of the ball after it has been scaled, you could probably get good results by just detecting the “contentWidth” or “contentHeight” of the ball (after it has been scaled), dividing it by 2, and applying that as the radius.

For example:

[lua]

local ball = display.newImage( “game_ballred.png” )

ball:scale( 1.2, 1.2 )

physics.addBody( ball, { radius = ball.contentWidth/2 } )

[/lua]

Of course, this assumes that visually, your ball image spans to the very edge of its square image canvas (meaning, you don’t have any significant amount of transparent pixels padding around it).

Hope this helps,

Brent

Thank you this helped