提前预测刚体移动轨迹 预测运动轨迹

2024-01-02 14:04

本文主要是介绍提前预测刚体移动轨迹 预测运动轨迹,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

提前预测刚体移动轨迹 预测运动轨迹

  • 一、效果
  • 二、介绍
  • 三、脚本
    • RigidbodyExtension.cs 计算工具类
    • DrawLine.cs 画线工具类
  • 四、资源分享

一、效果

在这里插入图片描述

二、介绍

通过计算Unity物理系统的运动方位来判断下一步移动的位置,主要用于物体运动的提前预测,通常使用于炮弹发射,机枪发射,足球篮球网球的运动等多种真实运动的物体。

三、脚本

RigidbodyExtension.cs 计算工具类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;namespace Dweiss
{public static class RigidbodyExtension{public static Vector3[] CalculateMovement(this Rigidbody that,int stepCount, float timeBeteenStep){return that.CalculateMovement(stepCount, timeBeteenStep, Vector3.zero, Vector3.zero);}public static Vector3[] CalculateMovement(this Rigidbody that,int stepCount, float timeBeteenStep, Vector3 addedSpeed){return that.CalculateMovement(stepCount, timeBeteenStep, addedSpeed, Vector3.zero);}/// <summary>/// /// </summary>/// <param name="that"></param>/// <param name="stepCount">Number of steps</param>/// <param name="timeBeteenStep">number of frames to skip</param>/// <param name="addedSpeed"></param>/// <param name="addedForce"></param>/// <returns></returns>public static Vector3[] CalculateMovement(this Rigidbody that,int stepCount, float timeBeteenStep, Vector3 addedSpeed, Vector3 addedForce){//var ret = new Vector3[stepCount];// var addedV = (addedForce / that.mass) * Time.fixedDeltaTime;var v = (that.isKinematic == false ? that.velocity : Vector3.zero);// + addedSpeed + addedV;var a = (that.useGravity && that.isKinematic == false ? Physics.gravity : Vector3.zero);return CalculateMovement(that.transform.position, v, a, stepCount, timeBeteenStep, addedSpeed, addedForce, that.mass, that.drag);//var x = that.transform.position;//var calc = new Vector3[] { x, v };//for (var i = 0; i < stepCount; ++i)//{//    calc = CalculateNewPos(calc[0], calc[1], a, that.drag, timeBeteenStep);//    ret[i] = calc[0];//}//return ret;}public static Vector3[] CalculateMovement(Vector3 position, Vector3 velocity, Vector3 acc, int stepCount, float timeBeteenStep, Vector3 addedSpeed, Vector3 addedForce, float mass, float drag){var ret = new Vector3[stepCount];var addedV = (addedForce / mass) * Time.fixedDeltaTime;var v = velocity + addedSpeed + addedV;var a = acc;var x = position;var calc = new Vector3[] { x, v };for (var i = 0; i < stepCount; ++i){calc = CalculateNewPos(calc[0], calc[1], a, drag, timeBeteenStep);ret[i] = calc[0];}return ret;}private static Vector3[] CalculateNewPos(Vector3 x, Vector3 v, Vector3 a, float drag, float deltaTimeCount){var dt = Time.fixedDeltaTime;var aDt = a * dt;var dragDt = 1 - drag * dt;dragDt = dragDt < 0 ? 0 : dragDt;var acc = .5f * a * dt * dt;for (int i = 0; i < deltaTimeCount; ++i){v = (v + aDt) * dragDt;x = x + v * dt + acc;}return new Vector3[]{ x, v };}private static Vector3 CalculateVDrag(Vector3 v, Vector3 a, float drag, int deltaTimeCount){var dt = Time.fixedDeltaTime;for(int i=0; i < deltaTimeCount; ++i)v = (v + a * dt) * (1 - drag * dt);return v;}Doesn't supports public static Vector3[] CalculateTime(this Rigidbody that, Vector3 targetPos){return CalculateTime(that, targetPos, Vector3.zero, Vector3.zero);}public static Vector3[] CalculateTime(this Rigidbody that, Vector3 targetPos,Vector3 addedSpeed){return CalculateTime(that, targetPos, addedSpeed, Vector3.zero);}public static Vector3[] CalculateTime(this Rigidbody that, Vector3 targetPos, Vector3 addedSpeed, Vector3 addedForce){var addedV = (addedForce / that.mass) * Time.fixedDeltaTime;var v = that.velocity + addedSpeed + addedV;var a = (that.useGravity && that.isKinematic == false ? Physics.gravity : Vector3.zero);var x0 = that.transform.position;//x = x0 +vt + .5*a*t^2//-b +- SQR(b*b - 4*a*c)/2*a // a= .5*a//b=v//a=x0-x//t12 = -v +- SQR(v*v -4 * .5 * a * (x0-x))/ 2 * .5 * avar x = x0 - targetPos;var sqr = (v.PointMul(v) - 4 * .5f * a.PointMul(x)).Sqrt();var t1 = (-v + sqr).PointDiv(2 * .5f * a);var t2 = (-v - sqr).PointDiv(2 * .5f * a);// a=0: (x0-x) + vt -> t = (x-x0)/vvar tWhenA0 = x.PointDiv(v);//a = 0if (float.IsNaN(t1.x)) { t2.x = t1.x = tWhenA0.x; }if (float.IsNaN(t1.y)) { t2.y = t1.y = tWhenA0.y; }if (float.IsNaN(t1.z)) { t2.z = t1.z = tWhenA0.z; }// a = 0 && v = 0if (float.IsNaN(t1.x) && x0.x == targetPos.x) { t2.x = t1.x = 0; }if (float.IsNaN(t1.y) && x0.y == targetPos.y) { t2.y = t1.y = 0; }if (float.IsNaN(t1.z) && x0.z == targetPos.z) { t2.z = t1.z = 0; }return new Vector3[] { t1, t2 };}public static Vector3 Sqrt(this Vector3 v){return new Vector3(Mathf.Sqrt(v.x), Mathf.Sqrt(v.y), Mathf.Sqrt(v.z));}public static Vector3 PointMul(this Vector3 v1, Vector3 v2){return new Vector3(v1.x*v2.x, v1.y*v2.y, v1.z*v2.z);}public static Vector3 PointDiv(this Vector3 v1, Vector3 v2){return new Vector3(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z);}}
}

DrawLine.cs 画线工具类

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Dweiss;[RequireComponent(typeof(LineRenderer))]
public class DrawLine : MonoBehaviour {/// <summary>/// 引导线/// </summary>public LineRenderer _lr;/// <summary>/// 刚体/// </summary>public Rigidbody _rb;/// <summary>/// 跨度/// </summary>public float timeBeteenStep = 1;/// <summary>/// 跨度数量/// </summary>public int stepCount = 30;/// <summary>/// 加速度/// </summary>public Vector3 addedV, addedF;void Start() {//找引导线_lr = GetComponent<LineRenderer>();//找刚体_rb = GetComponent<Rigidbody>();}public void AddPower(Vector3 dir){CalcTime();_rb.velocity += addedV;addedV = Vector3.zero;_rb.AddForce(dir, ForceMode.Force);addedF = Vector3.zero;}private void CalcTime(){Vector3[] t = _rb.CalculateTime(new Vector3(0, 0, 0),addedV, addedF);var timeT = new Vector3[]{new Vector3(Time.time + t[0].x, Time.time + t[0].y, Time.time + t[0].z),new Vector3(Time.time + t[1].x, Time.time + t[1].y, Time.time + t[1].z)};}#region 抛物曲线/// <summary>/// 移动走向曲线/// </summary>private void DrawMovementLine(){var res = _rb.CalculateMovement(stepCount, timeBeteenStep, addedV, addedF);_lr.positionCount = stepCount + 1;_lr.SetPosition(0, transform.position);for (int i = 0; i < res.Length; ++i){_lr.SetPosition(i + 1, res[i]);}}#endregionvoid Update (){DrawMovementLine();if (Input.GetKeyDown(KeyCode.W)){AddPower((Vector3.right + Vector3.up) * 500f);}}
}

四、资源分享

CSDN下载链接

在我的资源中搜索 RigidBodyExtension

这篇关于提前预测刚体移动轨迹 预测运动轨迹的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/562661

相关文章

双系统电脑中把Ubuntu装进外接移动固态硬盘的全过程

《双系统电脑中把Ubuntu装进外接移动固态硬盘的全过程》:本文主要介绍如何在Windows11系统中使用VMware17创建虚拟机,并在虚拟机中安装Ubuntu22.04桌面版或Ubunt... 目录一、首先win11中安装vmware17二、磁盘分区三、保存四、使用虚拟机进行系统安装五、遇见的错误和解决

使用FileChannel实现文件的复制和移动方式

《使用FileChannel实现文件的复制和移动方式》:本文主要介绍使用FileChannel实现文件的复制和移动方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录使用 FileChannel 实现文件复制代码解释使用 FileChannel 实现文件移动代码解释

慢sql提前分析预警和动态sql替换-Mybatis-SQL

《慢sql提前分析预警和动态sql替换-Mybatis-SQL》为防止慢SQL问题而开发的MyBatis组件,该组件能够在开发、测试阶段自动分析SQL语句,并在出现慢SQL问题时通过Ducc配置实现动... 目录背景解决思路开源方案调研设计方案详细设计使用方法1、引入依赖jar包2、配置组件XML3、核心配

Qt把文件夹从A移动到B的实现示例

《Qt把文件夹从A移动到B的实现示例》本文主要介绍了Qt把文件夹从A移动到B的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录如何移动一个文件? 如何移动文件夹(包含里面的全部内容):如何删除文件夹:QT 文件复制,移动(

Python重命名文件并移动到对应文件夹

《Python重命名文件并移动到对应文件夹》在日常的文件管理和处理过程中,我们可能会遇到需要将文件整理到不同文件夹中的需求,下面我们就来看看如何使用Python实现重命名文件并移动到对应文件夹吧... 目录检查并删除空文件夹1. 基本需求2. 实现代码解析3. 代码解释4. 代码执行结果5. 总结方法补充在

我在移动打工的日志

客户:给我搞一下录音 我:不会。不在服务范围。 客户:是不想吧 我:笑嘻嘻(气笑) 客户:小姑娘明明会,却欺负老人 我:笑嘻嘻 客户:那我交话费 我:手机号 客户:给我搞录音 我:不会。不懂。没搞过。 客户:那我交话费 我:手机号。这是电信的啊!!我这是中国移动!! 客户:我不管,我要充话费,充话费是你们的 我:可是这是移动!!中国移动!! 客户:我这是手机号 我:那又如何,这是移动!你是电信!!

用Unity2D制作一个人物,实现移动、跳起、人物静止和动起来时的动画:中(人物移动、跳起、静止动作)

上回我们学到创建一个地形和一个人物,今天我们实现一下人物实现移动和跳起,依次点击,我们准备创建一个C#文件 创建好我们点击进去,就会跳转到我们的Vision Studio,然后输入这些代码 using UnityEngine;public class Move : MonoBehaviour // 定义一个名为Move的类,继承自MonoBehaviour{private Rigidbo

轨迹规划-B样条

B样条究竟是干啥的?白话就是给出一堆点,用样条的方式,给这些点连接起来,并保证丝滑的。 同时B样条分为准均匀和非均匀,以下为准均匀为例。 参考链接1:https://zhuanlan.zhihu.com/p/50626506https://zhuanlan.zhihu.com/p/50626506 参考链接2: https://zhuanlan.zhihu.com/p/536470972h

简单的角色响应鼠标而移动

actor类 //处理移动距离,核心是找到角色坐标在世界坐标的向量的投影(x,y,z),然后在世界坐标中合成,此CC是在地面行走,所以Y轴投影始终置为0; using UnityEngine; using System.Collections; public class actor : MonoBehaviour { public float speed=0.1f; CharacterCo

Unity3D 运动之Move函数和translate

CharacterController.Move 移动 function Move (motion : Vector3) : CollisionFlags Description描述 A more complex move function taking absolute movement deltas. 一个更加复杂的运动函数,每次都绝对运动。 Attempts to