可用于挖机中的挖沟处机械结构运行
using UnityEngine;
public class JointLinkage : MonoBehaviour
{
public bool isDrawGizmos = true;
public Transform circle1; // 圆1的Transform组件
public Transform circle2; // 圆2的Transform组件
public float radius1; // 圆1的半径
public float radius2; // 圆2的半径
public Vector3 intersection1; //相交点1
public Vector3 intersection2; //相交点2
public CoordinateAxisType coordinateAxisType1 = CoordinateAxisType.X_Y; // 选择要绘制的轴
public IntersectionType intersectionType = IntersectionType.intersection1;
private Vector3 center1;
private Vector3 center2;
private void Start()
{
//SetRadius();
}
void Update()
{
CircleFocalPoint();
}
private void OnDrawGizmos()
{
if (!isDrawGizmos) return;
//SetRadius();
Gizmos.color = Color.black;
Gizmos.DrawWireSphere(circle1.position, radius1);
Gizmos.color = Color.white;
Gizmos.DrawWireSphere(circle2.position, radius2);
}
public Vector3 GetSelfPos()
{
switch (intersectionType)
{
case IntersectionType.intersection1:
return transform.position = intersection1;
case IntersectionType.intersection2:
return transform.position = intersection2;
}
return transform.position = intersection2;
}
private void CircleFocalPoint()
{
center1 = circle1.position; // 圆1的中心点
center2 = circle2.position; // 圆2的中心点
float distance = Vector3.Distance(center1, center2); // 两个圆心的距离
// 判断两个圆是否相交
if (distance < radius1 + radius2)
{
// 计算圆1和圆2的交点
float a = (radius1 * radius1 - radius2 * radius2 + distance * distance) / (2 * distance);
float h = Mathf.Sqrt(radius1 * radius1 - a * a);
Vector3 p2 = center1 + a * (center2 - center1) / distance;
Vector3[] intersections;
GetIntersection(p2, h, center1, center2, distance, out intersections);
intersection1 = intersections[0];
intersection2 = intersections[1];
if (isDrawGizmos)
DrawLine();
}
else
{
// 两个圆不相交,
Debug.LogWarning("挖机报废了!!!");
}
}
private void GetIntersection(Vector3 p2, float h, Vector3 center1, Vector3 center2, float distance, out Vector3[] intersections)
{
ComputeIntersection(coordinateAxisType1, p2, h, center1, center2, distance, out intersections);
}
private void ComputeIntersection(CoordinateAxisType coordinateAxisType, Vector3 p2, float h, Vector3 center1, Vector3 center2, float distance, out Vector3[] intersections)
{
intersections = new Vector3[2];
switch (coordinateAxisType)
{
case CoordinateAxisType.X_Y:
intersections[0] = new Vector3(
p2.x + h * (center2.y - center1.y) / distance,
p2.y - h * (center2.x - center1.x) / distance,
center1.z);
intersections[1] = new Vector3(
p2.x - h * (center2.y - center1.y) / distance,
p2.y + h * (center2.x - center1.x) / distance,
center1.z);
break;
case CoordinateAxisType.X_Z:
intersections[0] = new Vector3(
p2.x + h * (center2.z - center1.z) / distance,
0,
p2.z - h * (center2.x - center1.x) / distance);
intersections[1] = new Vector3(
p2.x - h * (center2.z - center1.z) / distance,
0,
p2.z + h * (center2.x - center1.x) / distance);
break;
case CoordinateAxisType.Y_Z:
intersections[0] = new Vector3(
0,
p2.y + h * (center2.z - center1.z) / distance,
p2.z - h * (center2.y - center1.y) / distance);
intersections[1] = new Vector3(
0,
p2.y - h * (center2.z - center1.z) / distance,
p2.z + h * (center2.y - center1.y) / distance);
break;
default:
break;
}
}
private void SetRadius()
{
radius1 = circle1.localScale.x / 2f;
radius2 = circle2.localScale.x / 2f;
}
private void DrawLine()
{
Debug.DrawLine(center1, center2, Color.red);
Debug.DrawLine(intersection1, intersection2, Color.green);
switch (intersectionType)
{
case IntersectionType.intersection1:
Debug.DrawLine(center1, intersection1, Color.blue);
Debug.DrawLine(center2, intersection1, Color.yellow);
break;
case IntersectionType.intersection2:
Debug.DrawLine(center1, intersection2, Color.blue);
Debug.DrawLine(center2, intersection2, Color.yellow);
break;
default:
break;
}
}
public enum CoordinateAxisType
{
X_Y,
X_Z,
Y_Z,
}
public enum IntersectionType
{
intersection1,
intersection2,
}
}