Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
What's the global position of an object? — Gideros Forum

What's the global position of an object?

seppseppseppsepp Member
edited June 2014 in General questions
Hi,

is there a more _native_, direct and faster way to calculate an object's global position rather than going through the object's graph via getParent() an sum up positions? It works, but doesn't _feel_ right. Have I overlooked something? Some object.globalX or the like?


Thanks a lot in advance

Sebastian

Comments

  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @seppsepp sure
    local x, y = sprite:getPosition()
    local globalX, globalY = sprite:getParent():localToGlobal(x, y)
  • Thats a quick answer :D !

    But it doesn't work as expected, because results are not the same as if done manually ^^. Does localToGlobal go through the whole graph? It stops at the parent right? I guess I just don't get how localToGlobal works =| .
  • seppseppseppsepp Member
    edited June 2014
    Actually the result seems always to be what I pass to localToGlobal().
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    the global coordinates are basically what are at the stage level

    So the local coordinates are how children positioned relatively to the parent
    And global coordinates are how children positioned relatively to the stage

    So if your sprite is added to the stage directly, then the results should be the same.

    here is a little test:
    local parent = Sprite.new()
    parent:setPosition(100, 100)
    stage:addChild(parent)
     
    local child = Sprite.new()
    child:setPosition(50, 50)
    parent:addChild(child)
     
    print(parent:getPosition()) -- prints 100 100
    print(child:getPosition()) -- prints 50 50
    print(parent:localToGlobal(child:getPosition())) -- prints 150 150
    print(parent:globalToLocal(20, 20)) -- prints -80 -80
  • seppseppseppsepp Member
    edited June 2014
    Yes, your example works. But (there is always a "but" ;) ) for deeper nested sprites I still have to go through all the parents - at least that's what it looks like. I can't just asked stage:localToGlobal(deeplyNestedSprite:getPosition()). Or am I wrong?
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @seppsepp it should convert coordinates no matter how deep you are
    and it usually worked as expected for me

    so what do you have so specific that it won't work? :)
  • Ah ok, your are right! I added another sprite as child to the first one ^^. But I still asked parent instead of the real parent. My fault.

    Thanks a lot for your answer.
  • MellsMells Guru
    edited June 2014
    @ar2rsasween so if :

    stage
    -grandparent
    --parent
    ---child

    to get the global position of child should we ask :
    -> a) parent:localToGlobal(child:getPosition())
    or
    -> b) stage:localToGlobal(child:getPosition())

    In your little test a) you do it on the direct parent and in that case how to know if parent is at the top of the hierarchy?
    but in @seppsepp 's example b) it's done on the stage

    I did not understand what you explained in your last answer @seppsepp sorry ^^
    I've always struggled with localToGlobal()
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • ar2rsawseenar2rsawseen Maintainer
    edited June 2014
    @Mells definitely a) parent:localToGlobal(child:getPosition())

    b) would return back same position as provided, because stage is global and and if you give coordinates that are relative to the stage, they are also the same as global
    Unless of course you changed position of stage, which is possible but not recommended as it again complicate stuff a lot :D

    I actually don't know if I made it clearer or worse? :D
  • seppseppseppsepp Member
    edited June 2014
    I actually don't know if I made it clearer or worse? :D
    What you said in a): Ask the direct parent to get the global position by passing the asking objects position into the request.

    ...somethings like that :D .
  • @ar2rsawseen thanks

    // actually don't know if I made it clearer or worse?
    weeeeeell :)

    so I now understand that :

    1)
    stage
    -grandparent
    --parent
    ---child

    -> child:getParent():localToGlobal(child:getPosition())

    and
    stage
    -grandparent
    --parent
    ---child
    (...)
    (...)
    ----------- grandGrandChild

    2) grandGrandChild:getParent():localToGlobal(grandGrandChild:getPosition())

    those would then be the two correct ways to get the global coordinates for child and grandGrandChild if I understand well.

    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
Sign In or Register to comment.