Cannot read object.path points

I am trying to get the vertices of a rectangle, but object.path is not returning any values. The docs say, “Shape objects have a path property that exposes properties of the path. The first corner is at the top-left and they go around in counter-clockwise order.”

That implies one can not only set the vertices, which DOES work, but read them. No?

local r = display.newRect(0,0, 200,100) r.x = 100 r.y = 50 r.rotation = 20 print(r.path.x1, r.path.x2, r.path.x3, r.path.x4) print(r.path.y1, r.path.y2, r.path.y3, r.path.y4)

The path property is used to create 2.5D graphics.

To get the corners, use contentBounds property.

local r = display.newRect(0,0, 200,100) r.x = 100 r.y = 50 r.rotation = 20 print(r.contentBounds.xMax) print(r.contentBounds.xMin) print(r.contentBounds.yMax) print(r.contentBounds.yMin)

Close, no cigar. The contentBounds of a rotated rect doesn’t let you immediately discover a vertex. For example, the top point has an x value in-between max and min.

Hi @mimetic,

I’m not sure why this isn’t working for you. I can both read and adjust the path vertices of a newRect. Do you have V1 mode set or something?

Brent

Nope, no V1 mode. Using Corona Version 2014.2362 (2014.7.8)

There is the entire code of main.lua

local r = display.newRect(0,0, 200,100) r.x = 100 r.y = 50 r.rotation = 20 print(r.path.x1, r.path.x2, r.path.x3, r.path.x4) print(r.path.y1, r.path.y2, r.path.y3, r.path.y4)

Here is the output:

2014-07-28 16:01:04.323 Corona Simulator[38837:507] 0 0 0 0 2014-07-28 16:01:04.324 Corona Simulator[38837:507] 0 0 0 0  

Here is the config:

-- config.lua application = { content = { width = 768, height = 1024, scale = "letterbox", antialias = false, xalign = "center", yalign = "center", imageSuffix = { ["@0-5"] = 0.5, -- for smaller devices ["@2x"] = 2, -- for iPad 3 } } }

Hi @mimetic,

This is the expected result… you haven’t adjusted the path points, so they’re all 0. I thought you meant that the values were coming back nil, but they’re 0, so that’s normal. What values are you seeking to get?

Take care,

Brent

I was expected to reach the x,y coordinates for each value. So, I was expecting that 

r.path.x1

would tell me the x coordinate of that corner point (in local or content, not sure which), i.e. 100 (before the rotation) See, if I knew x,y of the corner, I would not have to try to calculate the corner point after rotating. Very handy.

Hi @mimetic,

I think you can find some way to get this using “:localToContent”. Have you experimented with that?

http://docs.coronalabs.com/daily/api/type/DisplayObject/localToContent.html

Not sure how that would help if path.x/y report zero?

perhaps diy? fe:

local rect = display.newRect(display.contentCenterX, display.contentCenterY, 200, 100) -- record its dimensions (relative to local origin) rect.hw = 100 rect.hh = 50 local function rotate2d(x,y,theta)   local cs = math.cos(theta)   local sn = math.sin(theta)   return x \* cs - y \* sn, x \* sn + y \* cs end local function enterFrame(event)   -- animate   rect.rotation = (rect.rotation+1)%360   -- retrieve values   local hw = rect.hw   local hh = rect.hh   local rot = math.rad(rect.rotation)   -- rotated local coordinates (counter-clockwise from upper-right)   local x1,y1 = rotate2d( hw, -hh, rot)   local x2,y2 = rotate2d(-hw, -hh, rot)   local x3,y3 = rotate2d(-hw,  hh, rot)   local x4,y4 = rotate2d( hw,  hh, rot)   -- or if you want screen coords:   local sx1,sy1 = rect.x+x1, rect.y+y1   -- etc for 2,3,4   print(x1,y1,sx1,sy1) end Runtime:addEventListener("enterFrame", enterFrame)

Thank you!

The path property is used to create 2.5D graphics.

To get the corners, use contentBounds property.

local r = display.newRect(0,0, 200,100) r.x = 100 r.y = 50 r.rotation = 20 print(r.contentBounds.xMax) print(r.contentBounds.xMin) print(r.contentBounds.yMax) print(r.contentBounds.yMin)

Close, no cigar. The contentBounds of a rotated rect doesn’t let you immediately discover a vertex. For example, the top point has an x value in-between max and min.

Hi @mimetic,

I’m not sure why this isn’t working for you. I can both read and adjust the path vertices of a newRect. Do you have V1 mode set or something?

Brent

Nope, no V1 mode. Using Corona Version 2014.2362 (2014.7.8)

There is the entire code of main.lua

local r = display.newRect(0,0, 200,100) r.x = 100 r.y = 50 r.rotation = 20 print(r.path.x1, r.path.x2, r.path.x3, r.path.x4) print(r.path.y1, r.path.y2, r.path.y3, r.path.y4)

Here is the output:

2014-07-28 16:01:04.323 Corona Simulator[38837:507] 0 0 0 0 2014-07-28 16:01:04.324 Corona Simulator[38837:507] 0 0 0 0  

Here is the config:

-- config.lua application = { content = { width = 768, height = 1024, scale = "letterbox", antialias = false, xalign = "center", yalign = "center", imageSuffix = { ["@0-5"] = 0.5, -- for smaller devices ["@2x"] = 2, -- for iPad 3 } } }

Hi @mimetic,

This is the expected result… you haven’t adjusted the path points, so they’re all 0. I thought you meant that the values were coming back nil, but they’re 0, so that’s normal. What values are you seeking to get?

Take care,

Brent

I was expected to reach the x,y coordinates for each value. So, I was expecting that 

r.path.x1

would tell me the x coordinate of that corner point (in local or content, not sure which), i.e. 100 (before the rotation) See, if I knew x,y of the corner, I would not have to try to calculate the corner point after rotating. Very handy.

Hi @mimetic,

I think you can find some way to get this using “:localToContent”. Have you experimented with that?

http://docs.coronalabs.com/daily/api/type/DisplayObject/localToContent.html

Not sure how that would help if path.x/y report zero?

perhaps diy? fe:

local rect = display.newRect(display.contentCenterX, display.contentCenterY, 200, 100) -- record its dimensions (relative to local origin) rect.hw = 100 rect.hh = 50 local function rotate2d(x,y,theta)   local cs = math.cos(theta)   local sn = math.sin(theta)   return x \* cs - y \* sn, x \* sn + y \* cs end local function enterFrame(event)   -- animate   rect.rotation = (rect.rotation+1)%360   -- retrieve values   local hw = rect.hw   local hh = rect.hh   local rot = math.rad(rect.rotation)   -- rotated local coordinates (counter-clockwise from upper-right)   local x1,y1 = rotate2d( hw, -hh, rot)   local x2,y2 = rotate2d(-hw, -hh, rot)   local x3,y3 = rotate2d(-hw,  hh, rot)   local x4,y4 = rotate2d( hw,  hh, rot)   -- or if you want screen coords:   local sx1,sy1 = rect.x+x1, rect.y+y1   -- etc for 2,3,4   print(x1,y1,sx1,sy1) end Runtime:addEventListener("enterFrame", enterFrame)