Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Need help with contentToLocal()
Started by firemaplegames Feb 21 2019 07:08 AM

- - - - -
3 replies to this topic

Best Answer firemaplegames , 21 February 2019 - 09:03 AM

Yes!!!!!!!!!!!!!!!!!!!!!!!!!!

 

Thank you so much! That totally did the trick!

 

The wires on my steam shovel animation are now connected properly :-)

I had tried localToContent(0,0) originally but it was giving me different results on different screens and it was freaking me out. :-)

I didn't want the equation to be wrong, or the animation to play differently on different devices.

I had thought it was contentToLocal but couldn't figure out how to implement it.

You are the man.

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

firemaplegames

[GLOBAL: userInfoPane.html]
firemaplegames
  • Contributor

  • 178 posts
  • Corona SDK

Hey everyone!

 

After a few hours of noodling, I could use some help wrapping my head around this...

I believe I need to use contentToLocal() but I'm not sure. Blood is now coming out of my ears :-)

 

I'm essentially trying to draw a line between two display objects.

 

My "line" is a simple .png of a rectangle that is 3px tall and 100px wide. It will be stretched and rotated appropriately between the two objects.

 

I know I need to get the distance between the display objects using square root, and the angle between them using atan2.

 

I don't have a problem with any of that, my problem is that I cannot figure out the correct x and y of each object because they are all in different display groups.

 

My situation is this:

 

I have 3 Display Groups: Dot1Group, Dot2Group, and LineGroup.

 

dot1 is a display object in the dot1Group.

dot2 is a display object in the dot2Group.

myLine is a display object in the lineGroup.

 

Dot1Group and Dot2Group are both moving around and rotating.

 

I'm just trying to stretch the line between the two dots.

If they were all in the same display group it wouldn't be a problem, but this is breaking my mind.

Any help is very much appreciated!

 

Warm regards,

Joe

 



[TOPIC: post.html]
#2

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Corona Geek

  • 3,069 posts
  • Corona SDK

If you want to get the real (content) location of any particular display object, make sure you are talking about the correct object and call :localToContent(0,0) on it. By "correct object" I mean the display object you want the location for and not it's parent group.

 

So, let's say you want the location on screen of the dot1 object, you do this:

local dot1X, dot1Y = dot1:localToContent(0,0)

And repeat for the other objects. Bear in mind that the line object won't give you anything useful and you will need to remove and create that object each frame, or whenever.

 

Now, getting the angle, distance etc between the dot1 and dot2 should be easy, because you've now got their real world coordinates (content x and y's.)

 

But, you want to draw the line in the LineGroup, so for dot1x,dot1y and dot2x,dot2y you need to convert all of those into LineGroup-relative values, like this:

local lineDot1x,lineDot1y = LineGroup:contentToLocal(dot1x,dot1y)
local lineDot2x,lineDot2y = LineGroup:contentToLocal(dot2x,dot2y)

Then you can draw a line inside the LineGroup using those values.

 

Given the code above, the xxxGroup display groups can be anywhere and you should get a line directly between dot1 and dot2.



[TOPIC: post.html]
#3

firemaplegames

[GLOBAL: userInfoPane.html]
firemaplegames
  • Contributor

  • 178 posts
  • Corona SDK

  Best Answer

Yes!!!!!!!!!!!!!!!!!!!!!!!!!!

 

Thank you so much! That totally did the trick!

 

The wires on my steam shovel animation are now connected properly :-)

I had tried localToContent(0,0) originally but it was giving me different results on different screens and it was freaking me out. :-)

I didn't want the equation to be wrong, or the animation to play differently on different devices.

I had thought it was contentToLocal but couldn't figure out how to implement it.

You are the man.



[TOPIC: post.html]
#4

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 768 posts
  • Corona SDK

Just read the post. I could have sworn that @horacebury's answer was somewhat better.

Yeah, I'll just show myself out.  B)




[topic_controls]
[/topic_controls]