polygon collision

BlitzMax Forums/BlitzMax Beginners Area/polygon collision

Bremer(Posted 2006) [#1]
Can anyone point me in the right direction as far as something that will show me how to check if two polygons overlap. I have the transformed x and y values in two arrays and the polygons is drawn correctly, but I can't seem to figure out how to get the collision working. Or if someone would be as kind as to show me an easy to follow example of how its done using two sets of pixel coordinates, I would be extremely grateful. Its the only thing missing before I can release my sprite module for bmax.

The vertex data is held in two arrays of variable size depending on how many points the polygon consist of, but at a minimum of three.

Field vx:Int[]
Field vy:Int[]

The points are in clockwise order.

If someone helps me solve this, I will provide them with the sprite module free of charge. Its probably going to be sold at around 25 euros.

[edit] Let me know if more info is needed.


Oddball(Posted 2006) [#2]
I've just, about 5 mins ago, finished the poly2poly collisions for my game. I broke my polys up into triangles and then did tri2tri collision. Not sure if this is the best way but it works.


Dreamora(Posted 2006) [#3]
The fastest way might be SAT (projects to different axis' and if there is only 1 axis where they don't overlap, then they are not colliding. Axis = each side of the first or the second polygon, depending on which one has fewer sides)


Bremer(Posted 2006) [#4]


It will have to be able to check for collision between two polygon frames like those in the above image and similar shapes. I would like to avoid having to try and make triangles out of the frames.

I am not great at math myself, which is why I am asking for someone to help me out with this problem. I think that I have a good and useful sprite module going, but without polygon frame collision, its probably a lot less useful.

Info on the sprite module, for those interested in knowing more, can be found here:

http://zac-interactive.dk/tools/zsprite/zsprite.htm


Dreamora(Posted 2006) [#5]
For shape like that, SAT is definitely the easiest way to go. The cross ensures, that you only have 2 things to test:

xmin - xmax as well as ymin - ymax. This then would show that they intersect on y but not on x thus they don't intersect.

Would suggest you look for deeper informations on SAT and especially how to implement. Should be too hard on math, only a little vector and vector projection math ... thats game programming base knowledge, so no problem :-)


Bremer(Posted 2006) [#6]
I did some searching on "Separating Axis Theorem" and found this page, which seems to cover the basics of it.

http://www.harveycartel.org/metanet/tutorials/tutorialA.html

It could be useful to others, so I figured that I post it here. I am going to have a read through and see if I can understand it.

If someone have ideas and possibly example code on how this works then that would still be highly appreciated :)


Diablo(Posted 2006) [#7]
Got some BMax code for you:


The code is a quick ripp from This Good Chap

Edit: It dosent always work the way you might want it to but i'll leave you to experiment. The above link has got a link to download a .zip with some code in it. (cpp source)


SillyPutty(Posted 2006) [#8]
very nice


Bremer(Posted 2006) [#9]
Had a look at that tutorial I found, understood most of it. Then took another look at my code, and found out that I had been using a wrong array for one set of vertices. Once I corrected that, my current code actually worked. Go figure. Its bugs like that, which is hardest to track down, especially when you have staired yourself blind looking at the same code for hours on end.

Thanks for everyones input so far.