【发布时间】:2014-08-31 04:39:53
【问题描述】:
我正在尝试让角色以弧线形式向目标投掷东西。
我知道顶点(x,y)和目标(x,y),我想得到一条从原点(x,y)到目标的弧,最大高度为vertex.y
我所拥有的是基于 y = a(x-h)^2 + k 的顶点形式
public static Vector3 parabola(Vector2 origin, Vector2 target, float height)
{
float dist = target.x - origin.x;
Vector2 vertex = new Vector2(origin.x + (dist / 2), origin.y + height);
//a = (y-k) / (x-h)^2
float a = (target.y - vertex.y) / ((target.x - vertex.x) * (target.x - vertex.x));
//b = (-h + -h) * a
float b = (-vertex.x + -vertex.x) * a;
//c = (h * h) * a + k
float c = (vertex.x * vertex.x) * a + vertex.y;
return new Vector3(a, b, c);
}
x += Time.DeltaTime;
float yPos = a * ((x - h) * (x - h)) + k;
这不会产生正确的弧线。它通常太陡或太浅。我的代数是错的,还是我使用了错误的方法?
谢谢
【问题讨论】:
-
“我知道顶点(x,y)”是什么意思?和“vertex.y 的最大高度”?你真正想解决什么问题?您应该感兴趣的变量是您想要将弹丸从一个点发送到另一个点的起始位置、目标位置、初始速度、轨迹角和重力值 IF。您将知道开始/结束位置和重力值,您可以调整初始速度,然后您只需要计算出角度。这是假设游戏具有 2D 特性。另外,
Vector3如何描述抛物线?如果你澄清了这些问题,我会尽力解答。 -
这当然没有考虑任何阻力。 (来自空气等)
标签: c# unity3d algebra projectile