private AllMesh selected;     // the mesh that will be selected in the world
    /// <summary>
    /// Checks if something has been pressed by polling the current mouse status.
    /// Called once per frame.
    /// </summary>
    public void ProcessMouseEvents()
    {
        if (EventSystem.current.IsPointerOverGameObject() && !myWorld.HasSelected())
        {
            return;
        }

        // If you left alt on a mesh object, it will activate it
        // Code by nathan pham
        if (Input.GetKeyDown(KeyCode.LeftControl))
        {
            GameObject objAtMouse;
            Vector3    mousePos;

            if (GetObjectAtMouse(out objAtMouse, out mousePos, 1 << myWorld.selectableLayer))
            {
                if (objAtMouse.GetComponent <AllMesh>() != null)
                {
                    if (selected == null)
                    {
                        selected = objAtMouse.GetComponent <AllMesh>();
                    }
                    else
                    {
                        selected.HideNormals();
                        selected = objAtMouse.GetComponent <AllMesh>();
                    }
                    selected.ShowNormals();
                }
            }
        }

        // else, the selected hides its normals and becomes null
        if (Input.GetKeyUp(KeyCode.LeftControl) || Input.GetKeyUp(KeyCode.RightControl))
        {
            if (!myWorld.HasSelected())
            {
                if (selected != null)
                {
                    selected.HideNormals();
                    selected = null;
                }
            }
        }         // to here
        else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
        {
            HandleMesh();
        }
    }
示例#2
0
    // 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);
    }