[Solved] Looking for a way to find a position within an area

Hello everyone!

I’m having trouble finding a solution for a problem.

What I’m looking for is the position within an area. My area is a triangles, so I’ve got 3 positions. I also have a 4th position and a direction. I want to find the position withing the area based on the 4th position and the direction. Unfortunately I cannot use a raycast, which would make it rather easy. I have to calculate it, but I can’t figure out the right solution for it.

Does anyone know something that would help me?
I attached an image for better understanding what I’m looking for.

Any help is appreciated.

Thanks,
Marcus

Hey Marcus,

So you are having a line crossing a triangle and you’re looking for the center of the two points where the line crosses the triangles sides? 2D or 3D?

That’s just calculating the two intersection points and then half of the resulting vector.

Excactly, the line is crossing the triangle somewhere, it’s not always the same position, and I need to find the position where the line “hits” the triangle. It’s in 3D. That is why I need to use the direction to calulate the positions, I guess.

I tried something where I calculate the point on each of the 3 lines and then I took the 3 positions on the lines and divided them by 3. Didn’t work exactly as I need it.

Here is the code for this:

Vector3 GetPointOnLine (Vector3 startPos, Vector3 endPos, Vector3 checkPos)
    {
        Vector3 v = endPos - startPos;
        Vector3 w = checkPos - startPos;

        float dot1 = Vector3.Dot (w, v);
        float dot2 = Vector3.Dot (v, v);
        float b = dot1 / dot2;

        return startPos + b * v;
    }

What this code does is taking a line (with start and end position) and calulates the position in the line if there would be a straight line hitting the first line at a 90° angle.

Ah. If it is 3D you’re probably looking for a line-plane intersection code?
There are some very helpful examples over here.

Line-plane intersections sounds like something I’m looking for.
I’ll have a look at the examples.

Thanks!

It worked and is exactly what I needed.

I used the code from the “public static bool LinePlaneIntersection” example on http://wiki.unity3d.com/index.php/3d_Math_functions and adjusted it to my case.

Here is a short video of how it looks:

Thanks for the help,
Marcus

Hey Marcus, great to hear. Looks promising. :slight_smile:

There’s also a built in ray/plane intersection in unity, Plane.Raycast.

Oh, wow. Good to know! Thanks sir.

I can’t use the collider of the mesh in this case. I use it to update the position of some objects when terraforming the terrain. If I assign the mesh as meshcollider the game freezes for a moment, so I need to calculate it.

Also I’m not using a plane. But good to know that there is a Plane.Raycast. Might come in handy at some point.

Plane.Raycast doesn’t need a collider, it uses just a plane.

Oh ok, didn’t know that. If you’re not using a plane than the other solution works pretty well. I use the triangles from a mesh after I adjustes the vertices and it does the job for me.