Point inside polygon collision detection
Moderator: Coders of Rage
- cypher1554R
- Chaos Rift Demigod
- Posts: 1124
- Joined: Sun Jun 22, 2008 5:06 pm
Point inside polygon collision detection
I see a lot of ppl coming with this problem. I decided to show in a short (and hopefully friendly) app. how it can be done using vector's cross product checking. The deal is: The difference vectors of every next 2 vertices that form a triangle with the point, have to give a cross product that has the same sign for z coordinate of the product result throughout the whole polygon loop.
Don't ask me about the last sentence :D
Just look at the example (if you're interested): mr.SanJin - point inside convex polygon collision detection(B).rar
I also included the release app. It's basically a pentagon that changes attributes on collision detection with the cursor.
This can be useful for user interface.
Note: It's written in OpenGL using the GLUT library. don't forget you need their .dll-s to have it working.
Don't ask me about the last sentence :D
Just look at the example (if you're interested): mr.SanJin - point inside convex polygon collision detection(B).rar
I also included the release app. It's basically a pentagon that changes attributes on collision detection with the cursor.
This can be useful for user interface.
Note: It's written in OpenGL using the GLUT library. don't forget you need their .dll-s to have it working.
Last edited by cypher1554R on Thu Nov 27, 2008 3:27 am, edited 2 times in total.
- cypher1554R
- Chaos Rift Demigod
- Posts: 1124
- Joined: Sun Jun 22, 2008 5:06 pm
Re: Point inside polygon collision detection
Just wondering if this even works on other computers.. Anyone tried it yet?
edit: and no.. there's no bug or virus in it.. I grew over that.
edit: and no.. there's no bug or virus in it.. I grew over that.
Re: Point inside polygon collision detection
cypher1554R wrote:Just wondering if this even works on other computers.. Anyone tried it yet?
edit: and no.. there's no bug or virus in it.. I grew over that.
what do you mean "works on other computers"?
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- cypher1554R
- Chaos Rift Demigod
- Posts: 1124
- Joined: Sun Jun 22, 2008 5:06 pm
Re: Point inside polygon collision detection
If it executes on your computer without crashing / not starting etc..avansc wrote:what do you mean "works on other computers"?
I know that it shouldn't, but I can only try it on mine.
I'm not asking anyone to try it. I was just wondering if anyone did, and how it went. And also if it was useful (code), or was it too dirty to take out things from it.
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Point inside polygon collision detection
Code: Select all
PtInCPoly.exe - Entry Point Not Found
The procedure entry point __glutInitWithExit could not be located in the dynamic link library glut32.dll.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- cypher1554R
- Chaos Rift Demigod
- Posts: 1124
- Joined: Sun Jun 22, 2008 5:06 pm
Re: Point inside polygon collision detection
Okay, I edited the link. It's there now.
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Point inside polygon collision detection
Hehe, I'm working on someone else's computer right now, but as soon as I'm finished I'll check out again.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
Re: Point inside polygon collision detection
i looked at that code, but i dont like it for some or other reason.
i think its mainly that i feel its worthless for having a 3 component vector if you are only using 2 components.
and there are computations that are inefficient.
here is what i came up with. its simpler, less code, and ~2X the speed. with the same precision.
note that Vec2 is a struct with components float x, y.
here are the execution times for ITERATIONS = 10000000;
algo 1 = 15984 ms
algo 2 = 8219 ms
i think its mainly that i feel its worthless for having a 3 component vector if you are only using 2 components.
and there are computations that are inefficient.
here is what i came up with. its simpler, less code, and ~2X the speed. with the same precision.
Code: Select all
int side_of_line(Vec2 &a, Vec2 &b, Vec2 &p)
{
return float(p.y - a.y) * float(b.x - a.x) - float(p.x - a.x) * float(b.y - a.y);
}
bool point_inside_poly(std::vector<Vec2> &poly, Vec2 &point)
{
for(int i = 0; i < poly.size()-1; i++)
{
if(side_of_line(poly[i], poly[i+1], point) > 0)
return false;
}
if(side_of_line(poly[poly.size()-1], poly[0], point) > 0)
return false;
return true;
}
here are the execution times for ITERATIONS = 10000000;
algo 1 = 15984 ms
algo 2 = 8219 ms
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- cypher1554R
- Chaos Rift Demigod
- Posts: 1124
- Joined: Sun Jun 22, 2008 5:06 pm
Re: Point inside polygon collision detection
Interesting.. Although I wasn't really going for the speed. I was lazy and just copied vector computation code from my other project.
A good resume for someone looking for speed.
EDIT: BTW, does anyone know any good, easy method for point inside any poly? Meaning - it also works with concave polygons.
A good resume for someone looking for speed.
EDIT: BTW, does anyone know any good, easy method for point inside any poly? Meaning - it also works with concave polygons.