1

When a 3D object lies within the bounds of a 2D polygon(or a 3D polygon that extends infinitely on the Y-coordinate), a message should be triggered.

There are a number of 2D polygons that form a loop, with 2 co-ordinates of each polygon being shared with the previous polygon in the sequence.

I have tried using Rect() but am having trouble defining each rectangle with 4 co-ordinates available rather than width and height:

3D object to be detected = this Each 2D rectangle defined as (A = previousA, B = previousB, C = currentA, D = currentB)

void Update ()
{
    Vector3 previousA = new Vector3(0, 0, 0);
    Vector3 previousB = new Vector3(0, 0, 0);
    Vector3 currentA;
    Vector3 currentB;
    Rect rect1;

    foreach (GameObject item in CheckpointManager.GetComponent<Checkpoints>().checklines)
    {
        currentA = item.transform.GetChild(0).transform.position;
        currentB = item.transform.GetChild(1).transform.position;

        if (previousA == new Vector3(0, 0, 0) || previousB == new Vector3(0, 0, 0))
        {

        }
        else
        {
            rect1 = new Rect(
                previousA.x, 
                previousA.y, 
                Mathf.Sqrt( (previousA.x - currentB.x) * (previousA.x - currentB.x) ),
                Mathf.Sqrt( (previousA.y - previousB.y) * (previousA.y - previousB.y))
                );
            if (rect1.Contains(this.transform.position))
            {
                Debug.Log("intersection confirmed in ploygon: " + rect1);
            }
        }

        previousA = currentA;
        previousB = currentB;

    }

}

Edit 1:

The sample of code above is attactched to the 3D white cube object.

Here is a picture better demonstrating what I mean:

3D Cube in surrounding 2D polygons

Here the camera is high on the Y-axis, with the Z-axis going up and down (north/south), and the X-axis is going left and right (east/west).

Each of the red and green icons mark a single point. Two adjacent red icons and two adjacent green icons mark the four points of a 2D polygon (connected by Unity gizmo lines). The final pair of points created will be connected to the first pair of points that were created. I have created a tool in the editor that can create pairs of points, so that there will always be a loop of pairs of points, and therefore always a loop of 2D polygons with four points.

The 3D White Cube needs to be aware of when it is currently contained within any of the 2D polygons, no matter how many there are on the screen, and no matter their rotation. It also needs to be aware of which polygon it is currently residing in, if any.

However, the 3D cube can be anywhere along the Y-axis (close to the camera or potentially infinitely far away).

Ripi2
  • 7,031
  • 1
  • 17
  • 33
  • 2
    The question doesn't make sense to me. Do you mean the projection of a 3D object onto some 2D space? A (nondegenerate) 3D object is never contained within a 2D boundary. – Alexis Olson Mar 13 '18 at 18:55
  • I also didn't understand the question. Also is it not possible to just use a 3D collider? – Hristo Mar 14 '18 at 09:09
  • add sketch image ... as most likely the meaning of the question got lost in translation or bad wording ... also what do you mean by extending in y axis? did you mean 3D surface which perpendicular cut is you 2D boundary instead (something like prism)? – Spektre Mar 14 '18 at 11:42
  • I have updated the post to hopefully clarify things. I am not sure about using a 3D collider as it would have to have infinite scale on the Y-axis, or I would have to give the scale an arbitrarily large value unless testing is performed to find the max scale needed. – Denver Thomas Mar 14 '18 at 15:45
  • so you r polygons are in xz plane (y=0) and you want to know if perpendicular projection of 3D object lies within it. What does it mean contain (fully inside, touching ?) – Spektre Mar 15 '18 at 07:34
  • Can't you just ignore the `Y` coordinates of your 3D objects, or does this have to work depending on the camera position (like an UI overlay)? – Manfred Radlwimmer Mar 15 '18 at 08:01
  • Simplesy way might be to create an invisible 2D-Object with a collider and update it's position along the "floor" with a script attached to the cube. That way you should get collision events on the invisible helper object that you can use. – Manfred Radlwimmer Mar 15 '18 at 08:05

1 Answers1

0

if you define your polygons in a specific winding rule and your QUADS are always convex than you can do this:

point inside

Now ignore the y value of your 3D mesh and use y=0 instead for all points of your object. If point is inside than direction to all 2D polygon veticies match the winding rule of your 2D polygon. If at least one does not match than your point is outside. So check all points of your object and if all in then object is fully contained.

How to detect winding for point P ?

in your case simply by cross product of P(i+1)-P(i) and P-P(i). That will give you normal directed either up or down (in y axis) so just check y coordinate of the results if it is positive or negative.

N0 = cross( P1-P0, P-P0 )
N1 = cross( P2-P1, P-P1 )
N2 = cross( P3-P2, P-P2 )
N3 = cross( P0-P3, P-P3 )

if ((sign(N0.y)==sign(N1.y))
  &&(sign(N2.y)==sign(N3.y))
  &&(sign(N0.y)==sign(N2.y))) point_is_inside;
else point_is_outside;

So check all points of your object and if all lies inside object is fully inside. If just some object is intersecting ... The sign it self depends on winding and coordinate system properties.

Here (on the bottom) is how to compute the 3D cross product:

This approach works for any number of vertexes per your 2D polygon just must be convex with specific winding rule. If your 2D polygon is not convex than you need to divide it to convex triangles or use hit test instead.

Spektre
  • 49,595
  • 11
  • 110
  • 380