如何确定两个圆圈是否重叠或彼此接触?

时间:2019-01-27 16:08:49

标签: c# unity3d geometry collision-detection

我使用下面的代码在游戏对象周围画一个圆:

using UnityEngine;
 using System.Collections;

 [RequireComponent(typeof(LineRenderer))]
 public class DrawCircle : MonoBehaviour
 {
     [Range(0, 50)]
     public int segments = 50;
     [Range(0, 5)]
     public float xradius = 5;
     [Range(0, 5)]
     public float yradius = 5;
     LineRenderer line;

     void Start()
     {
         line = gameObject.GetComponent<LineRenderer>();
         line.positionCount = segments + 1;
         line.useWorldSpace = false;
         CreatePoints();
     }

     void Update()
     {
         CreatePoints();
     }

     void CreatePoints()
     {
         float x;
         float y;
         float z;

         float angle = 20f;

         for (int i = 0; i < (segments + 1); i++)
         {
             x = Mathf.Sin(Mathf.Deg2Rad * angle) * xradius;
             z = Mathf.Cos(Mathf.Deg2Rad * angle) * yradius;

             line.SetPosition(i, new Vector3(x, 0, z));

             angle += (360f / segments + 1);
         }
     }
 }

让我们说,如果我将此Circle类添加为对象A和对象B的组件。我如何确定对象A上的圆是否触及对象B上的圆?

1 个答案:

答案 0 :(得分:2)

如果圆心之间的距离大于其半径之和,则圆不会接触。如果较小,他们会这样做