Converting between global and local coordinates?

Does Corona provide any method to convert from a global x|y coord to the same pixel within a scaled and rotated group (and vice versa)?

As an example, I have some moving objects, placed in a group. As long as the group has not been scaled or rotated, its coordinate system is the same as the global one. I want to check if some of the objects moved outside the visible screen (to delete them), so I check the objects coords against display.contentWidth / display.contentHeight.

When the group has been scaled down to 0.5 however, this does not work anymore since both coordinate systems do not fit anymore.

Is there any way to transform a point from global to a local system?

[import]uid: 9644 topic_id: 2738 reply_id: 302738[/import]

* Bump * -No solution here?

To know what I mean, simply imagine a stack of some paper cards that are are randomly shifted and rotated. Now stab a needle into the stack that penetrates all cards. How can I convert the coordinate of the needle puncture from one of the card’s local coordinate system to another card’s local coordinate system (so that both coordinates describe the same point on screen, no matter if the cards have been moved or rotated)?

I could not find such a method yet. However, Flash provides it (localToGlobal, globalToLocal), Blitz3D provides it ( TFormPoint ) and Unity provides it, too.

This is *really* neccessary to compare coordinates between different groups.

Is there any way to do so?
[import]uid: 9644 topic_id: 2738 reply_id: 8370[/import]

So no one has an answer? [import]uid: 2927 topic_id: 2738 reply_id: 9006[/import]

Hi MauMau and Cixate,
I am unsure of if there are any native functions in Corona that allow this translation.

Other than that, it is a matter of simple maths.

You know the global co-ordinates of the point, lets say x,y
You know where card1 is globally, say rect1
You know where card2 is globally, say rect2

 local x //The point in global co-ordinates  
 local y //The point in global co-ordinates  

So the point in the local co-ordinates for rect1 are

 pin\_x1 = rect1.left - x   
 pin\_y1 = rect1.top - y  

the point in local co-ordinates for rect2 are

 pin\_x2 = rect2.left - x   
 pin\_y2 = rect2.top - y  

Now, you have the co-ordinates that you needed…

If you understand this simple math, you can apply it the otherway round and translate from a local point to the global points using the same math.

NOTE: This will work for rectangular regions that have not been rotated, calculating it for rotated objects is a bit more involved and I leave that for you as an exercise.

I guess you will appreciate why a games programmer has to have a good understanding of basic Maths, both Algebra and Geometry

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 2738 reply_id: 9145[/import]

:slight_smile:

>>I guess you will appreciate why a games programmer has to have a good understanding of basic Maths, both Algebra and Geometry

WOW, with that attitude, you are my new hero here.

I guess you just missed the part where he asked for something when a group is scaled and/or rotated. [import]uid: 5712 topic_id: 2738 reply_id: 9149[/import]

Hi Mike,
I am not sure if that was sarcastical or what, as they say, if I did not understand it it did not hurt, so I will take it as positive.

My point here was not about my skills, but the fact that a programmer has to have more well developed skills, and a games programmer even more.

I started my journey with the spectrums, those rubbery small machines and I learned programming with reverse engineering be hand disassembling the source, which had to be first loaded into memory without it autoexecuting and cracking the tape loaders, etc.

I kind of shied away from math, and I developed software mostly for enterprise use, mainly sales, service and inventory then went on to manage the IT departments. You cannot take away the programmer from the programmer, this got boring. Now I have come back full circle back to games development, I have a few games on the app store written in Objective-C but I had to learn a few things and most of them were simple tricks that were since days of Turbo-C 1, where you had to initialise the screens, write the code for graphics, swap the buffers, etc.

These days, they don’t teach proper skills at universities, (incidentally I teach at the university, but I teach Unix/Linux Administration) and I see that a lot of basic skills are not taught which help in being better developers.

so, the sentence was also what I tell some of my students and others when I rn community workshops on iOS development, etc, that you can be a developer by purchasing a machine and the $99 subscription, kids are no doubt quite smart these days, a lot smarter than I am for sure. However, it will always be helpful to have a well grounded understanding of basic concepts.

I am new to the Corona language and hence I have a lot of questions on “how to…” I am trying to map out the equivalents as I read about each command.

Till then there are quite a few “how to” questions and advice to developers.

Again, if you really mean that, thanks for the complement.

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 2738 reply_id: 9163[/import]

Yes, it is simple map, so simple to be tedious and something that should be provided by the framework.

It’s only as simple as you described it if you are nested one level deep. You can subtract your object’s parent’s position from the objects position to figure out it’s world coordinates. If there’s scaling involved you have to multiple the position by the x and y scale. With rotation involved it takes a bit of geometry to figure out the rest. That’s just for one level. You have to do this, crawling all the way up the parents to figure it out if you have have say 4 or 5 levels of grouping which is typical if you allow for the viewport, the view, the game world, the hero, and elements on the hero. While all this can be done, doing it by crawling up the chain is inefficient. If we have to do it in lua via the methods provided by the framework this is tedious to do and slow to execute. It should be provided in a more direct route by the framework which has access to better means of figuring this out (I assume).

In any case, since it is so simple, needed often, and really easy to screw up, it should be provided by the Corona. [import]uid: 2927 topic_id: 2738 reply_id: 9166[/import]

Hi Cixate,
I understand your concerns regarding lua. However I started development with Basic on 8Mhz machines, so tell me about speed of processing. No langauge can have everything, each has some Pros and Cons. Hopefully, Ansca will keep adding features that are important to the masses.

Did you try to see if you can write a function and call it recursively to transverse the hierarchy of your application. Instead of generating for each object, generate the co-ordinates for the group and then it is a matter of translating from there on.

Other option is to keep a translation map (if this is so important for your app) this is updated for the object whenever the object is moved, that way, just read the co-ordinates off this table. Much faster access than determining each co-ordinate at every event.

Speed can be an issue, but there is a trade off, ease of use comes at a price. If you want pure speed, use C++, but then you know how complicated this can become.

hope this helps,

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 2738 reply_id: 9171[/import]

I completely agree with cixate. It’s actually *not* a simple task to transform coordinates between layers that are nested / scaled / rotated. Corona is meant to be a rapid game development tool for indy developers. So Corona users shouldn’t have to mess around with low-level tasks or matrix transformations (wich would be required to solve this task).

Rapid game development is crucial for us indy developers who cannot efford to spend hours and hours for every basic task. [import]uid: 9644 topic_id: 2738 reply_id: 9579[/import]

Hi MauMau,
Yes, Corona should have everything, a built-in Dishwasher and a Butler Robot, etc. The bottom line is it doesn’t.

Yes, as an Indie developer cannot spend hours on basic tasks an in the era where programs are generated by clicking on a few buttons, yes, you have a point.

If you look up names like Hewson, Codemasters, Ocean, Imagine, you will realise that these were publishing houses of yesteryear’s, and people have spend 9.00 quid to 19.99 quids on their games on tape.

The best part is that many of the games were made by Indie developers that wrote the entire game, made graphics and sound for them and then got them published via one of these big names (if it was good enough for a commercial release).

In the scenario that Corona cannot do many of the things, developers look for alternative ways to achieve what they want, that is how the limits of the Hardware and software are pushed and spectacular results are created.

In summary to our question, well, Corona does not do such translation and mapping, you should wait for Corona to include that in the next release (if it is on the priority list) or try to gather what you have and learn and use it now.

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 2738 reply_id: 9582[/import]

@jayantv: Do NOT confuse me with any of those GameSalad non-scripting-users. I’ve been working with all major game dev kits around there in the last years and I know how to code my own box of tools. And because of this experience I can clearly say that a game dev kit that features coordinates matrices but does not provide an API to use this feature is simply not standard. At least there is a VERY good reason why to have it implemented as a native feature: PERFORMANCE!

So come on, use your brain first, before defaming users here.

To use your own words: if you buy a device that is labeled “Dishwasher”, you expect what? Right, that it cleans your dishes. In the same way you can expect game developing standards from a game dev kit. Ever seen a 3D game dev kit that doesn’t provide a rotation function but lets you mess around with matrices on your own? Surely not. Especially not if it is dedicated to RAPID game development. So if Corona supports nested coordinate systems (which is a really great feature!) there should also be a (fast) API to convert coords from one coord set to another, especially if Corona ALREADY DOES internally. So it wouldn’t be a big thing to provide an interface to it.

Why inventing the wheel a second time here? [import]uid: 9644 topic_id: 2738 reply_id: 10075[/import]

@MauMau,
I will leave you to your explorations, sorry I tried to help.

And by no means have I tried to belittle your efforts and experience with the numerous tools that you have worked with in the past year.

I found you comment quite interesting,
“Ever seen a 3D game dev kit that doesn’t provide a rotation function but lets you mess around with matrices on your own?”

Yes, I have seen that and more, I have seen enterprise apps that do accounting where the balances do not tally. So there is nothing that surprises me. Starting development with assembly, I am not exactly used to what’s provided, so I think differently, yes as a customer, you might want things in-built.

I take the example of Native components, namely UITableView, on the iOS that is one very important component, creating the same in LUA is Slllooooowwww, I know what you mean. I wish you got what I was trying to convey.

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 2738 reply_id: 10076[/import]

Sorry if my post above appeared a little angry. This wasn’t my intention. Perhaps it’s because I am sometimes annoyed myself with all those “game developers” who want to create games without making their hands dirty and do not want to do things theirselves. There are lots of features in the feature request forums that are requested for a single project only -nothing of use for the every-day development.

Coordinate transformation, however, is not. In fact, it’s fundamental. How would you compare coordinates between objects that are nested in different (rotated, scaled, moved) layers? Or just detect if a nested object moved out of screen? Those are very, very basic and common tasks you need in almost every single game.

Sure, I could code such a routine myself. But it would be simply too slow to be really useful, especially for a fast-paced game. I had to traverse through every parent, doing matrix calculations in LUA. This is simply not efficient. This is why I requested this feature. It’s not for simple convenience. *
* Otherwise I had requested a built-in MakeTombRaiderGame( ) function.

** Is there any? [import]uid: 9644 topic_id: 2738 reply_id: 10089[/import]

Hi MauMau,
No offense taken, I can understand your anxiety, I go through that myself at times.

I guess this is requested by a lot of developers and should be addressed as soon as possible. I agree with your point that this is not just for convenience.

But, MakeTombRaiderGame() function might not be so much to my liking :wink: even if it existed on the other hand a startRedAlert3() would be fun …

well, wishful thinking… till then I can just start that from the dock :slight_smile:

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 2738 reply_id: 10090[/import]

My concerns were performance as well. Corona went out of it’s way to be Flash like, which has a way to convert coordinates, but completely missed that functionality.

If you’re using a 3D library that doesn’t provide this, you’re using the wrong library or at least doing it the hard way. Libraries like Corona are there for rapid development so doing the common thing should be part of the framework so you don’t have to do it the hard way, even if you know how to. [import]uid: 2927 topic_id: 2738 reply_id: 10096[/import]

I don’t have much useful to add to this discussion except that we definitely recognize the need for a easy way to convert between local/global coordinates. Thanks for your patience!

Tim [import]uid: 8196 topic_id: 2738 reply_id: 10127[/import]

Thanks a lot, Tim -it feels good to be finally noticed after two weeks of desperate begging ^^

Seriously, this means much to me. I’ll definately sleep better tonight. [import]uid: 9644 topic_id: 2738 reply_id: 10130[/import]

@MauMau, sweet dreams. [import]uid: 8196 topic_id: 2738 reply_id: 10163[/import]

Is this still a someday maybe feature?

I’m looking for something akin to AS3’s localToGlobal & globalToLocal to map Runtime touch coordinates to randomly nested display objects. [import]uid: 4596 topic_id: 2738 reply_id: 28456[/import]

function globalToLocal( point, target )  
  
 local X = point.x  
 local Y = point.y  
  
 while target.parent do  
 X = X - target.x  
 Y = Y - target.y  
 target = target.parent  
 end  
 return { x=X, y=Y }  
end  

Although I don’t know how to calculate with scale/rotation transformations, especially with different ReferencePoints. [import]uid: 4596 topic_id: 2738 reply_id: 29928[/import]