login about faq

To prove you're not a spammer, email newuser.lgqa@gmail.com with the subject "Account Request" to request an account.

Hey Lockergnome,

I'm currently doing some experimental stuff with GLUT and C++ but I'm having quite a problem understanding how to calculate the point of intersect in a triangle when I've got 2 3D points to draw a line between.

Here's an example of what I need to calculate (the X Y and Z of value p)

Line triangle intersection

I've got this image from http://sarvanz.blogspot.nl/2012/03/probing-using-ray-casting-in-opengl.html but I just don't understand how I can get the intersecting point X Y and Z so I can determine what triangle I clicked on my screen.

I can already determine the near and far point of the mouse position and I have acces to the triangles (it's a simple calculation that is already done in the draw loop)

Can someone explain to me how I can calculate the position of p?

asked Feb 26 '13 at 11:17

nitrocrime's gravatar image


closed Feb 28 '13 at 06:10

The question has been closed for the following reason "I've found the answer, posted it below." by nitrocrime Feb 28 '13 at 06:10

You need two givens to come up with the third like in the Pythagorean theorem. You start from a point of origin "0, 0, 0" and calculate the cartesian coordinate. You need a point of origin to contrast the vectors with.

answered Feb 26 '13 at 21:13

ClosetFuturist's gravatar image


I've checked out your method but I didn't really find any use in it unfortunately (and yes this comment is quite late as I've already fixed this problem) Still thanks for trying to help me :)

(Mar 16 '13 at 21:36) nitrocrime nitrocrime's gravatar image

I've found the answer!

The main problem with my code was that my cross product of 2 3D vectors was completely wrong.
The current math is the following with the value of P being stored in Vector3D I, if the method return 1 it was a successful operation, 0 means not within the triangle and 2 means that it's parallel to the triangle.

Given values:
Ray R = P0 (near point), P1 (far point)
Triangle T = V0, V1, V2 which are A B and C respectively 
All values are Vector3D objects

Vector3D Normal = T.V1 - T.V0;
Normal.cross(T.V2 - T.V0); //value stored in Normal
Normal.Normalize(); //divide all values by the length of the Vector (sqrt(x^2+y^2+z^2))

Vector3D d1 = R.P0 - T.V0;
Vector3D d2 = R.P1 - T.V0;

float dist1 = d1.dot(Normal);
float dist2 = d2.dot(Normal);

if(dist1 * dist2 >= 0.0f)
    return 0;//no cross

    return 2;//parallel

*I = R.P0 + (R.P1-R.P0) * (-dist1/(dist2-dist1)); //Here the exact position is calculated

//check if the position in within the triangle and thus a valid value
Vector3D u = T.V1 - T.V0;
Vector3D v = T.V2 - T.V0;

// is I inside T?
float    uu, uv, vv, wu, wv, D;
uu = u.dot(u);
uv = u.dot(v);
vv = v.dot(v);
Vector3D w = *I - T.V0;
wu = w.dot(u);
wv = w.dot(v);
D = uv * uv - uu * vv;

// get and test parametric coords
float s, t;
s = (uv * wv - vv * wu) / D;
if (s < 0.0 || s > 1.0)         // I is outside T
    return 0;
t = (uv * wu - uu * wv) / D;
if (t < 0.0 || (s + t) > 1.0)  // I is outside T
    return 0;

return 1;                       // I is in T

The Vector3D math are calculated as such:

float Vector3D::dot(Vector3D v)
    return (x*v.x)+(y*v.y)+(z*v.z);
void Vector3D::cross(Vector3D v)
    Vector3D o = Vector3D(x,y,z);

    x = o.y*v.z - o.z*v.y;
    y = o.z*v.x - o.x*v.z;
    z = o.x*v.y - o.y*v.x;
void Vector3D::normalize()
    float l = length();

    x = x/l;
    y = y/l;
    z = z/l;
float Vector3D::length() // |vector| = length
    return sqrtf(powf(x, 2)+powf(y,2)+powf(z,2));

answered Feb 28 '13 at 06:08

nitrocrime's gravatar image


edited Feb 28 '13 at 06:11

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: Feb 26 '13 at 11:17

Seen: 888 times

Last updated: Mar 16 '13 at 21:36