Checking if a Vector is within a Triangle, regardless of it's Y

I’ve searched and searched and I can’t seem to find any plausible way to do this. I’ve tried pretty much all equations on stackflow, I’ve tried substituting the Y for the Z, but nothing seems to work well.

I’m looking to check if a part is within three other vectors, a triangle, I don’t want the Y-Axis to be taken in to account.

Where purple is true, and red would be false. ie; within/not on the triangle.

Here is a studio example.

Ignores the Y Axis

Outside of the Triangle

3 Likes

if the y axis doesn’t need to be taken in consideration then this problem becomes rather 2D. So using a normal 2d point in triangle function should work with just substituting the y for z . For example, using barycentric coordinates (for efficiency) and some math described all over this StackOverFlow thread, something like this should work:

local function PointInTriangle(p, p0, p1,p2)
      local s = p0.Z * p2.X - p0.X * p2.Z + (p2.Z - p0.Z) * p.X + (p0.X - p2.X) * p.Z
      local t = p0.X * p1.Z - p0.Z * p1.X + (p0.Z - p1.Z) * p.X + (p1.X - p0.X) * p.Z
        if (s < 0) ~= (t < 0)  then
        return false
      end
      local Area = -p1.Z * p2.X + p0.Z * (p2.X - p1.X) + p0.X * (p1.Z - p2.Z) + p1.X * p2.Z
      if Area < 0  then
      return (s <= 0 and s + t >= Area)
	else
    return(s >= 0 and s + t <= Area)
  end
end

Or do i not understand your question?

1 Like

Works wonders! Thanks for taking the time to write this out.

I kept seeing responses based on barycentric coordinates, I guess I need to go out and research them more now!

no problem, but don’t give me all the credit!..this time…, just some simple conversion from a C# function to a Lua one was all. Anyways, if you want to there were some other really good/interesting answers on that stackoverflow thread i would recommend checking out.

1 Like