Modify lines and polygons

Is there a way to modify/animate points of (multi segment) lines and polygons?

I’m writing a charts system and wondered if there’s a simple way to modify/update these easily after their initial creation.

Hi Michael.

Im new round here, but since there is no answer I did a bit of digging.

Once you create a DisplayObject such as a polygon, you have an object that contains what is essentially a table of points.

However, the Polygon object, and the ShapeObject from which it inherits, lacks any method or property that allows you to update that table.

The docs say: (my bold)

Near the beginning of this guide, we mentioned that display objects behave “essentially” like Lua tables. An exception to this rule is that you cannot set the metatables of display objects. This is due to the nature of our C++/Lua integration in this particular case.

This means that you must delete & nil the object , then create a new one to replace it.

Thanks for trying to help but you’re wrong on that.

I’m not sure you’re used to Lua/metatables?

Metatables is the Lua mechanism to define behaviour in fallback situations, f.i. when accessing the table with keys that do not exist and in addition you can change behaviour for a set of operators like “+” etc. - it’s basically a dynamic way to implement polymorphism as in C++ etc. (but with many different options due to it’s runtime dynamic nature). So what you can’t do is, set the metatables on display objects as this would literally remove all functionality Corona offers for any given object.

But we can set all kinds of properties of objects (it would not be practical to need to create and destory an object just to change it’s coordinates etc.) including vertices/uv’s/indices of mesh displayObjects, so I wondered if there’s something to modify polygons too.

You can certainly modify polygons after creation by using transforms on the individual points.   Access them as follows

object.path.x1, object.path.y1 = 10, 5

repeat for as many points as your polygon has or those you wish to move after creation.

For animation options you can transition them too.  More info here

I have animated bar graphs in my games.

Besides that this would be the probably worst API design I could imagine (require a unique string key to access a numbered vertex in an array) this was the first way I tested as I remembered that I’ve read it somewhere (I guess it was for rect fills) it does not work.

Have you used and changed polygons this way or probably, like me, just guessed it works like it does with rectangles?

As you (probably) know I created a 3D polygonal landscape.  But performance was too slow so I switched to quadrilateral deformed rects and performance more than doubled.

It was a while ago I was experimenting with deforming polys so I may of preformed the deform in the vertices prior to creation - I am not sure but I probably was.

this is off-topic from the original post, but the docs are wrong on this topic:  you CAN set the metatable of a Corona display object, though you must do so carefully else you’ll wipe out the internal functionality (it is necessary to “chain” a failed lookup in the new metatable to a fallback lookup in a stored copy of the original metatable)

.  this allows you to add new functionality while preserving the original.  yes, i’ve done it, and it works, but almost always it’s more work than it’s worth, as there are far easier “prototype”-based approaches (cloning) to object-oriented design that could cover ~>90% of use cases.

back on topic:  no you can’t modify the vertices once created

[edit: remainder re path deformation withdrawn, as it was based on same wrong/untested assumptions]

Hi Michael.

Im new round here, but since there is no answer I did a bit of digging.

Once you create a DisplayObject such as a polygon, you have an object that contains what is essentially a table of points.

However, the Polygon object, and the ShapeObject from which it inherits, lacks any method or property that allows you to update that table.

The docs say: (my bold)

Near the beginning of this guide, we mentioned that display objects behave “essentially” like Lua tables. An exception to this rule is that you cannot set the metatables of display objects. This is due to the nature of our C++/Lua integration in this particular case.

This means that you must delete & nil the object , then create a new one to replace it.

Thanks for trying to help but you’re wrong on that.

I’m not sure you’re used to Lua/metatables?

Metatables is the Lua mechanism to define behaviour in fallback situations, f.i. when accessing the table with keys that do not exist and in addition you can change behaviour for a set of operators like “+” etc. - it’s basically a dynamic way to implement polymorphism as in C++ etc. (but with many different options due to it’s runtime dynamic nature). So what you can’t do is, set the metatables on display objects as this would literally remove all functionality Corona offers for any given object.

But we can set all kinds of properties of objects (it would not be practical to need to create and destory an object just to change it’s coordinates etc.) including vertices/uv’s/indices of mesh displayObjects, so I wondered if there’s something to modify polygons too.

You can certainly modify polygons after creation by using transforms on the individual points.   Access them as follows

object.path.x1, object.path.y1 = 10, 5

repeat for as many points as your polygon has or those you wish to move after creation.

For animation options you can transition them too.  More info here

I have animated bar graphs in my games.

Besides that this would be the probably worst API design I could imagine (require a unique string key to access a numbered vertex in an array) this was the first way I tested as I remembered that I’ve read it somewhere (I guess it was for rect fills) it does not work.

Have you used and changed polygons this way or probably, like me, just guessed it works like it does with rectangles?

As you (probably) know I created a 3D polygonal landscape.  But performance was too slow so I switched to quadrilateral deformed rects and performance more than doubled.

It was a while ago I was experimenting with deforming polys so I may of preformed the deform in the vertices prior to creation - I am not sure but I probably was.

this is off-topic from the original post, but the docs are wrong on this topic:  you CAN set the metatable of a Corona display object, though you must do so carefully else you’ll wipe out the internal functionality (it is necessary to “chain” a failed lookup in the new metatable to a fallback lookup in a stored copy of the original metatable)

.  this allows you to add new functionality while preserving the original.  yes, i’ve done it, and it works, but almost always it’s more work than it’s worth, as there are far easier “prototype”-based approaches (cloning) to object-oriented design that could cover ~>90% of use cases.

back on topic:  no you can’t modify the vertices once created

[edit: remainder re path deformation withdrawn, as it was based on same wrong/untested assumptions]