centre points of different groups together? (why doesn't this code work as expected?)

Trying to get a short function to work (without success) which aligns an underlaying view (group) such that the specific point on the parent view (group) lines up visually (i.e. on top of each other) a point on the underlying group.   

Code is below?  Know what I’m doing wrong?  It moves the child group ok, but not such the points line up…

display.setStatusBar( display.HiddenStatusBar ) system.setTapDelay( 0.3 ) local function createRefPoint(parent, x, y)     local myCircle = display.newCircle(parent, x,y, 20 )     myCircle:setFillColor( 1,0,0 )     myCircle.strokeWidth = 0     return myCircle end --------------------------------- -- Preset --------------------------------- -- Cild Group local childGroup = display.newGroup() local childRect = display.newRect(childGroup, 50,50,  300,200) childRect.strokeWidth = 3 childRect:setFillColor  ( 1, 0.5 ,1, 0.5 ) childRect:setStrokeColor( 0, 0, 1, 1 ) local childRefPoint = createRefPoint(childGroup,  -(childRect.width/2-50), -childRect.height/2+50) -- Parent Group local parentGroup = display.newGroup() local parentRect = display.newRect(parentGroup, 0, 0,  500, 700 ) parentRect.strokeWidth = 3 parentRect:setFillColor  ( 1, 0 ,0, 0 ) parentRect:setStrokeColor( 0, 0, 1, 1 ) local parentRefPoint = createRefPoint(parentGroup, -parentRect.width/2,-parentRect.height/2) -- Insert Child Group into Parent Group parentGroup:insert(childGroup) -- Rotate Parent parentGroup.x = display.contentWidth/2 parentGroup.y = display.contentHeight/2 parentGroup:rotate( 20 ) -- -- Rotate Child childGroup:rotate( 45 ) childGroup.x = 20 childGroup.y = 250 --------------------------------- -- Helpers --------------------------------- local function log(txt, o) print(txt, "Local:", o.x, o.y, ", Content:", o.parent:localToContent( o.x, o.y ) ) end local function showPosition(parent, x, y)     local myCircle = display.newCircle(parent, x,y, 100 )     myCircle:setFillColor( 1,0,0,0 )     myCircle:setStrokeColor(1,0,0,  1 )     myCircle.strokeWidth = 10     return myCircle end --------------------------------- -- Centre Child Ref Point under Parent Ref Point ---------------------------------    -- \*\*\*\* TRYING TO GET THIS FN TO WORK \*\*\*\* local function centreChildRefUnderParentRef(childRef, parentRef)   local parentGroup = parentRef.parent local childGroup = childRef.parent local pCx, pCy = parentRef.parent:localToContent( parentRef.x, parentRef.y ) local cCx, cCy = childRef.parent:localToContent( childRef.x, childRef.y ) local dCx, dCy = pCx - cCx, pCy - cCy transition.moveBy( childGroup, { x=dCx, y=dCy, time=700 } ) end centreChildRefUnderParentRef(childRefPoint, parentRefPoint)

The child group’s 0,0 location is located within the parent group, so you need to set the child’s x,y to 0,0.

this wouldn’t work as the content of the parent & child views (groups) contain display objects that are offset.  So I’m want to work out how to calculate the variation in position of the child group I would have to apply such that childRefPoint & parentRefPoint would line up

So the code is a test sample of the goal of how to align two Corona groups of objects (my moving one of the groups) such that a specified location on group1 has to line up with a specified location on group2.  I’ve create childRefPoint & parentRefPoint’s to represent an example of the specified points here (if that makes sense).

Anyway, effectively how can I change the code in the function “centreChildRefUnderParentRef(childRef, parentRef)” such that the two points will line up one on top of the other…   (hope this helps clarify the question)

i think you just need to calc delta in parent’s local coord terms (which will be the frame in which the child moves)

roughly:  (that is, not tested, but hopefully closer than what you’ve got)

 local pCx, pCy = parentRef.parent:localToContent( parentRef.x, parentRef.y ) local cCx, cCy = childRef.parent:localToContent(childRef.x, childRef.y) -- ok as is so far, now back to parent's local for delta calc.. local lPx, lPy = parentRef:contentToLocal(pCx, pCy) local lCx, lCy = parentRef:contentToLocal(cCx, cCy) local dCx, dCy = lPx-lCx, lPy-lCy

you nailed it Dave!   thanks

PS.  Interesting I note that in the two second last lines, if you replace “parentRef:contentToLocal” with “parentRef .parent :contentToLocal” this works as well? 

if i followed your code correctly, then “parentRef” is actually your marker circle, right?

since the circle isn’t independently rotated from its group (unlike the childGroup, which is) it inherits the same coordinate frame.

so long as that’s true it wouldn’t matter which you used

arrr yes, I see what you mean re testing.  The generic solution therefore would be to use “parentRef .parent :contentToLocal” then…thanks again

The child group’s 0,0 location is located within the parent group, so you need to set the child’s x,y to 0,0.

this wouldn’t work as the content of the parent & child views (groups) contain display objects that are offset.  So I’m want to work out how to calculate the variation in position of the child group I would have to apply such that childRefPoint & parentRefPoint would line up

So the code is a test sample of the goal of how to align two Corona groups of objects (my moving one of the groups) such that a specified location on group1 has to line up with a specified location on group2.  I’ve create childRefPoint & parentRefPoint’s to represent an example of the specified points here (if that makes sense).

Anyway, effectively how can I change the code in the function “centreChildRefUnderParentRef(childRef, parentRef)” such that the two points will line up one on top of the other…   (hope this helps clarify the question)

i think you just need to calc delta in parent’s local coord terms (which will be the frame in which the child moves)

roughly:  (that is, not tested, but hopefully closer than what you’ve got)

 local pCx, pCy = parentRef.parent:localToContent( parentRef.x, parentRef.y ) local cCx, cCy = childRef.parent:localToContent(childRef.x, childRef.y) -- ok as is so far, now back to parent's local for delta calc.. local lPx, lPy = parentRef:contentToLocal(pCx, pCy) local lCx, lCy = parentRef:contentToLocal(cCx, cCy) local dCx, dCy = lPx-lCx, lPy-lCy

you nailed it Dave!   thanks

PS.  Interesting I note that in the two second last lines, if you replace “parentRef:contentToLocal” with “parentRef .parent :contentToLocal” this works as well? 

if i followed your code correctly, then “parentRef” is actually your marker circle, right?

since the circle isn’t independently rotated from its group (unlike the childGroup, which is) it inherits the same coordinate frame.

so long as that’s true it wouldn’t matter which you used

arrr yes, I see what you mean re testing.  The generic solution therefore would be to use “parentRef .parent :contentToLocal” then…thanks again