通过起点,终点和高度计算3D轨迹 [英] Calculate a 3D trajectory by start point, end point and height
问题描述
我已经想出了如何使用起点和角度绘制3D轨迹的方法.但是,我正在尝试从起点,终点和高度开始绘制轨迹.
I've already figured out how to make a 3D trajectory using a start point and an angle. However, I am trying to make a trajectory from a start point, an end point, and a height.
我尝试在3D空间中的2D平面上采用抛物线的方法.我计算了Prabola的A,B和C值以及在抛物线上给定3个点的平面上.但是,我在进行这种计算时遇到了一些麻烦,我认为这与无法正确计算没有平面的Z轴有关,但我无法分辨.
I tried taking the approach of a parabola on a 2D plane in a 3D space. I calculated the Prabola's A, B, and C values as well as the plane it's on given 3 points on the Parabola. However, I've had a few complications with this sort of calculation, I assume it has to do with the inability to properly calculate a Z-axis without a plane but I cannot tell.
除了平面上的2D抛物线外,谷歌没有提供其他可能的答案,而3D轨迹使用起点,角度和乘数产生了公式.
Other than a 2D parabola on a plane google did not provide another possible answer and a 3D trajectory yields a formula using a start point, an angle, and a power multiplier.
- 是否可以通过给定起点,终点和高度来计算3D轨迹?
感谢您的帮助
修改:
我的代码使用3点来计算抛物线(以防有人想知道我是如何做到的,也许是要解决我做错的事情)
My code to calculate a parabola using 3 points (in case someone would like to know how I've done that and perhaps fix what I've done wrong)
public Parabola(Vector3 pa, Vector3 pb, Vector3 pc)
{
this.pa = pa;
this.pc = pc;
float a1 = -pa.x * pa.x + pb.x * pb.x, b1 = -pa.x + pb.x, c1 = -pa.y + pb.y;
float a2 = -pb.x * pb.x + pc.x * pc.x, b2 = -pb.x + pc.x, c2 = -pb.y + pc.y;
float bm = -(b2 / b1), a3 = bm * a1 + a2, c3 = bm * c1 + c2;
float a = c3 / a3, b = (c1 - a1 * a) / b1, c = pa.y - a * pa.x * pa.x - b * pa.x;
this.a = a; this.b = b; this.c = c;
plane = Vector3.Cross(pb - pa, pc - pa);
}
public Vector3 GetPoint(float x)
{
float angle = Mathf.Atan2(pc.z - pa.z, pc.x - pa.x) * Mathf.Rad2Deg;
float xs = Mathf.Cos(angle * Mathf.Deg2Rad) * x, zs = Mathf.Sin(angle * Mathf.Deg2Rad) * x;
return new Vector3(xs, a * x * x + b * x + c, zs);
}
public Vector3 ProjectOn(float x) => Vector3.ProjectOnPlane(GetPoint(x), plane);
仅在2轴而不是3轴上时,结果看起来还可以.这是2张图片进行演示:
The result looks ok when it's only on 2 Axis, but not 3. here are 2 images for demonstration:
推荐答案
我找到了答案,但这是一种解决方法.
I found an answer, but it's kinda a workaround.
在搞乱3D中的Parabolas之前,我搞砸了3D中的线性方程.与抛物线不同,线即使在3D中也具有定义的方程式( Pn = P0 + t x V )(包含XYZ的Pn向量,包含XYZ的P0初始点,t float,V Vector3)
Before messing around with Parabolas in 3D, I messed around with linear equations in 3D. Unlike parabolas, lines have a defined equation even in 3D(Pn = P0 + t x V)(Pn vector containing XYZ, P0 initial point containing XYZ, t float, V Vector3)
此外,即使在3D模式下,也只有一个线穿过2点.
In addition, there's only ONE line that goes through 2 points, even in 3D.
我用它来制作一条由2个点和一个高度组成的轨迹.我在这两个点的中心创建了一个新点,并将高度值添加到这些点的最高Y值,从而创建了Apex.
I used that to make a trajectory that's made out of 2 points and a height. I make a new point in the center of those two points and add the height value to the highest Y value of the points, thus creating an Apex.
然后,我使用与以前相同的计算方法来计算A,B和C值,具有这3点的抛物线就可以了.
then I use the same calculations as before to calculate the A, B, and C values that a parabola with those 3 points would have had.
我制作了一种方法,该方法吸收X值并返回包含该X在线性方程式上的点的Vector3,但改为根据抛物线方程式更改向量的Y值.
I made a method that takes in an X value and returns a Vector3 containing the point this X is on a linear equation, but instead, changing the vector's Y value based on the parabola's equation.
实际上创建了一条高架线,我制作了看起来和行为都非常像3D抛物线的东西.
Practically creating an elevated line, I made something that looks and behaves perfectly like a 3D parabola.
如果您使用的是C#,下面是代码(图片):
If you're in C#, here is the code(images):
修复!
在Linear的GetX(float x)方法中.应该是:
in the Linear's GetX(float x) method. it should be:
public Vector3 GetX(float x) => => r0 + (x - r0.x)/v.x * v;
我在计算中犯了一个小错误,我立即注意到并进行了更改.
I made a slight mistake in the calculations which I noticed immediately and changed.
这篇关于通过起点,终点和高度计算3D轨迹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!