a physics and accelerometer problem

Hi,

I want to make a physics & accelerometer app.
my problem is when I lay the iphone on desk for a while, after the rect(my dynamic body) stop moving with accelerometer. then no matter how I rotate iphone, the rect doesn’t move. but the debug_text I made shows the xGravity has value and changing when I rotate the iphone.
here is my code, thanks for any help!


display.setStatusBar(display.HiddenStatusBar)
display.setDefault(“background”, 255, 255, 255)

local debug_text = display.newText(“debug”, 0, 0, native.systemFont, 26)
debug_text:setTextColor(0, 0, 0)
debug_text.x = display.contentWidth/2
debug_text.y = display.contentHeight/2

local physics = require “physics”
physics.start()
physics.setScale( 60 )

local top = display.newRect(0,0, display.contentWidth, 5)
local bottom = display.newRect(0, display.contentHeight - 5, display.contentWidth, 5)
local left = display.newRect(0,0, 5, display.contentHeight)
local right = display.newRect(display.contentWidth-5,0, 5, display.contentHeight)

top:setFillColor(200, 200 ,200)
bottom:setFillColor(200, 200 ,200)
left:setFillColor(200, 200 ,200)
right:setFillColor(200, 200 ,200)

local wallMaterial = {bounce = 0.3, friction=0.5}

physics.addBody(top, “static”, wallMaterial)
physics.addBody(bottom, “static”, wallMaterial)
physics.addBody(left, “static”, wallMaterial)
physics.addBody(right, “static”, wallMaterial)
local rect = display.newRect(20, 20, 50, 50)
rect:setFillColor(100, 100 ,100)

physics.addBody(rect, {density = 1.0})

local function onTilt( event )
physics.setGravity( 50 * event.xGravity, -50 * event.yGravity )
debug_text.text = event.xGravity
end

Runtime:addEventListener( “accelerometer”, onTilt )
----------------------------------------------------------------- [import]uid: 99926 topic_id: 22430 reply_id: 322430[/import]

Can you try setting;

rect.isSleepingAllowed = false

Let me know if that fixes it :slight_smile: [import]uid: 52491 topic_id: 22430 reply_id: 89553[/import]

it works, thanks!

seems I should read the API carefully .
:slight_smile: [import]uid: 99926 topic_id: 22430 reply_id: 89557[/import]

Fantastic!

Don’t worry, it’s hard to read and absorb every bit of into at once, especially with physics :wink: [import]uid: 52491 topic_id: 22430 reply_id: 89589[/import]