Should anchorX, anchorY rotate together with an object?

Hi.

I was having a problem trying to put one image upon another when one of the images was rotated. I noticed that that happened because the anchorX and anchorY kind of rotate together with the object. E.g: If you rotate 90 degrees an object, the anchorX value would be actually the anchorY value.

You can see that situation by running the code below. I create a red rectangle and rotate it. Then I create a green rectangle and set its position to be the same as the red one, but they do not be end overlapped. 

local rectRed = display.newRect(0,0,100,100) rectRed:setFillColor(255/255, 0/255, 0/255) rectRed.anchorX = 0 rectRed.anchorY = 1 rectRed.x = display.contentCenterX rectRed.y = display.contentCenterY rectRed.rotation = 90 local rectGreen = display.newRect(0,0,100,100) rectGreen:setFillColor(0/255, 255/255, 0/255) rectGreen.anchorX = rectRed.anchorX rectGreen.anchorY = rectRed.anchorY rectGreen.x = rectRed.x rectGreen.y = rectRed.y

At first I found that to be a bug, but now I really don’t know. What you guys think?

Hi @Renato,

I’m not sure I understand the case here. If you rotate the green square to 90 degrees, it ends up overlapping the red one. But of course, if you don’t rotate it, the green square resides above the red one. I don’t see anything odd about this case, personally. :slight_smile:

Brent

The point is: If you want to put one object exactly above other (assuming they have the same weight and height dimensions), if you make their anchorX, anchorY, x position and y position equals, they should be in the same position, shouldn’t they?

They are in the same position, before you rotate the one around its non-center point. Remember that setting anchor points actually moves objects too, in relation to that new anchor point. For example, via a transition:

[lua]

local rectRed  = display.newRect(0,0,100,100)

rectRed:setFillColor(255/255, 0/255, 0/255)

rectRed.anchorX = 0

rectRed.anchorY = 1

rectRed.x = display.contentCenterX

rectRed.y = display.contentCenterY

transition.to( rectRed, { time=2000, anchorY = 0 } )

local rectGreen  = display.newRect(0,0,100,100)

rectGreen:setFillColor(0/255, 255/255, 0/255)

rectGreen.anchorX = rectRed.anchorX

rectGreen.anchorY = rectRed.anchorY

rectGreen.x = rectRed.x

rectGreen.y = rectRed.y

[/lua]

Let’s say that I have a object with anchorX, anchorY = 0, 0.  That would mean that this object is using TopLeft Reference point.

If I rotate that object by 90 degrees, the object will start to behave as having a TopRight Reference point.

I am not saying that this is wrong or buggy, only that is not very intuitive… But I am understanding from you that is the normal behavior, so that it is ok.

Hi @Renato,

I’m not sure I understand the case here. If you rotate the green square to 90 degrees, it ends up overlapping the red one. But of course, if you don’t rotate it, the green square resides above the red one. I don’t see anything odd about this case, personally. :slight_smile:

Brent

The point is: If you want to put one object exactly above other (assuming they have the same weight and height dimensions), if you make their anchorX, anchorY, x position and y position equals, they should be in the same position, shouldn’t they?

They are in the same position, before you rotate the one around its non-center point. Remember that setting anchor points actually moves objects too, in relation to that new anchor point. For example, via a transition:

[lua]

local rectRed  = display.newRect(0,0,100,100)

rectRed:setFillColor(255/255, 0/255, 0/255)

rectRed.anchorX = 0

rectRed.anchorY = 1

rectRed.x = display.contentCenterX

rectRed.y = display.contentCenterY

transition.to( rectRed, { time=2000, anchorY = 0 } )

local rectGreen  = display.newRect(0,0,100,100)

rectGreen:setFillColor(0/255, 255/255, 0/255)

rectGreen.anchorX = rectRed.anchorX

rectGreen.anchorY = rectRed.anchorY

rectGreen.x = rectRed.x

rectGreen.y = rectRed.y

[/lua]

Let’s say that I have a object with anchorX, anchorY = 0, 0.  That would mean that this object is using TopLeft Reference point.

If I rotate that object by 90 degrees, the object will start to behave as having a TopRight Reference point.

I am not saying that this is wrong or buggy, only that is not very intuitive… But I am understanding from you that is the normal behavior, so that it is ok.