About Monkey 2 › Forums › Monkey 2 Projects › module PNG2Polygon
Tagged: moorNeighbor, Peucker, PNG outline, PNG2Polygon, PolyCollision
This topic contains 6 replies, has 5 voices, and was last updated by
Ethernaut
7 months, 1 week ago.
-
AuthorPosts
-
September 7, 2016 at 6:36 am #3728
Reserved topic for module
September 7, 2016 at 7:59 am #3733Sounds interesting!
September 7, 2016 at 10:40 am #3737I ‘uploaded’ the module PNG2Polygon and made a screenshot to make it easy to understand what is does. (check this image first, and then read below)
I use it in a situation like this.
– a number of high quality transparent PNG files that need a check for a collision and/or user mouseFirst I use a PNG2Polygon inside a editor to convert all the PNG files to the smallest simplified x and y coordinates and save them in a json file. something like this:
circle.png : [x,y,x,y,x,y]
star.png : [x,y,x,y]Then where I need the collision in game I load the coordinates (verts) once.
And use it something like this:Monkey12345678Local CollisionType:Int = PolyCollision(verts,Mouse.X,Mouse.Y)If CollisionType=0 Then' no collisionElseif CollisionType=1 Then' line collisionElseif CollisionType=2 Then' point insideEndAs you can see, the simplified coordinates are way less x,y points then the total png2polygon.outline points.
So there is less CPU/GPU needed to check for a collision.There are situations that simplifying not work in combination with a line collision.
For example the figure below the circle.
In that situation the PolyCollision() can’t detect line collision, but it can detect point inside collision
If you want a perfect line collision using that figure, then you need the full png2polygon.outlineThis script uses
https://en.wikipedia.org/wiki/Moore_neighborhood
https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithmInside the module there is a [test] directory with examples.
Attachments:
September 12, 2016 at 7:09 am #3896The module is now online inside Ted2.
September 12, 2016 at 7:29 am #3898I will check this out. This seems like the kind of solution I’m looking for in constructing polygons for in game collision. Neat.
September 8, 2018 at 11:23 pm #15399I totally missed this, but it’s awesome!
Is there a way to get some sort of data I could construct 3D shapes from?
I just want a flat “3D mesh”/surface using the simplified outline this module produces.September 9, 2018 at 7:00 am #15402Hezkore, would this work for you? Needs Import/Using mojo3d. The “length” parameter is how deep the extrusion is. It extrudes in both directions (positive/negative Z axis), but this can be easily changed.
(Untested outside the project I made this for).
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748Function LineExtrude:Mesh( points:Vertex2f[], length:Float, closedCurve:Bool = False, flip:Bool = False )Local front:= New Stack<Vertex3f>Local back:= New Stack<Vertex3f>Local indices := New Stack<UInt>Local vertices := New Stack<Vertex3f>Local totalVerts := points.LengthLocal sides:= totalVertsIf Not closedCurve sides -= 1If totalVerts = 2 Then sides = 1 'Prevents two overlapping planes if line only has two verticesLocal n := 0.0For Local v := Eachin pointsLocal u := n/ Float(sides)front.Push( New Vertex3f(v.position.X, v.position.Y, -length/2, u, 0.0) )back.Push( New Vertex3f(v.position.X, v.position.Y, +length/2, u, 1.0) )n += 1.0NextFor Local i0 := 0 Until sidesLocal i1 := i0 + 1If i1 > totalVerts - 1 Then i1 = 0indices.Push( UInt(i0) )indices.Push( UInt(i0 + totalVerts) )indices.Push( UInt(i1) )indices.Push( UInt(i0 + totalVerts) )indices.Push( UInt(i1 + totalVerts) )indices.Push( UInt(i1) )NextFor Local v := Eachin frontvertices.Push( v )NextFor Local v := Eachin backvertices.Push( v )NextLocal mesh := New Mesh( vertices.ToArray(), indices.ToArray() )mesh.UpdateTangents()mesh.UpdateNormals()If flip Then mesh.FlipTriangles()mesh.Compact()Return meshEndI’m using it to generate 3d colliders for a “2.5D” game.
[edit] Just realized this doesn’t generate front and back caps, only the side walls. It would need a good mesh triangulation algorithm to do that well.
-
AuthorPosts
You must be logged in to reply to this topic.
