Calculate globalToLocal manually?
- AnteHero
-
AnteHero
- Member since: Sep. 5, 2013
- Offline.
-
- Forum Stats
- Member
- Level 01
- Blank Slate
Does anyone know how to implement globalToLocal and localToGlobal functions manually?
I've written my own displayObject class which doesn't extend DisplayObject and really need these functions implemented.
- Sam
-
Sam
- Member since: Oct. 1, 2005
- Offline.
-
- Forum Stats
- Moderator
- Level 19
- Programmer
At 4/30/14 02:48 PM, AnteHero wrote: Does anyone know how to implement globalToLocal and localToGlobal functions manually?
I've written my own displayObject class which doesn't extend DisplayObject and really need these functions implemented.
You need to get the objects position relational to its parent, add this to its parents relation to its parent and so on until some common ancestor of all of your display objects is reached.
- Sam
-
Sam
- Member since: Oct. 1, 2005
- Offline.
-
- Forum Stats
- Moderator
- Level 19
- Programmer
At 4/30/14 03:17 PM, Sam wrote:At 4/30/14 02:48 PM, AnteHero wrote: Does anyone know how to implement globalToLocal and localToGlobal functions manually?You need to get the objects position relational to its parent, add this to its parents relation to its parent and so on until some common ancestor of all of your display objects is reached.
I've written my own displayObject class which doesn't extend DisplayObject and really need these functions implemented.
My bad, skim read your post. That should be localToGlobal if I'm not mistaken. While I've never had to use globalToLocal, I'd have to assume it's exactly the same but reversed. Take some point in the global scope, a solution to getting that point local to an object (I think) would be to: localToGlobal your objects position, find the difference between the point you want to localise and the objects global position.
I might be way off mark though.
- GeoKureli
-
GeoKureli
- Member since: Apr. 1, 2003
- Offline.
-
- Forum Stats
- Supporter
- Level 19
- Game Developer
At 4/30/14 02:48 PM, AnteHero wrote: Does anyone know how to implement globalToLocal and localToGlobal functions manually?
I've written my own displayObject class which doesn't extend DisplayObject and really need these functions implemented.
it's just about transforming points up the heiarchy. be mindful of position scale and rotation.
The easiest way is to use the Matrix class.
var mat:Matrix = new Matrix();
mat.rotate(90);
mat.scale(1.5, 2);//x,y
mat.translate(50, 45)// x,y
var p:Point = mat.transformPoint(new Point(0,0));
the order you do these in is insanely important! heres an article explaining that. but if your custom Display Object does not have rotation, then Matrux transformations is overkill. and if your custom dislayObject does not have scaling, then yeah its just as simple as adding the parents position. But whatever fnction you make should be recursive, because the a displayobject can have a parent, a grandparent, a great grand parent, and so on.
Out of curiosity, if you're not extending DisplayObject, how are you drawing it? are you drawing abstract classes to a bitmap? If so, you might just wanna try flixel
This blog I made | This game sucks | FlxGreed | Home Run Swingers: Rhythm Baseball
many typos
- AnteHero
-
AnteHero
- Member since: Sep. 5, 2013
- Offline.
-
- Forum Stats
- Member
- Level 01
- Blank Slate
At 4/30/14 10:03 PM, GeoKureli wrote:At 4/30/14 02:48 PM, AnteHero wrote: Does anyone know how to implement globalToLocal and localToGlobal functions manually?it's just about transforming points up the heiarchy. be mindful of position scale and rotation.
I've written my own displayObject class which doesn't extend DisplayObject and really need these functions implemented.
The easiest way is to use the Matrix class.
var mat:Matrix = new Matrix();
mat.rotate(90);
mat.scale(1.5, 2);//x,y
mat.translate(50, 45)// x,y
var p:Point = mat.transformPoint(new Point(0,0));
the order you do these in is insanely important! heres an article explaining that. but if your custom Display Object does not have rotation, then Matrux transformations is overkill. and if your custom dislayObject does not have scaling, then yeah its just as simple as adding the parents position. But whatever fnction you make should be recursive, because the a displayobject can have a parent, a grandparent, a great grand parent, and so on.
Out of curiosity, if you're not extending DisplayObject, how are you drawing it? are you drawing abstract classes to a bitmap? If so, you might just wanna try flixel
Thanks I'll give it a shot.
Yeah I'm blitting on to a bitmap, I've looked at Flixel before but never really got into it.
My custom class suits my purposes quite well for now, I've also built in a custom rectangle class which allows me to do ray casting in 2D which is quite nice.
I may give Flixel another shot but not anytime soon.

