简体   繁体   中英

Determining where / if line intersects triangle in 3D space

Problem

So basically I have a bunch of triangles that are defined by 3 points:

X0, Y0, Z0
X1, Y1, Z1
X2, Y2, Z2

I have two points that form a line:

Xl0, Yl0, Zl0
Xl1, Yl1, Zl1

I have a point in space (P) defined as:

Xp0, Yp0, Zp0

在此处输入图片说明

I want to know first, how can I determine an equation of the line that is parallel with the original line, but goes through my new point P?

Secondly, using that new line, how can I determine if and where on my triangle it intersects?

I would like to try and create a function that takes the inputs that I have presented and outputs a point which is the intersection of the triangle and a boolean value if it did intersect.

Now I would like to do this through c# or VB .NET but the actual solution really is just math based so answers that are expressed as math and not code would definitely help too.

What I have tried

I thought that calculating the new line would simply be the new point and a second point where

Xnew = Xl0 + (Xp0 - Xl1)
Ynew = Yl0 + (Yp0 - Yp1)
Znew = Zl0 + (Zp0 - Zp1)

then plotting the new line from (Xnew, Ynew, Znew) to (Xp0, Yp0, Zp0) should be a parallel line to my first one, however in doing so they are not parallel which means my calculation is wrong.

Any help would be greatly appreciated!

Update

After trying what was suggested below, the line that is drawn doesn't seem to be correct yet:

在此处输入图片说明

The camera's location to the origin is what creates the first line. The second line that we determined is the line from our cursor location, to the new calculated point which should be parallel to the line that goes from the origin to the camera, which as you see is not the case. Am I wrong in my thought process or am I not coding it correctly?

My code doing the calculation like suggested below:

GL.Begin(PrimitiveType.Lines)
    GL.Color3(Color.Orange)

    Dim XL0, YL0, ZL0 As Single
    XL0 = 0
    YL0 = 0
    ZL0 = 0

    'origin is at 0,0,0

    Dim XL1, YL1, ZL1 As Single
    XL1 = camx
    YL1 = camy
    ZL1 = camz

    Dim XS, YS, ZS As Single

    XS = XL1 - XL0
    YS = YL1 - YL0
    ZS = ZL1 - ZL0

    Dim length As Single = Sqrt(XS * XS + YS * YS + ZS * ZS)

    XS /= length
    YS /= length
    ZS /= length

    Dim XPN, YPN, ZPN As Single

    XPN = returnvec.X - XS * length
    YPN = returnvec.Y - YS * length
    ZPN = returnvec.Z - ZS * length


    GL.Vertex3(XPN, YPN, ZPN)
    GL.Vertex3(returnvec.X, returnvec.Y, returnvec.Z)
    GL.End()

在此处输入图片说明

Trial 1

在此处输入图片说明

Trial 2

I want to know first, how can I determine an equation of the line that is parallel with the original line, but goes through my new point P?

To get the slope of your line:

XS = XL1 - XL0;
YS = YL1 - YL0;
ZS = ZL1 - ZL0;
// Find length of new slope
length = Math.Sqrt(XS * XS + YS * YS + ZS * ZS);
// Normalize the new slope
XS /= length;
YS /= length;
ZS /= length;

Now XS is a Vector3 which basically represents the slope of your line, you can do the same with another line and you can test by comparing that slope to this one, and reverse slope to this one as well.

Now if you want to create a new line with this slope, and find a point N from a point P and a length we do:

XPN = XPP + XS * length;
YPN = YPP + YS * length;
ZPN = ZPP + ZS * length;

I drew the lines and they are indeed parallel. I think what is happening is that since I am viewing the line as a perspective view, it appears to converge inward. That means that my math for creating a parallel line is correct which is technically my question. The parallel line however is not what I need to do ray casting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM