类似unity组件化的碰撞器

被否定的代码,留作纪念。。。

using System.Collections.Generic;
using UnityEngine;

public class MyCollision : MonoBehaviour
{
    //需要检测的碰撞器池
    List myCollision;
    //碰撞器的偏移量
    public Vector2 offset;
    //碰撞器的大小
    public Vector2 Size;
    //四个顶点
    Vector2[] peakPos = new Vector2[4];

    Transform tran;

    private void Awake()
    {
        tran = transform;
        Vector2 localSize = tran.localScale;
        peakPos[0] = offset + new Vector2(-Size.x / 2 * localSize.x, Size.y / 2 * localSize.y);
        peakPos[1] = offset + new Vector2(Size.x / 2 * localSize.x, Size.y / 2 * localSize.y);
        peakPos[2] = offset + new Vector2(Size.x / 2 * localSize.x, -Size.y / 2 * localSize.y);
        peakPos[3] = offset + new Vector2(-Size.x / 2 * localSize.x, -Size.y / 2 * localSize.y);
    }
    
    public void RestartRecount()
    {
        Vector2 localSize = tran.localScale;
        peakPos[0] = offset + new Vector2(-Size.x / 2 * localSize.x, Size.y / 2 * localSize.y);
        peakPos[1] = offset + new Vector2(Size.x / 2 * localSize.x, Size.y / 2 * localSize.y);
        peakPos[2] = offset + new Vector2(Size.x / 2 * localSize.x, -Size.y / 2 * localSize.y);
        peakPos[3] = offset + new Vector2(-Size.x / 2 * localSize.x, -Size.y / 2 * localSize.y);
    }

    private void OnDrawGizmos()
    {
        tran = transform;
        RestartRecount();
    }

    private void OnEnable()
    {
        myCollision = null;
        enterName = null;
    }
    /// 
    /// 获得碰撞器顶点
    /// 
    /// 
    /// 
    public Vector2 GetpeakPos(int index)
    {
        Vector2 newVec = tran.rotation * peakPos[index] + tran.localPosition;
        return newVec;
    }
    /// 
    /// 是否碰撞一般式方程相交,两条直线坐标
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    bool isCross(Vector2 v1, Vector2 v2, Vector2 v3, Vector2 v4)
    {
        float a1 = v2.y - v1.y;
        float b1 = v1.x - v2.x;
        float c1 = v1.y * v2.x - v1.x * v2.y;

        float a2 = v4.y - v3.y;
        float b2 = v3.x - v4.x;
        float c2 = v3.y * v4.x - v3.x * v4.y;


        float v11 = a1 * v3.x + b1 * v3.y + c1;
        float v12 = a1 * v4.x + b1 * v4.y + c1;

        float v21 = a2 * v1.x + b2 * v1.y + c2;
        float v22 = a2 * v2.x + b2 * v2.y + c2;

        bool yes1 = false;
        bool yes2 = false;

        if ((v11 < 0 && v12 > 0) || (v11 > 0 && v12 < 0))
        {
            yes1 = true;
        }
        if ((v21 < 0 && v22 > 0) || (v21 > 0 && v22 < 0))
        {
            yes2 = true;
        }
        return (yes1 && yes2);
    }
    /// 
    /// 判断点是否在制定区域
    /// 
    /// 
    /// 
    public bool isPointCross(Vector2 point)
    {
        return GetPointCross(GetpeakPos(0), GetpeakPos(1), point) * GetPointCross(GetpeakPos(2), GetpeakPos(3), point) >= 0
            && GetPointCross(GetpeakPos(1), GetpeakPos(2), point) * GetPointCross(GetpeakPos(3), GetpeakPos(0), point) >= 0;
    }
    /// 
    /// 向量叉乘,如果值大于零,夹角为锐角,如果大于零,夹角为钝角
    /// 
    /// 
    /// 
    /// 
    /// 
    float GetPointCross(Vector2 v1, Vector2 v2, Vector2 point)
    { 
        return (v2.x - v1.x) * (point.y - v1.y) - (v2.y - v1.y) * (point.x - v1.x); // 向量叉乘  X1*Y2-X2*Y1
    }
    /// 
    /// 碰撞执行方法的委托
    /// 
    /// 
    public delegate void MyTriggerDelegate(MyCollision collision);
    public MyTriggerDelegate myTriggerEnter;
    string enterName;

    private void Update()
    {
        if (myCollision==null)
        {
            return;
        }

        for (int k = 0; k < myCollision.Count; k++) // 遍历需要检测的碰撞池,为节省性能,以后要改成四叉树遍历
        {
            if (myCollision[k] == null)
            {
                break;
            }
            for (int i = 0; i < 4; i++) // 检测碰撞者的四条边和被碰撞者的四条边是否相交
            {
                for (int j = 0; j < 4; j++)
                {
                    if (isCross(GetpeakPos(i), GetpeakPos(i == 3 ? 0 : i + 1), myCollision[k].GetpeakPos(j), myCollision[k].GetpeakPos(j == 3 ? 0 : j + 1)))
                    {
                        if (enterName != myCollision[k].name) // 防止重复检测
                        {
                            myTriggerEnter(myCollision[k]);
                            enterName = myCollision[k].name;
                        }
                    }
                }
            }
        }
    }
}

你可能感兴趣的:(C#,unity)