Help me debug TriTriIntersection on meshes

This is pretty complicted and you probaly need to recreate this in unity to be able to help me.
I am doing this because there is no other way to check for concave mesh intersection.
So currently if statement in line 56 is always false and isFree is always true. To make it easier put the TriTriOverlap in your scripts folder, create a gameobject with a mesh and put the intersectiontest in it. Thanks for your help!

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class intersectiontest : MonoBehaviour {

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
        bool isFree = true;
        Collider[] hitColliders = Physics.OverlapSphere(this.transform.position, this.collider.bounds.extents.magnitude);
        List<GameObject> hitGameObjects = new List<GameObject>();
        foreach(Collider currentCollider in hitColliders){
            hitGameObjects.Add(currentCollider.gameObject);
        }
        hitGameObjects.RemoveAt(hitGameObjects.FindIndex(item => item.name == this.gameObject.name));    // Removing in GroundObject from list of close objects
        foreach(GameObject currentGameObject in hitGameObjects){
            if(this.collider.bounds.Intersects(currentGameObject.collider.bounds) && currentGameObject.collider.GetType() != typeof(CharacterController)){
                int[] thisTriangles = this.transform.GetComponent<MeshFilter>().mesh.triangles;
                int[] otherTriangles = currentGameObject.GetComponent<MeshFilter>().mesh.triangles;
                Vector3[] thisVertices = this.transform.GetComponent<MeshFilter>().mesh.vertices;
                Vector3[] otherVertices = currentGameObject.transform.GetComponent<MeshFilter>().mesh.vertices;
                int adjustedTriangleCount = thisTriangles.Length / 3;
                int otherAdjustedTriangleCount = otherTriangles.Length / 3;
                for(int i = 1; i < adjustedTriangleCount; i ++){    // For every triangle in this...
                    if(isFree == false){
                    //    break;
                    }
                    for(int ib = 1; ib < otherAdjustedTriangleCount; ib ++){    // For every triangle in other object...
                        int[] aPoints = new int[3];
                        aPoints[0] = thisTriangles[(i - 1)*3 + 0];
                        aPoints[1] = thisTriangles[(i - 1)*3 + 1];
                        aPoints[2] = thisTriangles[(i - 1)*3 + 2];
                        Vector3 a1 = this.transform.TransformPoint(thisVertices[aPoints[0]]);
                        Vector3 a2 = this.transform.TransformPoint(thisVertices[aPoints[1]]);
                        Vector3 a3 = this.transform.TransformPoint(thisVertices[aPoints[2]]);
                        int[] bPoints = new int[3];
                        bPoints[0] = otherTriangles[(ib - 1)*3 + 0];
                        bPoints[1] = otherTriangles[(ib - 1)*3 + 1];
                        bPoints[2] = otherTriangles[(ib - 1)*3 + 2];
                        Vector3 b1 = currentGameObject.transform.TransformPoint(otherVertices[aPoints[0]]);
                        Vector3 b2 = currentGameObject.transform.TransformPoint(otherVertices[aPoints[1]]);
                        Vector3 b3 = currentGameObject.transform.TransformPoint(otherVertices[aPoints[2]]);
                    /*    Debug.DrawRay(a1, new Vector3(0, 0.1F, 0), Color.green);
                        Debug.DrawRay(a2, new Vector3(0, 0.1F, 0), Color.green);
                        Debug.DrawRay(a3, new Vector3(0, 0.1F, 0), Color.green);*/
                        Debug.DrawRay(b1, new Vector3(0, 0.1F, 0), Color.red);
                        Debug.DrawRay(b2, new Vector3(0, 0.1F, 0), Color.red);
                        Debug.DrawRay(b3, new Vector3(0, 0.1F, 0), Color.red);
                        if(TriTriOverlap.TriTriIntersect(a1, a2, a3, b1, b2, b3)){
                            isFree = false;
                        }
                    }
                }
            }
        }
        Debug.Log (isFree);
    }
}

Here’s the TriTriOverlap (TriTriIntersect obviously checks for TriTriIntersection)

lines 47,48,49
You still using aPoints, not bPoints

also:

  • aPoints and bPoints doint really need to be array of ints, just 3 individual ints would work fine

  • You dont particularly need to recalculate a1,2,3 each iteration, thats always the same iteration of i, not ib

  • i and ib should just start at 0, then you dont need to subtract 1 every time you use them

  • Shouldnt need to add this.gameobject to hitGameObjects, just to then remove it again immediatly. In the loop Lines 19,20,21 just check it currentCollider == this.collider

It works now thanks. :slight_smile: