// Preconditions: asteroid and collider are not null // Postconditions: Returns whether the asteroid collided with the ship. /// <summary> /// Returns whether the object collided with a transform /// </summary> /// <param name="tester">test object to collide</param> /// <param name="collider">desired object to check collision</param> /// <returns>true if the asteroid collides with the desired object, false if otherwise</returns> public bool ProcessCollision(Transform tester, Transform collider) { if (tester == null || collider == null) { return(false); } Vector3 delta = tester.transform.position - collider.position; SceneNode cur = collider.GetComponent <SceneNode>(); foreach (Transform child in collider) // Traverse the collider tree { SceneNode cn = child.GetComponent <SceneNode>(); //Debug.Log("Traversing: " + collider.gameObject.name); if (cn != null) { foreach (NodePrimitive geom in cn.PrimitiveList) // check if the node contains meshes { AllMesh mesh = geom.GetComponent <AllMesh>(); bool isContactMesh = false; if (mesh != null) // check collision with each vertex in the mesh { for (int i = 0; i < mesh.GetVertexLocations().Count; i++) { isContactMesh = ProcessCollision(tester, mesh.GetVertexLocations()[i]); //Debug.Log(string.Format("Going through vertex {0}", i)); if (isContactMesh) { if (htree.IsLeaf(cur) && tester.GetComponent <Asteroid>() != null) { //htree.Delete(ref cur); health -= 10 * Time.deltaTime; } return(true); } } } } return(ProcessCollision(tester, cn.transform)); // traverse the child transform } } // Hit radius of the object float hitRadius = Mathf.Sqrt(Mathf.Pow(collider.lossyScale.x, 2f) + Mathf.Pow(collider.lossyScale.y, 2f) + Mathf.Pow(collider.lossyScale.z, 2f)); if (hitRadius < 1) { hitRadius = 1; } if (delta.magnitude < tester.localScale.z && delta.magnitude < hitRadius) // condition of hit detection { if (cur != null) { if (htree.IsLeaf(cur) && tester.GetComponent <Asteroid>() != null) { health -= 10 * Time.deltaTime; } } return(true); } return(false); }