## Complex Polygon Merging with HardonCollider Problems [Fixed]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
xXOdysseusXx
Prole
Posts: 15
Joined: Sun Nov 13, 2011 4:56 pm

### Complex Polygon Merging with HardonCollider Problems [Fixed]

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.
Last edited by xXOdysseusXx on Sun Jan 22, 2012 4:12 pm, edited 5 times in total.
xXOdysseusXx
Prole
Posts: 15
Joined: Sun Nov 13, 2011 4:56 pm

### Re: Complex Polygon Merging with HardonCollider Problems

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.
Attachments
Test.love
As you can see it performs the check but I guess none of the shapes are merging.
Last edited by xXOdysseusXx on Mon Jan 16, 2012 5:55 pm, edited 3 times in total.
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: Complex Polygon Merging with HardonCollider Problems

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).
When I write def I mean function.
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

### Re: Complex Polygon Merging with HardonCollider Problems

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.

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.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
xXOdysseusXx
Prole
Posts: 15
Joined: Sun Nov 13, 2011 4:56 pm

### Re: Complex Polygon Merging with HardonCollider Problems

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.

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.
Attachments
Test.love
...
xXOdysseusXx
Prole
Posts: 15
Joined: Sun Nov 13, 2011 4:56 pm

### Re: Complex Polygon Merging with HardonCollider Problems

I hate to bump up my post but I really need help optomizing the code.
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

### Re: Complex Polygon Merging with HardonCollider Problems

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.
xXOdysseusXx
Prole
Posts: 15
Joined: Sun Nov 13, 2011 4:56 pm

### Re: Complex Polygon Merging with HardonCollider Problems

debug.debug? Is this a library?
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

### Re: Complex Polygon Merging with HardonCollider Problems

vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

### Re: Complex Polygon Merging with HardonCollider Problems

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():

Code: Select all

print("Vertices:", p: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 2855 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.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine

### Who is online

Users browsing this forum: Google [Bot], josip and 7 guests