Creating objects where X and Y = mid point - why?

I am beginning to get frustrated by certain implementations for graphics 2, and I’d be curious to know why some things are how they are.

One thing that has caught my attention is how you now specify things from their center points (IE top and left are gone, replaced by X and Y which are now the mid-points of the object).

The most obvious example is newRect().

I have mentioned in the past in the widgets forum my dislike of using the center of objects as reference points (for example you create a rectangle 25 by 25 pixels - the middle is now 12.5, 12.5 pixels - no guarantee of how the graphics engine will round this up or down, especially when you factor in content scaling - it has been this that is the cause of a lot of the widgets graphical bugs because the code uses center points everywhere and the maths just doesn’t always work).

I assume this decision was to make pretty much all object definitions identical in practice (for example, images have always had their reg point created at the center).

Now part of the problem is of course, I’m testing by updating an old project - things would likely be different when I am comfortable with the new way 

It appears I have a few choices when creating a rectangle now:

local rect = display.newRect( 0, 0, width, height )

rect.x = x + math.floor( width / 2 )

rect.y = y + math.floor( width / 2 )

  1. I *think* this would work, not sure!

local rect = display.newRect( 0, 0, width, height )

rect.anchorX = 0

rect.anchorY = 0

rect.x = x

rect.y = y

2a) *would* this work?

local rect = display.newRect( x, y, width, height )

rect.anchorX = 0

rect.anchorY = 0

local rect = display.newRect( x + math.floor( width / 2 ), y + math.floor( width / 2 ), width, height )

instead of:

local rect = display.newRect( x, y, width, height )

Yes, 2a should work. 

Anchor points control the alignment of the polygon relative to the anchor. And the x,y you pass in defines the location of that anchor point. 

By default, the anchoring is (0.5,0.5) which means the polygon is centered about the anchor. 

When you change it to (0,0), you are saying the polygon isn’t centered but top-left aligned against the anchor point.

What we’re missing is a way to change the default anchoring. Then you could set it to 0,0 and you’d always get top-left alignment for the x,y values that you pass in.

From the way you worded that, it sounds like you plan to add in the ability to set a default anchor point. It would indeed be helpful :slight_smile:

Yes, 2a should work. 

Anchor points control the alignment of the polygon relative to the anchor. And the x,y you pass in defines the location of that anchor point. 

By default, the anchoring is (0.5,0.5) which means the polygon is centered about the anchor. 

When you change it to (0,0), you are saying the polygon isn’t centered but top-left aligned against the anchor point.

What we’re missing is a way to change the default anchoring. Then you could set it to 0,0 and you’d always get top-left alignment for the x,y values that you pass in.

From the way you worded that, it sounds like you plan to add in the ability to set a default anchor point. It would indeed be helpful :slight_smile: