物理模拟重力 斜抛运动计算 抛物线计算
一、介绍
模拟Unity原始重力系统进行重写,可是实现发射到指定目标位置并能继续当前力进行自身的弹力与摩擦继续运动
二、原理
将Unity原始不受控制的物理系统使用一个模拟重力的方式进行斜抛等操作,在其落地瞬间将模拟的重力还原给Unity的原始重力,从而达到可任意抛物线移动的物理系统
三、实现如下
PhysicsUtil.cs 工具类
using UnityEngine;/// <summary> 物理计算工具
/// <para>ZhangYu 2018-05-10</para>
/// </summary>
public static class PhysicsUtil
{/**findInitialVelocity* Finds the initial velocity of a projectile given the initial positions and some offsets* @param Vector3 startPosition - the starting position of the projectile* @param Vector3 finalPosition - the position that we want to hit* @param float maxHeightOffset (default=0.6f) - the amount we want to add to the height for short range shots. We need enough clearance so the* ball will be able to get over the rim before dropping into the target position* @param float rangeOffset (default=0.11f) - the amount to add to the range to increase the chances that the ball will go through the rim* @return Vector3 - the initial velocity of the ball to make it hit the target under the current gravity force.* * Vector3 tt = findInitialVelocity (gameObject.transform.position, target.transform.position);Rigidbody rigidbody = gameObject.GetComponent<Rigidbody> ();Debug.Log (tt);rigidbody.AddForce(tt*rigidbody.mass,ForceMode.Impulse);*/public static Vector3 GetParabolaInitVelocity(Vector3 from, Vector3 to, float gravity = 9.8f, float heightOff = 0.0f, float rangeOff = 0.11f){// get our return value ready. Default to (0f, 0f, 0f)Vector3 newVel = new Vector3();// Find the direction vector without the y-component/// /找到未经y分量的方向矢量//Vector3 direction = new Vector3(to.x, 0f, to.z) - new Vector3(from.x, 0f, from.z);// Find the distance between the two points (without the y-component)//发现这两个点之间的距离(不y分量)//float range = direction.magnitude;// Add a little bit to the range so that the ball is aiming at hitting the back of the rim.// Back of the rim shots have a better chance of going in.// This accounts for any rounding errors that might make a shot miss (when we don't want it to).range += rangeOff;// Find unit direction of motion without the y componentVector3 unitDirection = direction.normalized;// Find the max height// Start at a reasonable height above the hoop, so short range shots will have enough clearance to go in the basket// without hitting the front of the rim on the way up or down.float maxYPos = to.y + heightOff;// check if the range is far enough away where the shot may have flattened out enough to hit the front of the rim// if it has, switch the height to match a 45 degree launch angle//if (range / 2f > maxYPos)// maxYPos = range / 2f;if (maxYPos < from.y)maxYPos = from.y;// find the initial velocity in y direction/// /发现在y方向上的初始速度//float ft;ft = -2.0f * gravity * (maxYPos - from.y);if (ft < 0) ft = 0f;newVel.y = Mathf.Sqrt(ft);// find the total time by adding up the parts of the trajectory// time to reach the max//发现的总时间加起来的轨迹的各部分////时间达到最大//ft = -2.0f * (maxYPos - from.y) / gravity;if (ft < 0)ft = 0f;float timeToMax = Mathf.Sqrt(ft);// time to return to y-target//时间返回到y轴的目标//ft = -2.0f * (maxYPos - to.y) / gravity;if (ft < 0)ft = 0f;float timeToTargetY = Mathf.Sqrt(ft);// add them up to find the total flight time//把它们加起来找到的总飞行时间//float totalFlightTime;totalFlightTime = timeToMax + timeToTargetY;// find the magnitude of the initial velocity in the xz direction/// /查找的初始速度的大小在xz方向//float horizontalVelocityMagnitude = range / totalFlightTime;// use the unit direction to find the x and z components of initial velocity//使用该单元的方向寻找初始速度的x和z分量//newVel.x = horizontalVelocityMagnitude * unitDirection.x;newVel.z = horizontalVelocityMagnitude * unitDirection.z;return newVel;}/// <summary> 计算抛物线物体在下一帧的位置 </summary>/// <param name="position">初始位置</param>/// <param name="velocity">移动速度</param>/// <param name="gravity">重力加速度</param>/// <param name="time">飞行时间</param>/// <returns></returns>public static Vector3 GetParabolaNextPosition(Vector3 position, Vector3 velocity, float gravity, float time){velocity.y += gravity * time;return position + velocity * time;}}
Missile.cs
using UnityEngine;/// <summary>
/// 抛物线导弹
/// <para>计算弹道和转向</para>
/// <para>ZhangYu 2019-02-27</para>
/// </summary>
public class Missile : MonoBehaviour
{public Transform target; // 目标public float hight = 16f; // 抛物线高度public float gravity = -9.8f; // 重力加速度private Vector3 position; // 我的位置private Vector3 dest; // 目标位置private Vector3 velocity; // 运动速度private float time = 0; // 运动时间private void Start(){dest = target.position;position = transform.position;velocity = PhysicsUtil.GetParabolaInitVelocity(position, dest, gravity, hight, 0);transform.LookAt(PhysicsUtil.GetParabolaNextPosition(position, velocity, gravity, Time.deltaTime));}private void Update(){// 计算位移float deltaTime = Time.deltaTime;position = PhysicsUtil.GetParabolaNextPosition(position, velocity, gravity, deltaTime);transform.position = position;time += deltaTime;velocity.y += gravity * deltaTime;// 计算转向transform.LookAt(PhysicsUtil.GetParabolaNextPosition(position, velocity, gravity, deltaTime));// 简单模拟一下碰撞检测if (position.y <= dest.y){if (Vector3.Distance(transform.position,target.position)>= 2) return;GetComponent<Rigidbody>().useGravity = true;GetComponent<Rigidbody>().velocity = velocity;enabled = false;};}}
四、资源分享
CSDN下载链接
淘宝插件链接
在我的资源中搜索 PhysicsMissile
物理模拟重力 斜抛运动计算 抛物线计算相关推荐
- matlab模拟斜抛运动60,大学物理教学改革论文,关于大学物理教学方法改革-Matlab的妙用相关参考文献资料-免费论文范文...
导读:本文是一篇关于大学物理教学改革论文范文,可作为相关选题参考,和写作参考文献. (1.长江师范学院大学物理教研室 重庆 408100, 2.内蒙古工业大学 理学院物理系 内蒙古呼和浩特 01005 ...
- Python + matplotlib.animation 模拟斜抛运动动画(含完整代码)
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 Abstract Introduction Matplotlib.animation Physics model and C ...
- 三维场景中斜抛运动顶点的生成
三维场景中斜抛运动顶点的生成 1 算法思想-斜抛运动 2 代码 3 参考文献 1 算法思想-斜抛运动 2 代码 void getparabola_vertex_2(glm::vec3 _Point, ...
- Unity 斜抛运动 路径点
1.截图 2.代码介绍 使用Unity自带Rigidbody刚体插件. 代码比较简单:使用Unity的 Rigidbody.velocity 进行位移(不使用AddForce). private ...
- C语言编码小球斜抛运动,利用C4droid绘制小球斜抛运动轨迹(考虑空气阻力)
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 我把源代码分享出来,欢迎有兴趣的朋友下载测试,修改优化. /*********************************************** ...
- HDOJ 5166 -----斜抛运动
题意:一个人站在H高的地方斜向上抛小球,求出小球落地后的水平距离. 算法思想: 1.计算出小球的最大滞空时间 2.计算出小球的水平速度 3.s=Vx*t; 推导来自小岛: 代码如下: #include ...
- 斜抛运动的最远射程问题
问题概述: 在o点上方高度为h处以速度v抛出一物体,该物体运动一段时间后落到地面p处,问抛出方向与水平方向的夹角是多少时,op有最大值,最大值是多少? 当 时: op有最大值: 题目:迎风舞
- 斜抛运动的最大水平射程
1.抛出点与落地点在同一水平面:在这种情况下,当抛出角为 45度时,水平射程最大,其值为: 2.抛出点与落地点不在同一水平面上:当抛出角等于 arctg(v0/vt)时,水平射程最大,其值为:v0*v ...
- matlab 斜抛 空气阻力,运用MATLAB对运动学、动力学问题进行过程分析
第 29 卷第 6 期 Vol. 29 NO. 6 重庆工商大学学报( 自然科学版) J Chongqing Technol Business Univ. ( Nat Sci Ed) 2012 年 6 ...
- 电能减去热能计算机械能,力与运动机械能热能计算题
<力与运动机械能热能计算题>由会员分享,可在线阅读,更多相关<力与运动机械能热能计算题(19页珍藏版)>请在人人文库网上搜索. 1.1.(2015益阳)如图甲所示,重500N的 ...
最新文章
- 常见蛋白质种类_什么是优质蛋白质?鸡蛋大豆算不算
- 前端常见知识点四之webscoket
- pip 设置超时时间_pip install 超时
- SAP ABAP 编程语言里的%_c_pointer代表什么
- 百度图床带数据库版本源码支持获取外链
- idea中artifacts、facets、modules是什么意思?
- 公交/地铁出行测试点
- 重写DEV的DateEdit控件的类只选择年月
- 微信公众号开发笔记2
- Atitit.执行cmd 命令行 php
- 中标麒麟V7安装Qt5.13.0
- 最新PHP超精简全站自适应小说网网站源码
- 分享一个不错的个人简历模板下载网站(word文件可编辑的)
- 19、ADS使用记录之窄带F类功放设计
- K9G8G08U0A升级到K9GAG08U0D烧录NK要注意的地方
- 手机连接ftp文件服务器,手机链接ftp服务器地址
- 从伊朗虚假新闻活动案例来看国外造谣及传谣模式
- 电脑问题处理篇4:解决华硕电脑“ASUS Battery Health Charging”软件界面呈现灰色不能选择充电模式的问题
- 万国数据表现不佳的风险很高
- 深度学习和并行化实现
热门文章
- matlab2014如何获得hostid,关于如何修改hostid的问题
- STM32旋转立方体
- 流媒体有哪些播放方式?流媒体视频三种播放方式介绍
- Dapper Object must implement IConvertible.
- 测序技术的一些原理理解(sanger测序与illumina测序)
- Linux 基础之基础网络ss命令
- Ps(Adobephoto shop)当中布尔运算的使用方法
- PowerDNS Authoritative Server 3.3 发布
- 接入与身份认证技术概述
- 傻傻分不清桃花,杏花,樱花,梨花,李花,海棠花?