I did call setReferencePoint and a first test run seemed okay.
But, because I need these shapes to be added to physics, and because I want to pause the simulation once in a while and, at that time, move all bodies back to their original positions, I changed my code to this and the problem (wonky physics) became apparent:
local physics = require "physics"
physics.setGravity(0, 9.8)
physics.setDrawMode( "hybrid" )
local function makeBody(x, y, w, h)
local r = display.newRect(x, y, w, h)
r:setReferencePoint( display.TopLeftReferencePoint )
print(r.x, r.xOrigin, r.y, r.yOrigin, r.width, r.height)
r:setFillColor(255, 0, 0)
r.orgX = r.x
r.orgY = r.y
r.orgRot = 0
return r
end
physics.addBody(makeBody(0, 0, 100, 100), { density = 1.0, friction = 0.7, bounce = 0.2})
physics.addBody(makeBody(90, 101, 100, 100), { density = 1.0, friction = 0.7, bounce = 0.2})
physics.start() -- this runs okay the first time, or so it seems
-- but, when user presses pause...
local function resetBody(aBody)
aBody:setReferencePoint(display.TopLeftReferencePoint)
aBody.x = aBody.orgX
aBody.y = aBody.orgY
aBody.rotation = aBody.orgRot
aBody:setLinearVelocity (0,0)
aBody.angularVelocity = 0
end
local function pause()
physics.pause()
for i=1,display.getCurrentStage().numChildren do
resetBody(display.getCurrentStage[i])
end
end
-- then presses play again...
local function play()
physics.start()
end
-- ===========================
-- physics starts misbehaving!
-- ===========================
And now, I am not even able to run the code above. simulator crashes from a bus error as soon as it has loaded the program.
Alex [import]uid: 3473 topic_id: 2434 reply_id: 7230[/import]