概述
Kinematic Equations
- s = x + y 2 t s = frac{x+y}{2}t s=2x+yt
- v = u + a t v = u + at v=u+at
- s = u t + a t 2 2 s = ut + frac{at^2}{2} s=ut+2at2
- s = v t − a t 2 2 s = vt - frac{at^2}{2} s=vt−2at2
- v 2 = u 2 + 2 a s v^2 = u^2 + 2as v2=u2+2as
代码
- 原始代码:已知抛物线的最大高度,A、B 两点的(x,y,z)坐标,重力值,计算初速度、运动到目标点的时间。在 Unity 中实现可视化,绘制预测曲线,并在控制台中输出计算结果。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallLauch : MonoBehaviour
{
public Rigidbody ball;
public Transform target;
public float h;
public float gravity = -18;
public bool debugPath;
// Start is called before the first frame update
void Start()
{
ball.useGravity = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Lauch();
}
if (debugPath)
{
DrawPath();
}
}
void Lauch()
{
Physics.gravity = Vector3.up * gravity;
ball.useGravity = true;
ball.velocity = CalculateLaunchData().initialVelocity;
print(CalculateLaunchData().initialVelocity);
print(CalculateLaunchData().timeToTarget);
}
LaunchData CalculateLaunchData()
{
float displacementY = target.position.y - ball.position.y;
Vector3 displacementXZ = new Vector3(target.position.x - ball.position.x, 0, target.position.z - ball.position.z);
float time = Mathf.Sqrt(-2 * h / gravity) + Mathf.Sqrt(2 * (displacementY - h) / gravity);
Vector3 velocityY = Vector3.up * Mathf.Sqrt(-2 * gravity * h);
Vector3 velocityXZ = displacementXZ / time;
return new LaunchData( velocityXZ + velocityY * -Mathf.Sign(gravity), time);
}
void DrawPath()
{
LaunchData launchData = CalculateLaunchData();
Vector3 previousDrawPoint = ball.position;
int resolution = 30;
for (int i = 1; i <= resolution; i++)
{
float simulationTime = i / (float)resolution * launchData.timeToTarget;
Vector3 displacement = launchData.initialVelocity * simulationTime + Vector3.up * gravity * simulationTime * simulationTime / 2f;
Vector3 drawPoint = ball.position + displacement;
Debug.DrawLine(previousDrawPoint, drawPoint, Color.green);
previousDrawPoint = drawPoint;
}
}
struct LaunchData
{
public readonly Vector3 initialVelocity;
public readonly float timeToTarget;
public LaunchData(Vector3 initialVelocity, float tiemToTarget)
{
this.initialVelocity = initialVelocity;
this.timeToTarget = tiemToTarget;
}
}
}
- 对原始代码修改,已知初速度、
发射角(不知是否为必要条件,之后验证),计算抛物线的最大高度,到达目标点的时间,并绘制出轨迹。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletLauch : MonoBehaviour
{
public Rigidbody bullet;
public Transform target;
public float initialVelocity;
public float launchAngle;
public float gravity;
private float h;
private float time;
public bool debugPath;
// Start is called before the first frame update
void Start()
{
bullet.useGravity = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Launch();
}
if (debugPath)
{
DrawPath();
}
}
void Launch()
{
Physics.gravity = Vector3.up * gravity;
bullet.useGravity = true;
bullet.velocity = CalculateLaunchData().initialVelocity;
print(CalculateLaunchData().initialVelocity);
print(CalculateLaunchData().time);
print(CalculateLaunchData().h);
}
void DrawPath()
{
LaunchData launchData = CalculateLaunchData();
Vector3 previousDrawPoint = bullet.position;
int resolution = 30;
for (int i = 1; i <= resolution; i++)
{
float simulationTime = i / (float)resolution * launchData.time;
Vector3 displacement = launchData.initialVelocity * simulationTime + Vector3.up * gravity * simulationTime * simulationTime / 2f;
Vector3 drawPoint = bullet.position + displacement;
Debug.DrawLine(previousDrawPoint, drawPoint, Color.green);
previousDrawPoint = drawPoint;
}
}
LaunchData
CalculateLaunchData()
{
float displacementY = target.position.y - bullet.position.y;
Vector3 displacementXZ = new Vector3(target.position.x - bullet.position.x, 0, target.position.z - bullet.position.z);
float initialVelocityY = initialVelocity * Mathf.Sin(Mathf.Deg2Rad * launchAngle);
h = -(initialVelocityY * initialVelocityY) / (2 * gravity);
time = -initialVelocityY / gravity + Mathf.Sqrt(2 * (displacementY - h) / gravity);
Vector3 initialVelocityXZ = displacementXZ / time;
return new LaunchData( initialVelocityXZ + Vector3.up * initialVelocityY, time, h);
}
struct LaunchData
{
public readonly Vector3 initialVelocity;
public readonly float time;
public readonly float h;
public LaunchData(Vector3 initialVelocity, float time, float h)
{
this.initialVelocity = initialVelocity;
this.time = time;
this.h = h;
}
}
}
修改后的代码仍需优化。
最后
以上就是激情超短裙为你收集整理的【Unity3D】抛物线运动方程原理和实现的全部内容,希望文章能够帮你解决【Unity3D】抛物线运动方程原理和实现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复