Need help with touch joint on objects in display groups.

This is an example. The crate is in an object_group which is then inserted into the main_display group. The touch joint is out of sync with the screen touch when the main_display is moved to keep up with the crate. I would appreciate if someone could provide some help with the math for this example.

[lua]–> Setup Display
display.setStatusBar (display.HiddenStatusBar)

–> Start Physics
local physics = require (“physics”)
physics.start ()
physics.setGravity (0, 10)
physics.setDrawMode (“hybrid”)
–physics.setDrawMode (“hybrid”)

local main_display = display.newGroup()
local object_group = display.newGroup()

main_display.x = main_display.x +40
main_display.y = main_display.y - 80

–> Create Walls
local leftWall = display.newRect (-100, 0, 1, display.contentHeight)
main_display:insert(leftWall)
local rightWall = display.newRect (display.contentWidth + 100, 0, 1, display.contentHeight)
main_display:insert(rightWall)
local ceiling = display.newRect (-100, 0, display.contentWidth + 200, 1)
main_display:insert(ceiling)
local floor = display.newRect (-100, display.contentHeight, display.contentWidth + 200, 1)
main_display:insert(floor)

physics.addBody (leftWall, “static”, {bounce = 0.0, friction = 10})
physics.addBody (rightWall, “static”, {bounce = 0.0, friction = 10})
physics.addBody (ceiling, “static”, {bounce = 0.0, friction = 10})
physics.addBody (floor, “static”, {bounce = 0.0, friction = 10})

local crate = display.newRect( 0, 0, 32, 32 )
physics.addBody( crate, “dynamic”, { density=0, friction=0.5,bounce=0.2 } )
crate.x = 100
crate.y = 280
object_group:insert(crate)

main_display:insert(object_group)

local function createDragBody( params )
local function dragBody( event )
local body = event.target
local phase = event.phase
local stage = display.getCurrentStage()

if “began” == phase then
stage:setFocus( body, event.id )
body.isFocus = true
– Create a temporary touch joint and store it in the object for later reference
if params and params.center then
– drag the body from its center point
body.tempJoint = physics.newJoint( “touch”, body, body.x, body.y)
else
– drag the body from the point where it was touched
body.tempJoint = physics.newJoint( “touch”, body, event.x, event.y)
end

– Apply optional joint parameters
if params then
local maxForce, frequency, dampingRatio

if params.maxForce then
– Internal default is (1000 * mass), so set this fairly high if setting manually
body.tempJoint.maxForce = params.maxForce
end

if params.frequency then
– This is the response speed of the elastic joint: higher numbers = less lag/bounce
body.tempJoint.frequency = params.frequency
end

if params.dampingRatio then
print( “ratio”, params.dampingRatio )
– Possible values: 0 (no damping) to 1.0 (critical damping)
body.tempJoint.dampingRatio = params.dampingRatio
end
end
elseif body.isFocus then
if “moved” == phase then
– Update the joint to track the touch
local contentx, contenty = event.x, event.y
– Convert to local coordinates of
local localx, localy = event.target:localToContent(contentx, contenty)
body.tempJoint:setTarget( event.x, event.y)
elseif “ended” == phase or “cancelled” == phase then
stage:setFocus( body, nil )
body.isFocus = false

– Remove the joint when the touch ends
body.tempJoint:removeSelf()
end
end

– Stop further propagation of touch event
return true
end

return dragBody
end
local function moveCamera()
main_display.x = -crate.x +200
main_display.y = -crate.y + 260
end

Runtime:addEventListener( “enterFrame”, moveCamera )

local dragBody = createDragBody( { dampingRatio = 1.0 })
crate:addEventListener ( “touch”, dragBody )
[import]uid: 7531 topic_id: 7289 reply_id: 307289[/import]