Unity技能伤害区域判定(矩形,圆,三角形)(二)

时间:2024-03-19 16:44:21

 源码地址:https://download.csdn.net/download/qq_37310110/10886004

一.判断目标点是否在某一圆形区域内

定义圆形

public class Circle {

    public Vector2 Center;
    public float Radius;

    public Circle(Vector2 center, float radius)
    {
        this.Center = center;
        this.Radius = radius;
    }

    public bool Contains( Vector2 point)
    {
        Vector2 vector = point - this.Center;
        return (vector.SqrMagnitude() <= (this.Radius * this.Radius));
    }

    public Vector2 Eval(float t)
    {
        return new Vector2(this.Center.x + (this.Radius * Mathf.Cos(t)), this.Center.y + (this.Radius * Mathf.Sin(t)));
    }
}

测试脚本

public class CircleTest : MonoBehaviour {

    public Transform Point;
    public Transform cCircle;
    Circle circle;
    private void OnDrawGizmos()
    {
        if (circle != null)
        {
            DrawCircle(circle);
            //Gizmos.DrawSphere(Point.position, 0.11f);
        }
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
             circle = new Circle(cCircle.position, cCircle.localScale.x);
            bool cont = circle.Contains(Point.position);
            if (cont)
            {
                Debug.Log("目标点在矩形范围内");
            }
            else
            {
                Debug.Log("目标点不在矩形范围内");
            }
        }
    }
    protected void DrawCircle( Circle circle)
    {
        Gizmos.color = Color.blue;
        int count = 40;//绘制圆形基础点的个数 适当即可
        float delta = 2f * Mathf.PI / count;
        Vector3 prev = circle.Eval(0);
        for (int i = 1; i <= count; ++i)
        {
            Vector3 curr = circle.Eval(i * delta);
            Gizmos.DrawLine(prev, curr);
            prev = curr;
        }
    }
}

结果

Unity技能伤害区域判定(矩形,圆,三角形)(二)

二.判断目标点是否在指定的三角形区域内

定义三角形

public class Triangle 
{
    public enum Orientations
    {
        CW,//三个点顺时针
        CCW,//三个点逆时针
        None//不清楚点的顺序
    }

    public Vector2 V0;
    public Vector2 V1;
    public Vector2 V2;
    public Triangle(Vector2 v0, Vector2 v1, Vector2 v2)
    {
        this.V0 = v0;
        this.V1 = v1;
        this.V2 = v2;
    }
    public static Triangle CreateTriangle2(Transform v0, Transform v1, Transform v2)
    {
        return new Triangle(v0.position, v1.position, v2.position);
    }

    public Orientations CalcOrientation(float threshold = 1E-05f)
    {
        float num = this.CalcDeterminant();
        if (num > threshold)
        {
            return Orientations.CCW;
        }
        if (num < -threshold)
        {
            return Orientations.CW;
        }
        return Orientations.None;
    }

    public float CalcDeterminant()
    {
        return ((((((this.V1.x * this.V2.y) + (this.V0.x * this.V1.y)) + (this.V2.x * this.V0.y)) - (this.V1.x * this.V0.y)) - (this.V2.x * this.V1.y)) - (this.V0.x * this.V2.y));
    }

    public bool Contains(Vector2 point)
    {
        bool flag = (((point.x - this.V1.x) * (this.V0.y - this.V1.y)) - ((point.y - this.V1.y) * (this.V0.x - this.V1.x))) < 0f;
        bool flag2 = (((point.x - this.V2.x) * (this.V1.y - this.V2.y)) - ((point.y - this.V2.y) * (this.V1.x - this.V2.x))) < 0f;
        if (flag != flag2)
        {
            return false;
        }
        bool flag3 = (((point.x - this.V0.x) * (this.V2.y - this.V0.y)) - ((point.y - this.V0.y) * (this.V2.x - this.V0.x))) < 0f;
        return (flag2 == flag3);
    }

    /// <summary>
    /// 当已经知道 三个顶点的顺序是逆时针方向的时候使用
    /// </summary>
    /// <param name="point"></param>
    /// <returns></returns>
    public bool ContainsCCW(Vector2 point)
    {
        if ((((point.x - this.V0.x) * (this.V1.y - this.V0.y)) - ((point.y - this.V0.y) * (this.V1.x - this.V0.x))) > 0f)
        {
            return false;
        }
        if ((((point.x - this.V1.x) * (this.V2.y - this.V1.y)) - ((point.y - this.V1.y) * (this.V2.x - this.V1.x))) > 0f)
        {
            return false;
        }
        if ((((point.x - this.V2.x) * (this.V0.y - this.V2.y)) - ((point.y - this.V2.y) * (this.V0.x - this.V2.x))) > 0f)
        {
            return false;
        }
        return true;
    }

    /// <summary>
    /// 当已经知道 三个顶点的顺序是顺时针方向的时候使用
    /// </summary>
    /// <param name="point"></param>
    /// <returns></returns>
    public bool ContainsCW(Vector2 point)
    {
        if ((((point.x - this.V0.x) * (this.V1.y - this.V0.y)) - ((point.y - this.V0.y) * (this.V1.x - this.V0.x))) < 0f)
        {
            return false;
        }
        if ((((point.x - this.V1.x) * (this.V2.y - this.V1.y)) - ((point.y - this.V1.y) * (this.V2.x - this.V1.x))) < 0f)
        {
            return false;
        }
        if ((((point.x - this.V2.x) * (this.V0.y - this.V2.y)) - ((point.y - this.V2.y) * (this.V0.x - this.V2.x))) < 0f)
        {
            return false;
        }
        return true;
    }
}

测试脚本

public class TriangleTest : MonoBehaviour {
    public Transform Point;
    public Transform V0, V1, V2;
    Triangle triangle;
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            triangle = Triangle.CreateTriangle2(V0, V1, V2);
            Triangle.Orientations orientation = triangle.CalcOrientation();

            bool cont = false;
            if (orientation == Triangle.Orientations.CCW)
            {
                 cont = triangle.Contains(Point.position);
                 //cont1 = triangle.ContainsCCW(Point.position); // 如果你知道三角形方向(顶点顺序方向),就用这个
            }
            else if (orientation == Triangle.Orientations.CW)
            {
                 cont = triangle.Contains(Point.position);
                 //cont1 = triangle.ContainsCW(Point.position); // 如果你知道三角形方向(顶点顺序方向),就用这个
            }
            else // Degenerate
            {
                Debug.LogError("Triangle is degenerate");
            }
            if (cont)
            {
                Debug.Log("目标点在矩形范围内");
            }
            else
            {
                Debug.Log("目标点不在矩形范围内");
            }
        }
    }
    private void OnDrawGizmos()
    {
        if (triangle!=null)
        {
            DrawTriangle( triangle);
        }
    }


    void DrawTriangle( Triangle triangle)
    {
        Gizmos.color = Color.blue;
        Gizmos.DrawLine(triangle.V0, triangle.V1);
        Gizmos.DrawLine(triangle.V1, triangle.V2);
        Gizmos.DrawLine(triangle.V2, triangle.V0);
    }
}

结果

Unity技能伤害区域判定(矩形,圆,三角形)(二)

 

 源码地址:https://download.csdn.net/download/qq_37310110/10886004