Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Programming Talk / C++ / C# - line triangle intersect? (sparky?)

Author
Message
mm0zct
20
Years of Service
User Offline
Joined: 18th Nov 2003
Location: scotland-uk
Posted: 17th Jun 2005 08:07
i'm struggling with a function i need, i need to be able to find the point of intersection of a line and a triangle pased to the function as lots of coordinates but my 3d vector math is not good enough (esp in c++).
could someone please explain how to do this or, failing that, post a suitable function?
sparky could probably help since he wrote an intersect object dll, if you read this sparky please help

i attached the startings of my opengl c++ fps game so you can see why i need the function.

http://www.larinar.tk
AMD athlon 64 3000+, 512mb ddr400, abit kv8, 160gb hdd, gigabit lan, ati radeon 9800se 128mb.

Attachments

Login to view attachments
Paul Johnston
TGC Developer
21
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 18th Jun 2005 21:51 Edited at: 18th Jun 2005 21:51
Typically you need at least the co-ordinates of the three vertices of your polygon, and the co-ordinates of the start and end points of the line. Having the normal of the plane stored, and its distance from the origin can help, if not you'll have to calculate it. (a plane is just an infinite extension of our polygon across 3d space)

Here's the general method I use:

-Work out (if not already stored) the plane equation in which the triangle lies. Check if the end points of the line are on different sides of the plane.

-Work out the point at which the line crosses the plane (the one point on the line that lies in the plane)

-Then check if this point lies inside the triangle, there are many methods to do this.

The plane equation is of the form Ax + By + Cz + D = 0, where A,B,C and D are values you need to work out (I don't know how much 3d maths you already know so if any of this seems like another language let me know). It just so happens that the A,B and C parts are equal to the normal of the polygon. D can be worked out by rearranging the equation and using any vertex point as the x,y,z values.

The idea behind the plane equation is if you plug any point into the x, y and z parts of a particular plane equation it will equal 0 if that point is on the plane, <0 if it's on one side, or >0 if it's on the other. so if we put our two line points into this equation it will tell us if both points are on the same side or if they cross the plane. If they are on the same side, then there's no way the line can cross the polygon and we can stop. Otherwise we continue to check if the line crosses the part of the plane where the polygon lies.

Here's a snippet from the code I use to achieve this:



If you need to work out the normal, use the polygon vertex points to get two vectors that lie in the plane (v2-v1 and v3-v1), cross product these to get the normal and then normalise it.

Here's the pointInPoly function I use, it may not be the best, as I said there are many ways to do it, but it's fairly straight forward.



@everyone, anyone can use this code for whatever they want.
mm0zct
20
Years of Service
User Offline
Joined: 18th Nov 2003
Location: scotland-uk
Posted: 19th Jun 2005 06:53 Edited at: 19th Jun 2005 07:23
thank's for the help sparky but yes i do have a few questions lol, my 3d maths does not cover plain equations or cross products. i can successfully convert most of the first snippit to work with my data structure but the second snippit uses vector functions and classes that i can't make work with my data structure (this consits of arrays of vertex x,y,z and normal x,y,z (although the normals are for the vertex so they are not the same as the plain for many faces).

so.. how do i find d?
and how can i convert the second part to work with my data types or convert my data types to those used?

edit: a temporary alternative would just be to use the x and z of the player subs into the plain to see what y value i get out but i need to work out a,b,c and d.

this way will suffice for what i need.

edit2: are a,b and c the corresponding normals ? if so i can find d and use a flat shaded model to get the normals

http://www.larinar.tk
AMD athlon 64 3000+, 512mb ddr400, abit kv8, 160gb hdd, gigabit lan, ati radeon 9800se 128mb.
Paul Johnston
TGC Developer
21
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 19th Jun 2005 08:01
In which case you'll need to calculate the polygon normal (can't use vertex normals unless you rely on flat shading, I haven't tried this)

The code 'Vector v3(vert1,vert2)' just sets up a vector between two points, the same as doing



The cross product works like this:



is the same as doing



The cross product of two vectors gives the vector that's perpendicular to both vectors, so in the case of our polygon, the cross product of two edges gives us the normal of the polygon.

As for the plane equation, A,B and C correspond to the x,y and z of the polygon normal. D is an extra value that you need to work out.
To get the normal use the cross product with a vector from vertex1 to vertex2 and a vector from vertex1 to vertex3 (the two edges mentioned eariler)



that should give you everything you need
mm0zct
20
Years of Service
User Offline
Joined: 18th Nov 2003
Location: scotland-uk
Posted: 19th Jun 2005 08:55 Edited at: 21st Jun 2005 00:09
cheers that's great , thatnk you very much for helping and explaining everything. (now i'll hae a head start when we get to advanced higher vectors in maths lol)


this is the result from converting your code to mine, let me know if you see something wrong

anyone can use that if they can get it to work lol

http://www.larinar.tk
AMD athlon 64 3000+, 512mb ddr400, abit kv8, 160gb hdd, gigabit lan, ati radeon 9800se 128mb.
Paul Johnston
TGC Developer
21
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 19th Jun 2005 23:35
Looks good.

Glad I could help
mm0zct
20
Years of Service
User Offline
Joined: 18th Nov 2003
Location: scotland-uk
Posted: 21st Jun 2005 00:14 Edited at: 21st Jun 2005 04:30
it isn't working right, i'm getiing collisions detected where they shouldn't be and not where they should, the vertices are in ccw front ( .x style).

the collision areas seem to be projecting off from the model like a bow-tie shape (or a few of them)

edit: it's like some collision plains are not being limmited to the triangle and aothers are just not being detected

if i disable the checking if it is in the triangle then it finds every collision correctly (with all the hundreds of extras)

i disabled it checking which side it was on to allow back polygon collision.



changing the vertex order doesn't seem to help either

edit: i think i have narrowed it down to the point in poly function:


edit: it seems that there are a few triangles that it detects collisions with ignoring all the others and not limiting the triangle to it's boundaries correctly in certain directions.

http://www.larinar.tk
AMD athlon 64 3000+, 512mb ddr400, abit kv8, 160gb hdd, gigabit lan, ati radeon 9800se 128mb.
mm0zct
20
Years of Service
User Offline
Joined: 18th Nov 2003
Location: scotland-uk
Posted: 21st Jun 2005 23:45 Edited at: 22nd Jun 2005 02:08
ok i made a single triange level, red dots indicate an intersection detected


that is my altered PointIntTiangle code

raising a corner prevents any collision being detected


edit: i resorted to this slow method of checking whether the point is in the triangle:




it runs at about 445fps checking every frame on a 240tri map looks like it wasn't that much of a frame hit after all.

http://www.larinar.tk
AMD athlon 64 3000+, 512mb ddr400, abit kv8, 160gb hdd, gigabit lan, ati radeon 9800se 128mb.

Attachments

Login to view attachments
Paul Johnston
TGC Developer
21
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 22nd Jun 2005 07:56
I've just realised that by removing the cross product from it's own seperate method introduced a slight problem.



This won't work because in the fourth line v3x changes, then it's used again in the fifth line. Sorry I didn't spot this earlier.

Try something like this instead:



or something similar to get around the problem.

Also you'll want to avoid the sqrt function as much as possible in the low level functions, since they can start to build up when the number of polygons increases.
mm0zct
20
Years of Service
User Offline
Joined: 18th Nov 2003
Location: scotland-uk
Posted: 22nd Jun 2005 09:57
i am aware of the use of sqrt and acos being slow but as a temporary solution it was acceptable and fully within my understanding as i developed it (after being reminded that you can make 3 triangles inside the main one).

i was suspicious of your use of v3x etc multiple times overwriting it's value but i assumed it was supposed to but i'll fix it now you've pointed it out (fortunately i make a habit of commenting out unwanted functions rather than completely deleting them )

thanks again for the help

http://www.larinar.tk
AMD athlon 64 3000+, 512mb ddr400, abit kv8, 160gb hdd, gigabit lan, ati radeon 9800se 128mb.
mm0zct
20
Years of Service
User Offline
Joined: 18th Nov 2003
Location: scotland-uk
Posted: 23rd Jun 2005 06:22


i think that's what you meant but it still doesn't work right

http://www.larinar.tk
AMD athlon 64 3000+, 512mb ddr400, abit kv8, 160gb hdd, gigabit lan, ati radeon 9800se 128mb.
Paul Johnston
TGC Developer
21
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 23rd Jun 2005 07:50
Second side, you're missing the 2's

mm0zct
20
Years of Service
User Offline
Joined: 18th Nov 2003
Location: scotland-uk
Posted: 23rd Jun 2005 08:30
thanks again, you're a great help, definately going in the credits of any games i make use of this in

http://www.larinar.tk
AMD athlon 64 3000+, 512mb ddr400, abit kv8, 160gb hdd, gigabit lan, ati radeon 9800se 128mb.

Login to post a reply

Server time is: 2024-05-04 14:17:43
Your offset time is: 2024-05-04 14:17:43