Thread Tools Display Modes
06-05-14, 12:43 PM   #1
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Square and Triangle collision

As the title says, i would like to detect collision between the mentioned polygons. I can provide the coordinates of the objects in real time.
  Reply With Quote
06-05-14, 01:21 PM   #2
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
You might look into ray casting, not sure what you're looking to accomplish.
  Reply With Quote
06-05-14, 02:29 PM   #3
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Found this nasty little monster:

Lua Code:
  1. local triangle = {
  2.     [1] = {x = 0, y = 0},
  3.     [2] = {x = 1, y = 0},
  4.     [3] = {x = 0, y = 1}
  5. }
  6.  
  7. local function PointInPolynom(p, x, y)
  8.     local c = false
  9.     for i = 1, #p do
  10.         local j = i + 1
  11.         if j > #p then
  12.             j = 1
  13.         end
  14.         if ((p[i].y > y) ~= (p[j].y > y)) and (x < (p[j].x - p[i].x) * (y - p[i].y) / (p[j].y - p[i].y) + p[i].x) then
  15.             c = not c
  16.         end
  17.     end
  18.     return c
  19. end
  20.  
  21. print(PointInPolynom(triangle, 0.5, 0.5)) -- false
  22. print(PointInPolynom(triangle, 0.5, 0.49)) -- true

Should work with every polynom.

Edit: Some fixes.

Last edited by Resike : 06-05-14 at 04:00 PM.
  Reply With Quote
06-05-14, 02:46 PM   #4
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
Should probably be "polygon", but your code won't work since you call the function "PointInPolynom" and call it via "PointInPoly".

I'm sure it's just a typo from copying it into this.
  Reply With Quote
06-05-14, 02:46 PM   #5
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by semlar View Post
Should probably be "polygon", but your code won't work since you call the function "PointInPolynom" and call it via "PointInPoly".

I'm sure it's just a typo from copying it into this.
Yeah made some last minute changes.

Actually i'm not entierely sure how the hell it is working, but its superfast and clean. Working on it to convert it to square vs polynom.
  Reply With Quote
06-05-14, 03:16 PM   #6
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
I think it loops through all of the points of one shape and checks whether any of them fall within the boundaries of the other shape (might only work on convex polygons, but that won't matter with squares and triangles).
  Reply With Quote
06-05-14, 03:46 PM   #7
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Polynom in Polynom version:

Lua Code:
  1. local triangle = {
  2.     [1] = {x = 0, y = 0},
  3.     [2] = {x = 1, y = 0},
  4.     [3] = {x = 0, y = 1}
  5. }
  6.  
  7. local square = {
  8.     [1] = {x = -1, y = 0},
  9.     [2] = {x = -2, y = 0},
  10.     [3] = {x = -1, y = 1},
  11.     [4] = {x = -2, y = 1}
  12. }
  13.  
  14. local function PolynomInPolynom(p1, p2)
  15.     local c = { }
  16.     for i = 1, #p2 do
  17.         c[i] = false
  18.         for j = 1, #p1 do
  19.             local k = j + 1
  20.             if k > #p1 then
  21.                 k = 1
  22.             end
  23.             if ((p1[j].y > p2[i].y) ~= (p1[k].y > p2[i].y)) and (p2[i].x < (p1[k].x - p1[j].x) * (p2[i].y - p1[j].y) / (p1[k].y - p1[j].y) + p1[j].x) then
  24.                 c[i] = not c[i]
  25.             end
  26.         end
  27.         if c[i] then
  28.             return true
  29.         end
  30.     end
  31.     return false
  32. end
  33.  
  34. print(PolynomInPolynom(triangle, square)) -- false

It's not perfect, but it's pretty accurate.

Last edited by Resike : 06-05-14 at 04:04 PM.
  Reply With Quote
06-05-14, 04:09 PM   #8
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by semlar View Post
I think it loops through all of the points of one shape and checks whether any of them fall within the boundaries of the other shape (might only work on convex polygons, but that won't matter with squares and triangles).
I think it should work with concaves too, gotta test it.
  Reply With Quote
06-05-14, 08:12 PM   #9
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
Originally Posted by semlar View Post
I think it loops through all of the points of one shape and checks whether any of them fall within the boundaries of the other shape (might only work on convex polygons, but that won't matter with squares and triangles).
I would say it takes all lines from p1 and uses a horizontal line to test if and how many time the line intersects with a line of p2 (http://www.alienryderflex.com/polygon/). It can be used for convex and concave polygons.

There are other ways to test for polygon collisions. Like the separating axis approach (eg. http://www.codeproject.com/Articles/...sion-Detection). It's not that much more work, and it provides you with more options. Like using a velocity vector to detect collisions before they occure or to separate two overlapping polygons from each other. The drawback is that this approach works with convex polygons only. Concave polygons must be separated into triangles. Which is not that funny.

I've never used it. But it looks fascinating. If you're interested I could try to set up a lua implementation (for convex polygons *g*) next week.

Last edited by Duugu : 06-05-14 at 08:59 PM.
  Reply With Quote
06-06-14, 06:11 AM   #10
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by Duugu View Post
I would say it takes all lines from p1 and uses a horizontal line to test if and how many time the line intersects with a line of p2 (http://www.alienryderflex.com/polygon/). It can be used for convex and concave polygons.

There are other ways to test for polygon collisions. Like the separating axis approach (eg. http://www.codeproject.com/Articles/...sion-Detection). It's not that much more work, and it provides you with more options. Like using a velocity vector to detect collisions before they occure or to separate two overlapping polygons from each other. The drawback is that this approach works with convex polygons only. Concave polygons must be separated into triangles. Which is not that funny.

I've never used it. But it looks fascinating. If you're interested I could try to set up a lua implementation (for convex polygons *g*) next week.
Thats looks intresting too, could be used in the future, but for my current project i want to kill the player as soon as the collision occours, so this code is perfect for me.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Square and Triangle collision


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off