当前位置: 首页 > article >正文

en造数据结构与算法C# 群组行为优化 和 头鸟控制

实现:

1.给鸟类随机播放随机动画使得每一只鸟扇翅膀的频率都不尽相同

2.可以自行添加权重,并在最后 sumForce = separationForce + cohesionForce + alignmentForce;分别乘上相应权重,这样鸟就能快速飞行和转向辣

using System.Collections.Generic;
using UnityEngine;
using System.Collections;

public class BoidsCode1 : MonoBehaviour {
    Rigidbody rb;
    //鸟群存储线性表
    public List<GameObject> birdNeighbors = new List<GameObject>();

    //separation分离  alignment对齐 cohesion凝聚
    public Vector3 separationForce = Vector3.zero;
    public Vector3 alignmentForce = Vector3.zero;
    public Vector3 cohesionForce = Vector3.zero;
    //平均位置,鸟群朝向
    public Vector3 averagePosition = Vector3.zero;
    public Vector3 birdForward = Vector3.zero;
    //鸟群速度
    public Vector3 birdVelocity = Vector3.zero;
    //总力
    public Vector3 sumForce;

    //检测间隔
    public float checkInterval = 0.2f;
    //检测距离
    public float checkDistance = 2;
    //分离力最大值
    public float maxSeparationForce = 1.0f;

    private void Awake() {
        rb = GetComponent<Rigidbody>();
    }

    private void Start() {
        InvokeRepeating("CalcForce", 0, checkInterval);
    }

    //计算函数
    private void CalcForce() {
        //清空邻居鸟的列表
        birdNeighbors.Clear();
        //检测到范围内所有的鸟
        Collider[] colliders = Physics.OverlapSphere(transform.position, checkDistance);
        foreach (Collider collider in colliders) {
            if (collider != null && collider.gameObject != this.gameObject) {
                //添加到列表里面
                birdNeighbors.Add(collider.gameObject);
            }
        }

        //平均点位置为0
        averagePosition = Vector3.zero;

        //朝向设置为0
        birdForward = Vector3.zero;
        birdVelocity = Vector3.zero;

        separationForce = Vector3.zero; //分离力重设
        cohesionForce = Vector3.zero; //凝聚力重设
        alignmentForce = Vector3.zero; //对齐力重设

        foreach (GameObject bird in birdNeighbors) {
            //设定分离力的方向
            Vector3 spForceDirection = (this.transform.position - bird.transform.position);
            if (spForceDirection.magnitude > 0) {
                separationForce += spForceDirection.normalized / spForceDirection.sqrMagnitude;
            }
            //得到鸟群位置(加起来的和)
            averagePosition += bird.transform.position;
            //得到鸟群的方向和速度
            birdForward += bird.transform.forward;
            birdVelocity += bird.GetComponent<Rigidbody>().velocity;
        }

        //限制分离力的最大值
        if (separationForce.magnitude > maxSeparationForce) {
            separationForce = separationForce.normalized * maxSeparationForce;
        }

        //计算平均位置
        if (birdNeighbors.Count > 0) {
            averagePosition /= birdNeighbors.Count;
        }

        //设定凝聚力的方向
        Vector3 cohesionDirection = (averagePosition - transform.position).normalized;
        if (cohesionDirection.magnitude > 0) {
            cohesionForce += cohesionDirection;
        }

        //求取平均速度
        if (birdNeighbors.Count > 0) {
            alignmentForce = birdVelocity / birdNeighbors.Count;
        }

        施加分离力
        //rb.AddForce(separationForce, ForceMode.VelocityChange);
        施加凝聚力
        //rb.AddForce(cohesionForce, ForceMode.VelocityChange);
        施加对齐力
        //rb.AddForce(alignmentForce, ForceMode.VelocityChange);
        sumForce = separationForce + cohesionForce + alignmentForce;
     
        rb.AddForce(sumForce,ForceMode.Force);
        // 仅当当前鸟不是头鸟时,才设置朝向
        if (birdForward.magnitude > 0 && !gameObject.CompareTag("Leader")) {
            this.transform.forward = birdForward.normalized;
        }
        给予一只鸟速度平均速度
        //if (alignmentForce.magnitude > 0) {
        //    this.GetComponent<Rigidbody>().velocity = alignmentForce;
        //}
    }
}


http://www.kler.cn/news/313087.html

相关文章:

  • Flink 中 Checkpoint 的底层原理和机制
  • Java客户端SpringDataRedis(RedisTemplate使用)
  • Neko一个在Docker环境下的虚拟浏览器
  • 大数据-142 - ClickHouse 集群 副本和分片 Distributed 附带案例演示
  • Day69补 前后端分离思想
  • JAVA毕业设计176—基于Java+Springboot+vue3的交通旅游订票管理系统(源代码+数据库)
  • 【HTML5】html5开篇基础(1)
  • 【学习笔记】数据结构(六 ①)
  • 通过markdown表格批量生成格式化的word教学单元设计表格
  • 基于深度学习的花卉智能分类识别系统
  • 【win工具】win安装flameshot并设置截图快捷键
  • 【Python报错已解决】xlrd.biffh.XLRDError: Excel xlsx file; not supported
  • C++自动寻径算法
  • 《黑神话悟空》开发框架与战斗系统解析
  • 4.C++中程序中的命名空间
  • git 本地分支误删,怎么恢复?误删本地已提交未推送的分支!
  • 基于Python实现一个浪漫烟花秀
  • 如何设置 Django 错误邮件通知 ?
  • I2C中继器TCA9517A(TI)
  • 新160个crackme - 060-snake
  • 2024年Q3国际信息系统安全认证联盟(ISC2)内部研讨会要点分享
  • 系统架构设计师 大数据架构篇二
  • 球形包围框-Bounding Sphere-原理-代码实现
  • Mycat中间件
  • 牛客BC92,逆序输出
  • 222222222
  • Qt/C++开发经验
  • 【深度学习 transformer】理解 Transformer:机器学习界的“变形金刚
  • Vue3:v-model实现组件通信
  • Streamlit:使用 Python 快速开发 Web 应用