美文网首页
形状碰撞测试

形状碰撞测试

作者: jojo911 | 来源:发表于2019-01-16 14:27 被阅读0次

https://blog.csdn.net/i_dovelemon/article/details/40394741

public class collisiontest : MonoBehaviour
{
    public MeshRenderer render;
    public SphereCollider sc;

    void Start ()
    {}
    
    // Update is called once per frame
    void Update ()
    {
        IsSphereInterSectAABB(sc.center, sc.radius, render.bounds);
    }

    bool IsSphereInterSectAABB(Vector3 p, float radius, Bounds b)
    {
        Vector3 close = ClosePointToAABB(p, b);

        float dis = Vector3.Distance(close, p);
        Debug.LogFormat("{0} isIN:{1}", dis, dis <= radius);

        return dis <= sc.radius;
    }

    Vector3 ClosePointToAABB(Vector3 p, Bounds b)
    {
        if (p.x < b.min.x) p.x = b.min.x;
        if (p.x > b.max.x) p.x = b.max.x;

        if (p.y < b.min.y) p.y = b.min.y;
        if (p.y > b.max.y) p.y = b.max.y;

        if (p.z < b.min.z) p.z = b.min.z;
        if (p.z > b.max.z) p.z = b.max.z;

        return p;
    }
}

相关文章

网友评论

      本文标题:形状碰撞测试

      本文链接:https://www.haomeiwen.com/subject/vvrmdqtx.html