如何在2D中创建两个圆之间的对撞机

时间:2016-09-05 20:43:38

标签: c# unity3d game-physics

I have created a mesh of this image and i want the collider between the pink shaded region

After writing the following Script i got this collider

我创建了第一个图像的网格,我想要粉红色阴影区域之间的对撞机

写完以下脚本后,我在第二张图片中找到了对撞机

    void InnerOuterCircle () {

    vertice = new List<Vector3> ();
    triangle = new List<int> ();

    for (int x = 0; x <= angle; x ++) {

        vertice.Add(new Vector3(Mathf.Cos(x * Mathf.Deg2Rad) * innerRadius,Mathf.Sin(x * Mathf.Deg2Rad) * innerRadius));
        vertice.Add(new Vector3(Mathf.Cos(x * Mathf.Deg2Rad) * outerRadius,Mathf.Sin(x * Mathf.Deg2Rad) * outerRadius));

    }

    for (int x = 0; x < vertice.Count - 2; x += 2) {

        triangle.Add (x + 0);
        triangle.Add (x + 2);
        triangle.Add (x + 1);

        triangle.Add (x + 2);
        triangle.Add (x + 3);
        triangle.Add (x + 1);

    }

    Mesh mesh = new Mesh ();

    MeshFilter filter = GetComponent<MeshFilter> ();
    filter.mesh = mesh;

    mesh.vertices = vertice.ToArray ();
    mesh.triangles = triangle.ToArray ();

    PolygonCollider2D collider = gameObject.AddComponent<PolygonCollider2D> ();
    Vector2[] edgePoints = new Vector2[vertice.Count];
    for (int i = 0; i < vertice.Count; i++) {
        edgePoints [i] = vertice [i];
    }
    collider.points = edgePoints;
}

1 个答案:

答案 0 :(得分:1)

假设在Unity中可以进行自定义碰撞检测逻辑,则圆之间的碰撞检测相当简单;只需在他们的中心之间的向量上使用毕达哥拉斯定理:

return distance <= r1outer + r2outer && distance >= r1inner + r2inner;

如果圆圈的中心有圆孔,你可以添加一个额外的测试,以确保它们不太接近,与现有的半径测试相同,但是使用&gt; =而不是&lt; = on on内:

{{1}}