Page 1 of 1
Complex Polygon Merging with HardonCollider Problems [Fixed]
Posted: Sun Jan 15, 2012 10:24 pm
by xXOdysseusXx
Hello. I'm attempting to merge all my connecting tiles into one to optomize performance and make things easier.
Being a beginner, I've had to do a lot of hard thinking and I got somewhere nice. But now I've hit a wall that I have absolutely no idea how to solve.
... The blocks refuse to merge...
I'm guessing it's gonna take somebody with a good amount of experience with HC to figure this one out.
Anyways, here's my code. The stuff from the beginning is taken from a tile map
Code: Select all
for y=1, map_h do
for x=1, map_w do
if map[y+map_y][x+map_x] == 1 then -- if it's a wall tile
px = (x*tile_w)+map_offset_x
py = (y*tile_h)+map_offset_y
table.insert(wallTilesP1, polygon(px,py,px + 34,py,px,py + 34, px + 34, py, px + 34, py +34, px, py + 34)) -- Now we have all our block polygons, let us combine the colliding ones together
end
end
end
for i = 1, #wallTilesP1, 1 do
for z = 1, #wallTilesP1, 1 do
if wallTilesP1[i] ~= wallTilesP1[z] then
debug1 = "Performing colliding shape test"
if wallTilesP1[i]:mergedWith(wallTilesP1[z]) ~= nil then
debug1 = "WE HAS COLLIDING SHAPES!!"
table.insert(wallTilesP1, wallTilesP1[i]:mergedWith(wallTilesP1[z]))
table.remove(wallTilesP1,z)
table.remove(wallTilesP1,i)
end
end
end
end
Apparently the shapes don't share an edge. All though you can clearly see it does in the .love.
Re: Complex Polygon Merging with HardonCollider Problems
Posted: Mon Jan 16, 2012 4:08 pm
by xXOdysseusXx
Here is the .love file.
I should also mention that the blocks are spaced 32 pixels apart, but being of a 33 pixel diameter, the blocks share an edge.
Re: Complex Polygon Merging with HardonCollider Problems
Posted: Mon Jan 16, 2012 4:18 pm
by kikito
I have never used HardonCollider so I can't solve your programming problem. But I can help you on getting better responses: provide a .love file exemplifying the problem you are having. As mentioned on the
Posting Guidelines, this will help much more than bumping (which I think is not very well considered on this forum).
Re: Complex Polygon Merging with HardonCollider Problems
Posted: Tue Jan 17, 2012 12:31 pm
by vrld
Printing the polygons reveals this:
Code: Select all
P1[1] 384, 256 418, 256 418, 256 418, 290 384, 290
P1[2] 416, 256 450, 256 450, 256 450, 290 416, 290
P1[3] 448, 256 482, 256 482, 256 482, 290 448, 290
P1[4] 384, 288 418, 288 418, 288 418, 322 384, 322
P1[5] 416, 288 450, 288 450, 288 450, 322 416, 322
P1[6] 448, 288 482, 288 482, 288 482, 322 448, 322
You can see that they dont actually have shared edges -- the positions are off by 2 pixels, e.g. (418, 256)->(418, 290) vs. (416, 290)->(416, 256) in P1[2]. Also note that your polygon creation is erroneous: You insert one point multiple times.
Instead of
Code: Select all
table.insert(wallTilesP1, polygon(px,py,px + 34,py,px,py + 34, px + 34, py, px + 34, py +34, px, py + 34))
it should be
Code: Select all
table.insert(wallTilesP1, polygon(px,py, px+34,py, px+34,py+34, px,py+34))
It would probably be easier (and faster) to search for blobs of neighboring tiles of the same kind to create the polygon shapes.
Re: Complex Polygon Merging with HardonCollider Problems
Posted: Tue Jan 17, 2012 11:19 pm
by xXOdysseusXx
vrld wrote:Printing the polygons reveals this:
Code: Select all
P1[1] 384, 256 418, 256 418, 256 418, 290 384, 290
P1[2] 416, 256 450, 256 450, 256 450, 290 416, 290
P1[3] 448, 256 482, 256 482, 256 482, 290 448, 290
P1[4] 384, 288 418, 288 418, 288 418, 322 384, 322
P1[5] 416, 288 450, 288 450, 288 450, 322 416, 322
P1[6] 448, 288 482, 288 482, 288 482, 322 448, 322
You can see that they dont actually have shared edges -- the positions are off by 2 pixels, e.g. (418, 256)->(418, 290) vs. (416, 290)->(416, 256) in P1[2]. Also note that your polygon creation is erroneous: You insert one point multiple times.
Instead of
Code: Select all
table.insert(wallTilesP1, polygon(px,py,px + 34,py,px,py + 34, px + 34, py, px + 34, py +34, px, py + 34))
it should be
Code: Select all
table.insert(wallTilesP1, polygon(px,py, px+34,py, px+34,py+34, px,py+34))
It would probably be easier (and faster) to search for blobs of neighboring tiles of the same kind to create the polygon shapes.
Yeah I noticed that and fixed that. The blocks were spaced 32 apart, so I set them to a length and width of 32 and now they merge and all... Only just not perfectly. Some blocks are left out and some still dont' merge. I'm going to send the revised version.
1. How can I optomize the code to where it merges perfectly?
2. How did you get the elements in the table to print? I wanted to do that.
Re: Complex Polygon Merging with HardonCollider Problems
Posted: Sat Jan 21, 2012 5:14 am
by xXOdysseusXx
I hate to bump up my post but I really need help optomizing the code.
Re: Complex Polygon Merging with HardonCollider Problems
Posted: Sat Jan 21, 2012 9:21 am
by MarekkPie
I haven't been following the problem, but I can answer the print out question. Looking through the source code, it looks like the vertices are stored in self._polygon.vertices. So just looping through your polygon table, and print() out what you wish to see. There might be a way to look at this using debug.debug, but I haven't messed with that much yet.
Re: Complex Polygon Merging with HardonCollider Problems
Posted: Sat Jan 21, 2012 2:34 pm
by xXOdysseusXx
debug.debug? Is this a library?
Re: Complex Polygon Merging with HardonCollider Problems
Posted: Sat Jan 21, 2012 2:51 pm
by MarekkPie
Re: Complex Polygon Merging with HardonCollider Problems
Posted: Sat Jan 21, 2012 4:26 pm
by vrld
debug.debug() just opens a lua promt at the console where you can put arbitrary lua code.
To print the coordinates, you can use
polygon:unpack():
You can also use
vector pretty printing on polygon.vertices like so:
Code: Select all
print("Vertices:", unpack(polygon.vertices))
As for the merging: That's all that you will get using polygon:mergedWith(other). As the documentation states:
Create a merged polygon of two polygons if, and only if the two polygons share one edge
However, these two polygons don't share an edge:
- nosharededge.png (208 Bytes) Viewed 5513 times
The right edge of the left rectangle is shorter than the right edge of the left rectangle.
To make the algorithm capable of merging polygons with partially overlapping edges would make it way more complicated (and I am to lazy to add that). Besides, for collision detection concave polygons (which you would get when merging this) are internally split into convex polygons anyway.