Math to remap touch coords from one place to another

I just started working with Corona and love it. I’ve gotten pretty far without having to post anything, but now I’m at a point where I’m just not getting it.

Here’s the situation:

  • display group with a background, and some cards on the background that are added by double tapping on this background
  • The whole display group is zoomable and pannable

The problem is this:

  • Getting the double tap on the background = add new card
  • touch and drag on the background. Reason I’m capturing the touch on the background is because each card is also draggable. Seems if I capture the drag from the entire display group it’ll work, BUT then I can’t capture drags on any containing objects.
  • Right now, double tap to do this will add the card when it’s zoomed, but the x/y is based on the coords of the background. I did some math that adds it with the offset so it’s at least RIGHT:
    – from inside a listener on the bg
    cardbg.x = event.x + (display.contentWidth - desktopGroup.width)
    cardbg.y = event.y + (display.contentHeight - desktopGroup.height)
  • Problem with this is most of the time the card will be place off the zoomed view somewhere and the user won’t know about it and think it didn’t work.

What I need to do is have it only add the card under the mouse in the viewport no mater what the zoom or pan. Can somebody that’s better at math then me please tell me the magic sauce for this?

Thanks all!

P.S. Maybe there’s a better way to do this than what I have outlined? What about a hit test from the touch? [import]uid: 169520 topic_id: 29784 reply_id: 329784[/import]

Maybe to put it another way:

How can I add a display item at a screen relative X/Y into a display group after said display group has been scaled up and moved around on the screen?

Or, how can I make sure that when I touch the screen, whatever I add, even if it’s into a scaled/panned display group, will be added only into the visible area?

I’d be happy with just getting it added to the visible area and not be pixel accurate with regard to the touch x/y. [import]uid: 169520 topic_id: 29784 reply_id: 119532[/import]

“How can I add a display item at a screen relative X/Y into a display group after said display group has been scaled up and moved around on the screen?”

If you’re adding into a display group, it doesn’t matter whether the display group was scaled and moved around - when you add in it, relative to other objects in it, it is as if it is scaled and placed as it originally was - that’s the whole purpose of display groups. [import]uid: 160496 topic_id: 29784 reply_id: 119540[/import]

Thanks for your response.

Right, that part works as expected. In fact, it’s doing exactly what it’s suppose to do regarding display groups. I double tap, it adds the item at the x/y. The problem is:

  • the x/y is say 120/240 on the visible screen
  • the display group is zoomed in (scaled up)
  • it adds the item to 120/240 scaled up and it thus off the screen and unseen most of the time
  • user has to pan around or zoom out to see new object unless it just HAPPENS to luckily be visible

What I’d like to have happen is the object would get added only to the visible area of the scaled display object. It’s not absolutely necessary that it be under the double tap, just that it’s only added to the display groups visible area even if it’s zoomed all the way in.

Make sense? [import]uid: 169520 topic_id: 29784 reply_id: 119542[/import]

Ah I see. Look up contentToLocal and localToContent functions. [import]uid: 160496 topic_id: 29784 reply_id: 119548[/import]

Hmmm, okay Mike, I’ll check those out. Thank! [import]uid: 169520 topic_id: 29784 reply_id: 119550[/import]

Maybe to put it another way:

How can I add a display item at a screen relative X/Y into a display group after said display group has been scaled up and moved around on the screen?

Or, how can I make sure that when I touch the screen, whatever I add, even if it’s into a scaled/panned display group, will be added only into the visible area?

I’d be happy with just getting it added to the visible area and not be pixel accurate with regard to the touch x/y. [import]uid: 169520 topic_id: 29784 reply_id: 119532[/import]

“How can I add a display item at a screen relative X/Y into a display group after said display group has been scaled up and moved around on the screen?”

If you’re adding into a display group, it doesn’t matter whether the display group was scaled and moved around - when you add in it, relative to other objects in it, it is as if it is scaled and placed as it originally was - that’s the whole purpose of display groups. [import]uid: 160496 topic_id: 29784 reply_id: 119540[/import]

Thanks for your response.

Right, that part works as expected. In fact, it’s doing exactly what it’s suppose to do regarding display groups. I double tap, it adds the item at the x/y. The problem is:

  • the x/y is say 120/240 on the visible screen
  • the display group is zoomed in (scaled up)
  • it adds the item to 120/240 scaled up and it thus off the screen and unseen most of the time
  • user has to pan around or zoom out to see new object unless it just HAPPENS to luckily be visible

What I’d like to have happen is the object would get added only to the visible area of the scaled display object. It’s not absolutely necessary that it be under the double tap, just that it’s only added to the display groups visible area even if it’s zoomed all the way in.

Make sense? [import]uid: 169520 topic_id: 29784 reply_id: 119542[/import]

Ah I see. Look up contentToLocal and localToContent functions. [import]uid: 160496 topic_id: 29784 reply_id: 119548[/import]

Hmmm, okay Mike, I’ll check those out. Thank! [import]uid: 169520 topic_id: 29784 reply_id: 119550[/import]