(SOLVED) Touch Joint does not work after group's coordinates change.

I need some help understanding this. The code below works correctly at first. Touch (click) the ball and you can drag it around the white testGroup. But if you uncomment lines 58, 59 to shift the testGroup, it stops working correctly when you click and drag the ball.

I assume this has something to do with stage coordinates vs local coordinates. but I don’t really understand how to use them to solve this problem. Can anyone help?

[code]
– turn off status display bar
display.setStatusBar(display.HiddenStatusBar)

–Physics
local physics = require(“physics”);
physics.start()
physics.setGravity(0, 0)

local bg = display.newGroup() – Create a background group
local testGroup = display.newGroup() – Create a game group
bg:insert( testGroup )

local rect = display.newRect( 0, 0, 300, 300 ) – Create a rectangle object
testGroup:insert( rect ) – add rect to group

local ball = {}

function dragBall(e)
local body = e.target;
local phase = e.phase;
local stage = display.getCurrentStage();

if(phase == “began”) then

–Focus the touch to ONLY its target
stage:setFocus(body, e.id)

– Create a temporary touch joint and store it in the object for later reference
body.tempJoint = physics.newJoint(“touch”, body, e.x, e.y)

elseif(phase == “moved”) then

– Update the joint to track the touch
body.tempJoint:setTarget(e.x, e.y);

elseif(phase == “ended” or phase == “cancelled”) then
stage:setFocus(body, nil);

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

– Stop further propagation of touch event
return true
end
local doBall = function(xBase, yBase, radius)
ball = display.newCircle(xBase, yBase, radius )
ball:setFillColor(255,0,0)
testGroup:insert(ball)
physics.addBody(ball, “dynamic”, {density = 5.0, friction = 0, bounce = 0, radius = radius})
ball:addEventListener(“touch”, dragBall);
end

doBall(50,50,20)

–testGroup:setReferencePoint(display.CenterReferencePoint)
–testGroup.x , testGroup.y = display.contentCenterX, display.contentCenterY

[/code] [import]uid: 23636 topic_id: 26827 reply_id: 326827[/import]

I found a solution by reviewing several posts about content vs local coordinates. This new version works without the problems of the first. Note the new statement added at line 22, and the resulting modified statements at line 30 and line 35.

Bruce

[code]
– turn off status display bar
display.setStatusBar(display.HiddenStatusBar)

–Physics
local physics = require(“physics”);
physics.start()
physics.setGravity(0, 0)

local bg = display.newGroup() – Create a background group
local testGroup = display.newGroup() – Create a game group
bg:insert( testGroup )

local rect = display.newRect( 0, 0, 300, 300 ) – Create a rectangle object
testGroup:insert( rect ) – add rect to group

local ball = {}

function dragBall(e)
local body = e.target;
local phase = e.phase;
local stage = display.getCurrentStage();
local x_local, y_local = testGroup:contentToLocal(e.x, e.y)

if(phase == “began”) then

–Focus the touch to ONLY its target
stage:setFocus(body, e.id)

– Create a temporary touch joint and store it in the object for later reference
body.tempJoint = physics.newJoint(“touch”, body, x_local, y_local)

elseif(phase == “moved”) then

– Update the joint to track the touch
body.tempJoint:setTarget(x_local, y_local);

elseif(phase == “ended” or phase == “cancelled”) then
stage:setFocus(body, nil);

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

– Stop further propagation of touch event
return true
end
local doBall = function(xBase, yBase, radius)
ball = display.newCircle(xBase, yBase, radius )
ball:setFillColor(255,0,0)
testGroup:insert(ball)
physics.addBody(ball, “dynamic”, {density = 5.0, friction = 0, bounce = 0, radius = radius})
ball:addEventListener(“touch”, dragBall);
end

doBall(50,50,20)

testGroup:setReferencePoint(display.CenterReferencePoint)
testGroup.x , testGroup.y = display.contentCenterX, display.contentCenterY

[/code] [import]uid: 23636 topic_id: 26827 reply_id: 108979[/import]

Hmm. I tried the exact same thing but the joint is still appearing -40 pixels above the actual touch point. What else can I do? Even if I add a hard-coded +40 pixels to the joint position, then it just moves the physicsbody down that far and the touch joint in Hybrid display mode still shows up -40 pixels above it.

You see I moved the game group that contains all the physicsbodies that need to interact with each other up 40 pixels to create a scrolling effect and reveal the next portion of the level.

game:contentToLocal(x,y) works great for everything except the physics joints. The hybrid view shows them still floating 40 pixels above where I told them to go. touch events got adjusted to the right spot using that approach, and everything but the physics joints.

Am I just seeing a glitch and the joints are really where they belong, just being hybrid-drawn in the wrong place?

Actually I should probably open a new thread, eh? [import]uid: 63787 topic_id: 26827 reply_id: 125236[/import]

Hmm. I tried the exact same thing but the joint is still appearing -40 pixels above the actual touch point. What else can I do? Even if I add a hard-coded +40 pixels to the joint position, then it just moves the physicsbody down that far and the touch joint in Hybrid display mode still shows up -40 pixels above it.

You see I moved the game group that contains all the physicsbodies that need to interact with each other up 40 pixels to create a scrolling effect and reveal the next portion of the level.

game:contentToLocal(x,y) works great for everything except the physics joints. The hybrid view shows them still floating 40 pixels above where I told them to go. touch events got adjusted to the right spot using that approach, and everything but the physics joints.

Am I just seeing a glitch and the joints are really where they belong, just being hybrid-drawn in the wrong place?

Actually I should probably open a new thread, eh? [import]uid: 63787 topic_id: 26827 reply_id: 125236[/import]