Drag and drop haywire after moving displayGroup...

Hi All.

I have tried to implement some camera tracking and touch to move camere, but I keep running in to a problem…

It seems my very simple drag and drop code Begins to misbehave if i move the displayGroup where the obejct i want to drag is placed.

For example I have this tracking code:

function track(self,event) local target = self if target ~= nil then local targetx = 100 - target.x local targety = 100 - target.y screenGroup.x = screenGroup.x + ((targetx - screenGroup.x)\*0.1) --screenGroup.y = screenGroup.y + ((targety - screenGroup.y)\*0.1) end end

I put it on the object I want to drag, but as soon as I do this I become unable to move the object in the Y direction.

I also tried another example where I just move the displayGroup to the left very simply by screenGroup.x =  screenGroup.x - 10

This also result in this behaviour…

I drag and drop with this code

function dragAndDrop( event ) -- A general function for dragging physics bodies 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 body.tempJoint = physics.newJoint( "touch", body, event.x, event.y ) elseif body.isFocus then if "moved" == phase then -- Update the joint to track the touch 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

When I view the app in debug mode it seems the line between my mouse pointer and the object i move is flying around the screen, and the object does the same …

Any ideas … Is it something like I need to put the joint into the screengroup somehow…

Hi All.

So I surfed the forum and found Horaceburys reply to

http://forums.coronalabs.com/topic/37667-moving-a-display-group-wont-move-the-physics/?hl=%2Bmoving+%2Bphysics+%2Bobjects#entry195541

It seems he has the trick … Maybe I am wrong but there should allways be a parent layer that does not move.

So i created a mainView layer and inserted screengroup from above …

That did the trick … I am now dragging perfectly!

And now I just tested correctly :frowning:

It still does not Work … I just didn’t move the layer when testing …

So still an issue.

You can’t move the parent group that the physics objects are in. If you do, only the graphics will move. Then physics objects stay where they are, even though you can’t see them. That’s why we have hybrid mode.

Hi … to further explain …

It is moving objects and physics as such…

I see that in hybrid mode.

It is the

body.tempJoint = physics.newJoint( "touch", body, event.x, event.y )  

That is not working correctly … It seems that this is working from “origin position” maybe…

Here I attached image to show issue. Notice the purple joint line it is not really going to the object but out of the frame …

Green box and the snake object is with applyed physics and container is scrolled to the right.

Physics Work as they should except for dragging, meaning that the snake bounces perfectly of the walls…

So finally I found the solution

function gMechs.drag(e) local phase = e.phase local stage = display.getCurrentStage() if (e.phase == "began") then e.target.hasFocus = true local x, y = screenGroup:contentToLocal( e.x, e.y ) e.target.touchjoint = physics.newJoint( "touch", e.target, x, y ) stage:setFocus( e.target ) return true elseif (e.target.hasFocus) then if (e.phase == "moved") then local x,y = screenGroup:contentToLocal(e.x, e.y) -- This line does some magic where the joint gets some better positioning (It still needs some tuning but I am sure anyone with the issue will be able to continue from here) e.target.touchjoint:setTarget( x, y ) --xOffset, yOffset = calcTouchOffset( e ) else e.target.hasFocus = false e.target.touchjoint:removeSelf() e.target.touchjoint = nil stage:setFocus( nil ) end return true end return false end

It was found in reply from aukStudios in this post:

http://forums.coronalabs.com/topic/35579-scrolling-and-dragging-bug/?hl=%2Bnewjoint+%2Btouch#entry184310

Thanks to aukStudios :slight_smile:

Add on … for anyone interested.

To make it layer independent I found that 

   local x, y = target.parent:contentToLocal( e.x, e.y )  

Is a better way as it will make sure that no paralaxing or other functions distort the dragging… And makes sure that the “localized” coordinates is from the layer where the dragged object resides.

This was what i ment by “some tuning needed”

Hi All.

So I surfed the forum and found Horaceburys reply to

http://forums.coronalabs.com/topic/37667-moving-a-display-group-wont-move-the-physics/?hl=%2Bmoving+%2Bphysics+%2Bobjects#entry195541

It seems he has the trick … Maybe I am wrong but there should allways be a parent layer that does not move.

So i created a mainView layer and inserted screengroup from above …

That did the trick … I am now dragging perfectly!

And now I just tested correctly :frowning:

It still does not Work … I just didn’t move the layer when testing …

So still an issue.

You can’t move the parent group that the physics objects are in. If you do, only the graphics will move. Then physics objects stay where they are, even though you can’t see them. That’s why we have hybrid mode.

Hi … to further explain …

It is moving objects and physics as such…

I see that in hybrid mode.

It is the

body.tempJoint = physics.newJoint( "touch", body, event.x, event.y )  

That is not working correctly … It seems that this is working from “origin position” maybe…

Here I attached image to show issue. Notice the purple joint line it is not really going to the object but out of the frame …

Green box and the snake object is with applyed physics and container is scrolled to the right.

Physics Work as they should except for dragging, meaning that the snake bounces perfectly of the walls…

So finally I found the solution

function gMechs.drag(e) local phase = e.phase local stage = display.getCurrentStage() if (e.phase == "began") then e.target.hasFocus = true local x, y = screenGroup:contentToLocal( e.x, e.y ) e.target.touchjoint = physics.newJoint( "touch", e.target, x, y ) stage:setFocus( e.target ) return true elseif (e.target.hasFocus) then if (e.phase == "moved") then local x,y = screenGroup:contentToLocal(e.x, e.y) -- This line does some magic where the joint gets some better positioning (It still needs some tuning but I am sure anyone with the issue will be able to continue from here) e.target.touchjoint:setTarget( x, y ) --xOffset, yOffset = calcTouchOffset( e ) else e.target.hasFocus = false e.target.touchjoint:removeSelf() e.target.touchjoint = nil stage:setFocus( nil ) end return true end return false end

It was found in reply from aukStudios in this post:

http://forums.coronalabs.com/topic/35579-scrolling-and-dragging-bug/?hl=%2Bnewjoint+%2Btouch#entry184310

Thanks to aukStudios :slight_smile:

Add on … for anyone interested.

To make it layer independent I found that 

   local x, y = target.parent:contentToLocal( e.x, e.y )  

Is a better way as it will make sure that no paralaxing or other functions distort the dragging… And makes sure that the “localized” coordinates is from the layer where the dragged object resides.

This was what i ment by “some tuning needed”