Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Where is the reference point of nested objects? — Gideros Forum

Where is the reference point of nested objects?

edited November 2013 in General questions
Well, I made a rectangle made of polygons that had its first corner somewhere in the screen that is NOT 0,0.

Then I added a child to it, and set it to 0,0, and it ended on 0,0 of the whole screen.

Then I moved the rectangle, and the child moved too.


My guess is: for shape objects, their 0,0 is the 0,0 of their parent, so if you draw away from their 0,0, their "anchor" ends not inside the actual shape.


Yet I must ask, anyone has clear rules of how reference points and nested objects work? Corona SDK was a nightmare in relation to that (it had some bugs, and their system in general was greatly annoying).
I make games for children: http://www.kidoteca.com

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited November 2013 Accepted Answer
    The default reference point is always 0;0 coordinate

    The only exception is Bitmap, because it can use setAnchorPoint, which relatively to its dimensions changes the reference point.

    And if you use Shape, you can actually choose where to draw the 0,0 coordinate, either inside or outside the Shape.

    You can either draw as:
    local shape = Shape.new()
    shape:beginPath()
    shape:moveTo(0,0)
    shape:lineTo(0, 100)
    shape:lineTo(100, 100)
    shape:lineTo(100, 0)
    shape:lineTo(0, 0)
    shape:endPath()
    and 0,0 will be in one of the corners of triangle
    but we can also draw it in the center
    local halfWidth = 100/2
    shape:beginPath()
    shape:moveTo(-halfWidth,-halfWidth)
    shape:lineTo(-halfWidth, halfWidth)
    shape:lineTo(halfWidth, halfWidth)
    shape:lineTo(halfWidth, -halfWidth)
    shape:lineTo(-halfWidth, -halfWidth)
    shape:endPath()
    No we dealt with reference points, lets check what happens with positioning and nestings.

    So when you use setPosition method, you actually move the 0,0 coordinate to a specified coordinate.

    What might seem confusing that each Sprite internally has it's own coordinate.

    Let's consider this code:
    local sprite = Sprite.new()
    local bmp = Bitmap.new(Texture.new("myimage.png", true))
    sprite:addChild(bmp)
    stage:addChild(sprite)
    Stage contains Sprite, and Sprite contains Bitmap, now let's try to position them like this:
    sprite:setPosition(100, 100)
    bmp:setPosition(-50, -50)
    So what is actually happening? We position the Sprite at 100, 100, which means that Sprite's 0,0 coordinate is at stage's 100, 100

    And the we provide the position of Bitmap, but it's not relative to stage's coordinates, because Bitmap is inside Sprite, thus Bitmap positions is relative to Sprite's internal coordinates.
    So if Sprite's 0,0 is positioned at 100,100, and we position Bitmap at -50,-50 inside Sprite, then relative to stage, Bitmap will actually be at 50, 50 coordinate.

    To find out where Bitmap is relative to other parent, you can use local to global method:
    local x, y = bmp:getPosition()
    print(sprite:localToGlobal(x, y))
    so what we do, is we take Bitmap position (x, and y coordinate), and pass it to Sprite's (parent's in which the provided coordinates are) localToGlobal method, which will convert them to stages (global) coordinates.

    Now another scenario, what if we want to position Bitmap at 75, 75 global stage coordinates, and don't want to calculate, what this position will be inside the Sprite's coordinates. Then we can use globalToLocal.
    local x, y = sprite:globalToLocal(75,75)
    bmp:setPosition(x, y)
    So what happens here, is that we pass to Sprite's globalToLocal method the coordinates where we want to position Bitmap on the stage. And globalToLocal will convert it to Sprite's internal coordinates

    So we simply retrieve this x and y coordinates (hint which will be -25, -25)and set them to the Bitmap (which is inside Sprite). Thus Bitmap is position 75, 75 coordinate relatively to the stage.

    Hope that helps :)
    +1 -1 (+1 / -0 )Share on Facebook
  • Whoa, awesome. This needs to go officially in the docs somewhere.
    I make games for children: http://www.kidoteca.com
  • Yes and it will, I just need to figure out where.
    It could go into blog post, it could go into wiki, it could go into docs section or it could go into Ultimate Guide or :((
Sign In or Register to comment.