0
\$\begingroup\$

I have a problem with collisions in Unity and I am unsure how to fix it.

I have an octree with a depth of 2, and each node in the tree has a Bounds object attached to it for collisions. I am trying to see which nodes have been intersected by a specific object however it is giving me unexpected results.

The colliding object is a flat triangle that I have constructed myself which is acting as a cutting plane and I have attached a MeshCollider component to the GameObject.

When there is an intersection with a leaf node in the octree, the node is outlined in yellow and if there is no intersection, it is outlined in purple.

Before rotation

enter image description here

After rotation In this image I rotated the triangle a small amount on the Y axis and apparently Unity found a collision even though the objects are not even touching at all. enter image description here

I am drawing the octree nodes using Gizmos.DrawWireCube, providing the center and size of the cube which are used to create the Bounds object of the node. Due to this it seems that the Bounds object for the nodes are correct, however the collider for the triangle may be incorrect.

When I test this with another 3D object such as a cube, it works perfectly, however not with the object I need it to work with. How can I fix this issue?


Collision checking code:

       public bool intersects(Bounds otherBounds)
        {
//bounds refers to the node's Bounds object
//otherBounds is the one I'm checking against, in this case the triangle's bounds from the MeshCollider
            if (otherBounds.Intersects(bounds))
            {
                return true;
            }
            return false;
        }

Octree traversal:

 if (node.intersects(triangleCollider.bounds))
        {

            if (node.isLeaf())
            {
                node.colour = Color.yellow;
            }

            if (node.hasChildren)
            {
                for (int i = 0; i < node.children.Length; i++)
                {
                    findIntersections(node.children[i]);
                }
            }
        }

It seems that perhaps a quad is being wrapped around the triangle as can be seen in the image below. This image has a triangle which is intersecting the top half of the octree, however the top left octant should have no collision.

How can I create a custom MeshCollider which I can specify the vertices manually?

enter image description here


See accepted answer for solution

int cutterMask = 1 << 9;
Collider[] intersectedColliders = Physics.OverlapBox(node.bounds.center, node.bounds.extents, Quaternion.identity, cutterMask, QueryTriggerInteraction.UseGlobal);

enter image description here

\$\endgroup\$
0
\$\begingroup\$

"It seems that perhaps a quad is being wrapped around the triangle"

You're not far off. All of your intersection methods are checking for intersections between Bounds structs. These represent axis-aligned bounding boxes, a set of min & max extents along each axis: precisely a box drawn around the object. They don't have the fidelity of an arbitrarily oriented plane or shaped polygon.

So it's not that your collider is being formed incorrectly. I'd be willing to bet it's correct as-is. You just need to make sure you're testing against that MeshCollider and not only its bounds.

To test a given box against the actual colliders in your scene, you can use Physics.OverlapBox or its non-allocating version.

I haven't found a simple built-in way to check for a box intersection with a single provided collider, but if you only need this for planes or triangles (or other primitives) we can do this analytically for each case with a few math formulas.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.