概述
介绍
还在学习物理知识中,代码可能出现一些BUG。
代码
using UnityEngine;
public class ZeroPhysicsHelper : MonoBehaviour
{
private static Vector3 GetPosX(Vector3 vX, float t)
{
return vX * t;
}
private static Vector3 GetPosY(Vector3 vY, Vector3 g, float t)
{
return vY * t + 0.5f * Mathf.Pow(t, 2) * g;
}
/// <summary>
/// 根据'速度'计算'位移'
/// </summary>
/// <param name="startPos"></param>
/// <param name="velocity"></param>
/// <param name="g"></param>
/// <param name="t"></param>
/// <returns></returns>
public static Vector3 GetPos(Vector3 startPos, Vector3 velocity, Vector3 g, float t)
{
var upNormal = -g.normalized;
var vX = Vector3.ProjectOnPlane(velocity, upNormal);
var vY = Vector3.Project(velocity, upNormal);
return startPos + GetPosX(vX, t) + GetPosY(vY, g, t);
}
/// <summary>
/// 根据'最高高度'计算'起跳速度'
/// </summary>
/// <param name="g"></param>
/// <param name="height"></param>
/// <returns></returns>
public static Vector3 GetTakeoffVelocityByHeight(Vector3 g, float height)
{
return -g.normalized * Mathf.Sqrt(2f * g.magnitude * height);
}
/// <summary>
/// 根据'时间'计算'发射速度'
/// </summary>
/// <param name="startPos"></param>
/// <param name="targetPos"></param>
/// <param name="g"></param>
/// <param name="t"></param>
/// <returns></returns>
public static Vector3 GetLaunchVelocityByT(Vector3 startPos, Vector3 targetPos, Vector3 g, float t)
{
var upNormal = -g.normalized;
var toTarget = targetPos - startPos;
var targetPosXZ = Vector3.ProjectOnPlane(toTarget, upNormal);
var targetPosY = Vector3.Project(toTarget, upNormal);
var vX = targetPosXZ / t;
var vY = (targetPosY - 0.5f * Mathf.Pow(t, 2) * g) / t;
return vX + vY;
}
/// <summary>
/// 根据'发射角度'计算'发射速度'
/// </summary>
/// <param name="startPos"></param>
/// <param name="targetPos"></param>
/// <param name="g"></param>
/// <param name="angel"></param>
/// <returns></returns>
public static Vector3 GetLaunchVelocityByAngel(Vector3 startPos, Vector3 targetPos, Vector3 g, float angel)
{
var upNormal = -g.normalized;
var projectileXZPos = Vector3.ProjectOnPlane(startPos, upNormal);
var targetXZPos = Vector3.ProjectOnPlane(targetPos, upNormal);
var toTargetXZ = targetXZPos - projectileXZPos;
var forwardNormal = toTargetXZ.normalized;
var r = toTargetXZ.magnitude;
var tanAlpha = Mathf.Tan(angel * Mathf.Deg2Rad);
var h = targetPos.y - startPos.y;
var xSpeed = Mathf.Sqrt(-g.magnitude * r * r / (2.0f * (h - r * tanAlpha)));
var ySpeed = tanAlpha * xSpeed;
var vX = xSpeed * forwardNormal;
var vY = ySpeed * upNormal;
return vX + vY;
}
}
最后
以上就是想人陪小蝴蝶为你收集整理的[Unity]计算抛体运动轨迹(抛物线)与发射速度介绍代码的全部内容,希望文章能够帮你解决[Unity]计算抛体运动轨迹(抛物线)与发射速度介绍代码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复