コード例 #1
0
    /// <summary>
    /// Method to determine collision using bounding circles
    /// </summary>
    /// <param name="A">Game object with sprite info script</param>
    /// <param name="B">Game object with sprite info script</param>
    /// <returns></returns>
    bool CircleCollision(GameObject A, GameObject B)
    {
        SpriteInfo a = A.GetComponent <SpriteInfo>();
        SpriteInfo b = B.GetComponent <SpriteInfo>();

        float x = Mathf.Pow(a.Center().x - b.Center().x, 2);
        float y = Mathf.Pow(a.Center().y - b.Center().y, 2);

        if (x + y > Mathf.Pow(a.radius + b.radius, 2))
        {
            return(false);
        }

        return(true);
    }
コード例 #2
0
    /// <summary>
    /// Checks if two game objects are colliding using circle collision.
    /// </summary>
    /// <returns><c>true</c>, if collision was detected, <c>false</c> otherwise.</returns>
    /// <param name="obj1">Obj1.</param>
    /// <param name="obj2">Obj2.</param>
    public bool CircleCollision(GameObject obj1, GameObject obj2)
    {
        //get the sprtie info scripts from each game object which hold corrected bounds of the sprite renderers
        SpriteInfo info1 = obj1.GetComponent <SpriteInfo>();
        SpriteInfo info2 = obj2.GetComponent <SpriteInfo>();

        //distance between centers
        Vector3 distance = info2.Center() - info1.Center();
        float   dist     = distance.magnitude * distance.magnitude;

        //check for AABB collision
        if ((info1.GetRadiusBullet() + info2.GetRadiusBullet()) * (info1.GetRadiusBullet() + info2.GetRadiusBullet()) > dist)
        {
            //Debug.Log("Colliding");
            return(true);
        }

        //if they are not colliding return false
        return(false);
    }