How to find out if two triangles touch each other..
Started by RZ, Aug 19 2001 12:11 PM
9 replies to this topic
Sponsor:
#2 Staff Emeritus  Reputation: 2067
Posted 19 August 2001  12:24 PM
You''re trying to find out if a point (vertex) on one triangle is on the line (edge) of another triangle. Which is a case of finding the equation of the line and testing whether the point p in question satisfies it.
I can''t remember the equations in question, but someone else is sure to. (Sorry, I just moved).
I can''t remember the equations in question, but someone else is sure to. (Sorry, I just moved).
#4 Members  Reputation: 122
Posted 20 August 2001  02:58 AM
Let''s say point P is a vertex on the 1st triangle and has coords (xP, yP, zP). Let''s then also say that Q and R make up a side of the 2nd triangle and have coords (xQ, yQ, zQ) and (xR, yR, zR) respectively.
Point P is on Line QR (therefore making the triangles touch) iff
xP  xQ, yP  yQ, zP  zQ is in proportion to xR  xQ, yR  yQ, zR  zQ, iff
(You only need to test two of the three equalities in the thing above. )
Dragonus
Thought of the Moment
If F = ma, and Work = Fd, then does Work = mad?
Point P is on Line QR (therefore making the triangles touch) iff
xP  xQ, yP  yQ, zP  zQ is in proportion to xR  xQ, yR  yQ, zR  zQ, iff

(You only need to test two of the three equalities in the thing above. )
Dragonus
Thought of the Moment
If F = ma, and Work = Fd, then does Work = mad?
#5 Anonymous Poster_Anonymous Poster_* Guests  Reputation:
Posted 20 August 2001  03:18 AM
In 3d...
I''ve looked around on the web and in my math books but I have not found any good solution..
On way to find if a point lies on a line(3D) is this:
(x  x0) / Vx = (y – y0) / Vy = (z  z0) / Vz
(x0,y0,z0) is a point on the line.
(Vx,Vy,Vz) is an vector on the line.
Then you put a point (x,y,z) in the equation to see if it is on the line.
The problem is that this only works if Vx,Vy,Vz != 0
so that equation doesn’t work in my case…
Sorry my english isn’t that good…hope you understand anyway..
I''ve looked around on the web and in my math books but I have not found any good solution..
On way to find if a point lies on a line(3D) is this:
(x  x0) / Vx = (y – y0) / Vy = (z  z0) / Vz
(x0,y0,z0) is a point on the line.
(Vx,Vy,Vz) is an vector on the line.
Then you put a point (x,y,z) in the equation to see if it is on the line.
The problem is that this only works if Vx,Vy,Vz != 0
so that equation doesn’t work in my case…
Sorry my english isn’t that good…hope you understand anyway..
#7 Members  Reputation: 347
Posted 20 August 2001  03:45 AM
It''s mathematically tricky to test for exact properties such as a point lying exactly on a line because of limitations of mathematical precision. It''s better to test for closeness within a tolerance, e.g. testing whether a corner of one triangle is within 1/1000 of the edge of the other. I.e. work out the distances and see if any are small enough, so you need a way to work out the smallest distance from the corners of one triangle to the edges of another.
You can do this by looking at each corner and edge in turn. E.g. take one corner of one triangle, P, and an edge of the other, AB. You want the distance from P to AB.
AB can be describd using a line equation,
X = A + t * (B  A)
Where A and B are the ends of the line and t is a scalar which goes from 0 to 1 as the line is traversed from A to B.
From this and the point P the point on AB nearest to P is given by
t = ((P  A) . (B  A)) / ((B  A) . (B  A))
Where ''.'' is the inner product.
The result depends on the value of t, but because t is restricted to a range of 0 to 1:
If t < 0 the nearest point on the line is when t = 0, at A, so the distance is the distance from A to P
If t > 1 the nearest point on the line is when t = 1, at B, so the distance is the distance from B to P
Otherwise the nearest point is between A and B. Work this point out by putting t into
X = A + t * (B  A)
And the distance is the distance from X to P.
Then work this out for all corners and all the edges of the triangles: a lot more calculations but easy enough to work out in for () loops. Each time work out the distance, and if it''s shorter than the shortest worked out distance remember it. When you have done all edges and corners the triangles ''touch'' if the distance is short enough.
This works in 2D and 3D, even for triangles (or other polygons) not parallel to each other. It is very calculation intensive so is not recommended for large groups of triangles/polygons, unless other logic takes care of making sure only adjacent polys are tested.
You can do this by looking at each corner and edge in turn. E.g. take one corner of one triangle, P, and an edge of the other, AB. You want the distance from P to AB.
AB can be describd using a line equation,
X = A + t * (B  A)
Where A and B are the ends of the line and t is a scalar which goes from 0 to 1 as the line is traversed from A to B.
From this and the point P the point on AB nearest to P is given by
t = ((P  A) . (B  A)) / ((B  A) . (B  A))
Where ''.'' is the inner product.
The result depends on the value of t, but because t is restricted to a range of 0 to 1:
If t < 0 the nearest point on the line is when t = 0, at A, so the distance is the distance from A to P
If t > 1 the nearest point on the line is when t = 1, at B, so the distance is the distance from B to P
Otherwise the nearest point is between A and B. Work this point out by putting t into
X = A + t * (B  A)
And the distance is the distance from X to P.
Then work this out for all corners and all the edges of the triangles: a lot more calculations but easy enough to work out in for () loops. Each time work out the distance, and if it''s shorter than the shortest worked out distance remember it. When you have done all edges and corners the triangles ''touch'' if the distance is short enough.
This works in 2D and 3D, even for triangles (or other polygons) not parallel to each other. It is very calculation intensive so is not recommended for large groups of triangles/polygons, unless other logic takes care of making sure only adjacent polys are tested.
#8 Members  Reputation: 122
Posted 20 August 2001  04:18 AM
quote:
Original post by RZ
Your equation is the same as mine.. but as I said it will not work if the edge vectors x, y or z = 0
There must be another way
Don''t worry about that ^_~
Anyways, about the zero thing, for mine it will work... most of the way. We only have a problem if any of the x, y, or zcoordinates of Q equals exactly the corresponding coordinate of R. If that''s the case (say, xQ == xR) then xP == xQ (== xR) must be true for it to work. Then you have to test the final euqation to make sure the ratios work.
If you have two of the denominators at 0 (say, xQ == xR && yQ == yR) then test xP == xQ (== xR) and yP == yQ (== yR) and then the zcoordinate is trivial.
If you have three denominators at 0, then we don''t really care because the Q == R in 3space. Then, P is guaranteed to be colinear with Q & R because, as well all know, a line is determined by two points...
As far as the tolerance, granted I was doing this hypothetically. Knowing FPU''s and whatnot, it won''t be 100% accurate (e.g., 3 / 2 = 1.49999999 for "stupid" computers). The cheap way to fix that is to (depending upon the order of magnitude of the numbers) multiply them by a power of 10, cast them as int''s, and then test them.
Dragonus
Thought of the Moment
If F = ma, and Work = Fd, then does Work = mad?
#10 Members  Reputation: 122
Posted 20 August 2001  08:07 AM
Look at the triangles from two axis (sp?) in 2D, disreguarding the third axis.
if for both of them a trangle created by the midpoints of the lines between corrosponding corners of the two triangles has any of its edges intersecting an edge from one of the two triangles, then the two triangles intersect.
if for both of them a trangle created by the midpoints of the lines between corrosponding corners of the two triangles has any of its edges intersecting an edge from one of the two triangles, then the two triangles intersect.