AstarPathfindingProject 中RVO碰撞体扩展

原本库中只有矩形RVO碰撞体,如果要添加自己的需要继承RVOObstacle抽象类,重写里面的方法

例如下面的圆柱形碰撞

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using Pathfinding.RVO;
#endif

namespace Pathfinding.RVO
{
    [AddComponentMenu("Pathfinding/Local Avoidance/Cylinder Obstacle")]
    public class RVOCylinderObstacle : RVOObstacle
    {
        /// <summary>Height of the obstacle</summary>
		public float height = 1;

        /// <summary> 边数 </summary>
        [Range(5, 12)]
        public int edge = 5;

        /// <summary>
        /// 半径
        /// </summary>
        public float radius = 1f;

        /// <summary>Center of the square</summary>
        public Vector2 center = Vector3.zero;

        protected override bool StaticObstacle { get { return false; } }
        protected override bool ExecuteInEditor { get { return true; } }
        protected override bool LocalCoordinates { get { return true; } }
        protected override float Height { get { return height; } }

        //If UNITY_EDITOR to save a few bytes, these are only needed in the editor
#if UNITY_EDITOR
        [Range(5, 12)]
        private int _edge = 5;
        private Vector2 _center;
        private float _height;
        private float _radius;
#endif

        protected override bool AreGizmosDirty()
        {
#if UNITY_EDITOR
            bool ret = _edge != edge || _radius != radius || _height != height || _center != center;
            _edge = edge;
            _center = center;
            _height = height;
            _radius = radius;
            return ret;
#else
			return false;
#endif
        }

        protected override void CreateObstacles()
        {
            edge = Mathf.Clamp(edge, 5, 12);
            height = Mathf.Abs(height);
            if(radius<0) radius = 0.1f;

            var verts = new Vector3[edge];
            float p = 360f / edge;
            float angle = 0f;
            for (int i = 0; i < verts.Length; i++)
            {
                float x = center.x + radius * Mathf.Cos(angle * Mathf.PI / 180);
                float z = center.y + radius * Mathf.Sin(angle * Mathf.PI / 180);
                verts[i] = new Vector3(x,0,z);
                angle += p;
            }

            AddObstacle(verts, height);
        }
    }
}


#if UNITY_EDITOR
namespace Pathfinding {
	[CustomEditor(typeof(RVOCylinderObstacle))]
	[CanEditMultipleObjects]
	public class RVOCylinderObstacleEditor : Editor {
		public override void OnInspectorGUI () {
			DrawDefaultInspector();
		}
	}
}
#endif

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值