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
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.
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?
See accepted answer for solution
int cutterMask = 1 << 9;
Collider[] intersectedColliders = Physics.OverlapBox(node.bounds.center, node.bounds.extents, Quaternion.identity, cutterMask, QueryTriggerInteraction.UseGlobal);