What am I doing wrong? Simple physics..?

I’m officially stumped. After completing a project, I noticed an issue with my physics that I’m not sure how to fix. I’ve striped down the code to the very basics which shows the problem. Originally, I started with Corona’s sample code ‘ShapeTumbler’ which does not seem to have the problem.

The Problem: When running on the device (not simulator), the ball gets ‘stuck’ when it is tilted into a corner or touching multiple surfaces (from what I’ve seen). As a test, I added drag code (not included here) to move the ball when it gets stuck, and I am able to get it moving again but it only gets stuck again. Rolling the ball around the screen works, but when it comes to rest in a corner and I try to tilt again, the ball is stuck or the physics gets out of sync and simply does not respond. Any clues? I’m guessing its something so simple, but I feel I’ve just stared at it too long. Thanks in advance.

[code]

local physics = require(“physics”)
physics.start()
physics.setScale( 60 )
physics.setGravity( 0, 9.8 )

system.setAccelerometerInterval( 100 )

function onTilt( event )
physics.setGravity( ( 9.8 * event.xGravity ), ( -9.8 * event.yGravity ) )
end

Runtime:addEventListener( “accelerometer”, onTilt )
display.setStatusBar( display.HiddenStatusBar )

local ball = display.newImage(“soccer_ball.png”)
ball.x = 40; ball.y = 80

borderBodyElement = { friction=0.5, bounce=0.3 }

local borderTop = display.newRect( 0, 0, 320, 20 )
physics.addBody( borderTop, “static”, borderBodyElement )

local borderBottom = display.newRect( 0, 460, 320, 20 )
physics.addBody( borderBottom, “static”, borderBodyElement )

local borderLeft = display.newRect( 0, 20, 20, 460 )
physics.addBody( borderLeft, “static”, borderBodyElement )

local borderRight = display.newRect( 300, 20, 20, 460 )
physics.addBody( borderRight, “static”, borderBodyElement )

physics.addBody( ball, { density=0.9, friction=0.9, bounce=0.3, radius=38 } )

[/code] [import]uid: 10593 topic_id: 3389 reply_id: 303389[/import]

Most likely you need to disable “sleeping” on the ball.

http://developer.anscamobile.com/content/game-edition-physics-bodies#body.isSleepingAllowed

In Box2D objects go to sleep after a period of inactivity (in the physics sense), and sleeping bodies don’t respond to tilt/gravity changes. You can also disable sleeping globally when you start the physics engine:

http://developer.anscamobile.com/content/game-edition-box2d-physics-engine#physics.start_noSleep_

Tim [import]uid: 8196 topic_id: 3389 reply_id: 10114[/import]

Thank you! That was the problem. I ended up using the global sleep variable on physics as the local version produced the same result…

--works like a charm, globally  
physics.start(true)  
--Unable to get this working  
physics.start() --default is false (bodies may sleep) according to documentation  
ball.isSleepingAllowed = false   

For future reference on local sleep, is the 2nd section of pseudo code valid? I still don’t see how it was done in the Shape Tumbler demo app, unless all of the objects are constantly monitoring collision detection somehow… Dunno, the code says otherwise but I’m still learning.

Anywho, thanks again Tim! This helps a ton.

[import]uid: 10593 topic_id: 3389 reply_id: 10140[/import]