unity 机械臂控制(一)

unity 机械臂控制

基本原理

机械臂的控制主要是通过控制父节点的旋转,带动子节点移动,子节点的旋转再带动自己的子节点移动以此类推。每个节点的旋转单一,通常是水平方向和垂直方向,即x轴旋转和y轴旋转,也有可能出现z轴旋转,这里可以控制模型的的轴方向实现统一。

实现的难点

可控的旋转是自身的相对旋转,但是实际结果却是其父节点旋转和位移的累加。

实现的方式

机械臂运动有两种方式,一种是父节点运动后子节点才运动;另一种是各节点同时运动。

第一种实现方式比较简单,不用考虑累加的结果;

第二种方式比较复杂,可能要用到傅里叶变换或者用结果反推过程。

第一种方式unity实现方法

节点控制

针对每个节点的控制

using System;
using UnityEngine;

public class JxbPoint : MonoBehaviour
{
    public RotateType RotateType;//判断旋转的方式
    public float Time;//运动的时间
    float curAngle;//记录每个节点上一次的移动的角度
    public void SetAngle(float angle, Action action)
    {
        if (curAngle== angle)
        {
            action();
        }
        else
        {
            if (RotateType == RotateType.Vertical)
            {
                transform.RotateX(angle, Time, action);
            }
            else
            {
                transform.RotateY(angle, Time, action);
            }
            curAngle = angle;
        }
    }
}

用来控制整体旋转

using UnityEngine;
public enum RotateType
{
    Vertical,
    Horizontal
}
public class JxbControler : MonoBehaviour
{
    public  JxbPoint[] JxbPoints;
    //旋转的数据
    float[] place0 = { -60, -112,30, 10,0,3,180 };
    float[] place1 = {-60,-186,82,10,0.5f,-7, 180};
    float[] place2 = { -25, -186, 82, 10, 0.5f, -7, 180 };

    void MoveJxb(float[] data,int i)
    {
        JxbPoints[i].SetAngle(data[i], () =>
        {
            i++;
            if (i>=JxbPoints.Length)
            {
                return;
            }
            MoveJxb(data, i); });
    }
    public void ControlerMove(int i)
    {
        switch (i)
        {
            case 0:
                MoveJxb(place0,0);
                break;
            case 1:
                MoveJxb(place1,0);
                break;
            case 2:
                MoveJxb(place2,0);
                break;
            default:
                break;
        }
    }
}

Transform的方法扩展

using UnityEngine;
using DG.Tweening;
using System;

public static class TransformExtention
{
    public static void RotateX(this Transform transform, float x, float duration, Action action)
    {
        Vector3 my_EulerAngles = transform.eulerAngles;
        transform.DOLocalRotate(new Vector3(x, 0, 0), duration).OnComplete(() => action());
    }
    public static void RotateY(this Transform transform, float y, float duration, Action action)
    {
        Vector3 my_EulerAngles = transform.eulerAngles;
        transform.DOLocalRotate(new Vector3(0, y, 0), duration).OnComplete(() => action());
    }
}

demo下载地址:下载

猜你喜欢

转载自blog.csdn.net/dxs1990/article/details/123596141