# How can I find if a line intersects a triangle?

**URL:** <https://forums.unrealengine.com/t/how-can-i-find-if-a-line-intersects-a-triangle/367719>\
**Category:** Community & Industry Discussion\
**Tags:** math, unreal-engine, vector, geometry, question\
**Created:** [August 20, 2016, 1:35pm UTC](https://forums.unrealengine.com/t/how-can-i-find-if-a-line-intersects-a-triangle/367719 "2016-08-20T13:35:09Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thumper](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Thumper](https://forums.unrealengine.com/u/Thumper)\
**Post date:** [August 20, 2016, 1:35pm UTC](https://forums.unrealengine.com/t/how-can-i-find-if-a-line-intersects-a-triangle/367719/1 "2016-08-20T13:35:09Z")

</div>

I need to figure out if a line intersects a triangle. I’ve looked through the FMath section and I cannot find anything that works. I want to be able to pick a face from my container and use the three FVectors it references, then compare that with a ray (2 FVectors) and see if this ray is inside the triangle (where it penetrates the triangle)

---

<div class="post-metadata">

**Author:** ![ScottSpadea](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scottspadea/32/1194682_2.png) [@ScottSpadea](https://forums.unrealengine.com/u/ScottSpadea)\
**Post date:** [August 20, 2016, 1:58pm UTC](https://forums.unrealengine.com/t/how-can-i-find-if-a-line-intersects-a-triangle/367719/2 "2016-08-20T13:58:20Z")

</div>

have you tried line traces?

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

---

<div class="post-metadata">

**Author:** ![Thumper](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Thumper](https://forums.unrealengine.com/u/Thumper)\
**Post date:** [August 20, 2016, 2:09pm UTC](https://forums.unrealengine.com/t/how-can-i-find-if-a-line-intersects-a-triangle/367719/3 "2016-08-20T14:09:22Z")

</div>

I’m trying to figure out which face index on my procedural mesh (component) is being hit or specifically my character is standing on. So my ray hits the procedural mesh, but I don’t think there is a way to get the face index from the rayhit so I’m using the container from where the proc mesh was built from. This houses all the vert locations. What I want to do is use three FVectors (the triangle being tested) and the ray (my characters location) and see if the ray is inside the triangle. This will let me know which face index is being hit. My faces are organized into subdivision levels so the lowest subD is tested first (because it has very few triangles) then it steps up the subdivision locally to quickly find which higher resolution face is being hit. Problem is I can’t find a function to tell me if my ray is intersecting a triangle I provide…

---

<div class="post-metadata">

**Author:** ![Thumper](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Thumper](https://forums.unrealengine.com/u/Thumper)\
**Post date:** [August 20, 2016, 2:19pm UTC](https://forums.unrealengine.com/t/how-can-i-find-if-a-line-intersects-a-triangle/367719/4 "2016-08-20T14:19:39Z")

</div>

I would like to be able to use three FVectors to define a triangle, and 2 FVectors to define a ray. And check to see if the ray intersects the triangle.

---

<div class="post-metadata">

**Author:** ![ScottSpadea](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scottspadea/32/1194682_2.png) [@ScottSpadea](https://forums.unrealengine.com/u/ScottSpadea)\
**Post date:** [August 20, 2016, 2:25pm UTC](https://forums.unrealengine.com/t/how-can-i-find-if-a-line-intersects-a-triangle/367719/5 "2016-08-20T14:25:01Z")

</div>

FHitResult has a FaceIndex

---

<div class="post-metadata">

**Author:** ![Thumper](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Thumper](https://forums.unrealengine.com/u/Thumper)\
**Post date:** [August 20, 2016, 2:33pm UTC](https://forums.unrealengine.com/t/how-can-i-find-if-a-line-intersects-a-triangle/367719/6 "2016-08-20T14:33:19Z")

</div>

Yeah but that isn’t the actual face index. [Link](https://answers.unrealengine.com/questions/50830/does-anything-set-fhitresultfaceindex.html) Or maybe I’m reading this wrong, but it sounds like if you’re using a triangle mesh for the search query? Does that mean the ray is a triangle mesh or the triangle it is hitting?

---

<div class="post-metadata">

**Author:** ![ScottSpadea](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scottspadea/32/1194682_2.png) [@ScottSpadea](https://forums.unrealengine.com/u/ScottSpadea)\
**Post date:** [August 20, 2016, 2:44pm UTC](https://forums.unrealengine.com/t/how-can-i-find-if-a-line-intersects-a-triangle/367719/7 "2016-08-20T14:44:37Z")

</div>

> **[FCollisionQueryParams](https://docs.unrealengine.com/4.27/en-US/API/Runtime/Engine/FCollisionQueryParams/)**
>
> Structure that defines parameters passed into collision function

FCollisionQueryParams has a member called bReturnFaceIndex, which probably defaults to false for performance, because that granularity is usually not needed.

so try this in your tracing:

```
FCollisionQueryParams TraceParams(blah, blah, blah)
TraceParams.bTraceComplex = true;
TraceParams.bReturnFaceIndex = true;
```

---

<div class="post-metadata">

**Author:** ![Thumper](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Thumper](https://forums.unrealengine.com/u/Thumper)\
**Post date:** [August 20, 2016, 3:46pm UTC](https://forums.unrealengine.com/t/how-can-i-find-if-a-line-intersects-a-triangle/367719/8 "2016-08-20T15:46:51Z")

</div>

Hmm when using LineTraceSingle\_New my trace hits the procedural mesh, but when I use LineTraceSingleByChannel it doesn’t. The problem here is that I can turn on TraceParams.bReturnFaceIndex = true; using the single by channel, but I can’t using the linetracesingle\_new. There’s no way to give trace params to the \_new version, but there is with the by channel version, but it doesn’t hit the procedural mesh. Even if I turn on bTraceComplex (because my procedural mesh uses that)

---

<div class="post-metadata">

**Author:** ![Thumper](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Thumper](https://forums.unrealengine.com/u/Thumper)\
**Post date:** [August 20, 2016, 4:25pm UTC](https://forums.unrealengine.com/t/how-can-i-find-if-a-line-intersects-a-triangle/367719/9 "2016-08-20T16:25:16Z")

</div>

Okay so that worked. Problem is I was hitting my character with the LineTraceSingleByChannel. I wasn’t ignoring that actor in the trace. It now returns the face index from the trace! This saved me a headache. Thanks a bunch!

---

<div class="post-metadata">

**Author:** ![CppCode](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CppCode](https://forums.unrealengine.com/u/CppCode)\
**Post date:** [October 28, 2016, 7:41am UTC](https://forums.unrealengine.com/t/how-can-i-find-if-a-line-intersects-a-triangle/367719/10 "2016-10-28T07:41:57Z")

</div>

Hi, I have meeting the same trouble. after read you work. I want to konw how to get the material of the static mesh by triangle index?

---

<div class="post-metadata">

**Author:** ![Thumper](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Thumper](https://forums.unrealengine.com/u/Thumper)\
**Post date:** [October 28, 2016, 12:32pm UTC](https://forums.unrealengine.com/t/how-can-i-find-if-a-line-intersects-a-triangle/367719/11 "2016-10-28T12:32:29Z")

</div>

This sounds like what you’re looking for. [LINK](https://answers.unrealengine.com/questions/110892/line-trace-how-to-get-hit-material.html). You need to set bReturnPhysicalMaterial to true on the FCollisionQueryParams, then you should be able to get the material directly from the FHitResult.
