计算贝塞尔样条曲线以从一个点到另一个点
我在 X,Y + 旋转中有 2 个点,我需要计算平滑连接这 2 个点的贝塞尔样条线(二次贝塞尔曲线的集合)。 (见图)点代表游戏中只能缓慢旋转的单位。所以从A点到B点,必须走很长的路。附图显示了一条相当夸张的弯曲路径,但你明白了。
我可以使用什么公式来计算这样的贝塞尔样条线?
I have 2 points in X,Y + Rotation and I need to calculate a bezier spline (a collection of quadratic beziers) that connects these 2 points smoothly. (see pic) The point represents a unit in a game which can only rotate slowly. So to get from point A to B, it has to take a long path. The attached picture shows quite an exaggeratedly curvy path, but you get the idea.
What formulas can I use to calculate such a bezier spline?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
刚刚看到我误解了你的问题。你不能使用单个 三次 Hermite 样条 来代替,因为你有一个起点和终点以及两个方向(切线)?有任何额外的限制吗?
要计算起点和终点切线,只需使用起点和终点方向,并根据起点和终点之间的距离缩放它们(另外还有一些其他常数因子,例如 0.5,具体取决于您希望路径的弯曲程度)。:
我使用这个系统可以在我正在开发的游戏中插入相机路径,并且效果很好。
Just saw that I misunderstood your question. Couldn't you use a single cubic hermite splines instead since you have a start and end point and two directions (tangents)? Are there any additional constraints?
To calculate the start and end tangents just use the start and end direction and scale them with the distance between the start and end points (and additionally some other constant factor like 0.5 depending on how curvy you want the path to be).:
I use this system to interpolate camera paths in a game I'm working on and it works great.
您可能知道,即使我们假设有 2 个控制点,也有无限的解。
我发现 使用贝塞尔曲线的平滑算法,它回答了您的问题(请参阅开头的
Bx(t)
和By(t)
方程):Bx(t) = (1-t)3 P1x + 3(1-t)2 t P2x + 3 (1-t) t2 P3x + t3 P4x
B< sub>y(t) = (1-t)3 P1y + 3(1-t)2 t P2y + 3 (1-t) t2 P3y + t 3 P4y
P1 和 P4 是您的端点,P2 和P3 是控制点,您可以沿着所需的角度自由选择。如果控制点与点
(x, y)
的距离为r
,且沿角度 θ,则该点的坐标为:x' = x - r sin( θ)
y' = y - r cos(θ)
(根据您使用的坐标系 - 我认为我的符号是正确的)。唯一的免费参数是
r
,您可以根据需要选择。您可能想使用r = α dist(P1, P4)
且 α 1.
As you probably know, there are infinite solutions, even if we assume 2 control points.
I found Smoothing Algorithm Using Bézier Curves, which answers your question (see equations of
Bx(t)
andBy(t)
in the beginning):Bx(t) = (1-t)3 P1x + 3 (1-t)2 t P2x + 3 (1-t) t2 P3x + t3 P4x
By(t) = (1-t)3 P1y + 3 (1-t)2 t P2y + 3 (1-t) t2 P3y + t3 P4y
P1 and P4 are your endpoints, and P2 and P3 are the control points, which you're free to choose along the desired angles. If your control point is at a distance
r
along an angle θ from point(x, y)
, the coordinates of the point are:x' = x - r sin(θ)
y' = y - r cos(θ)
(according to the co-ordinate system you've used—I think I got the signs right). The only free parameter is
r
, which you can choose as you want. You probably want to user = α dist(P1, P4)
with α < 1.
我不记得我从哪里得到它,但我一直在使用这个:
其中
t
是沿起点和终点之间的路径的比率。I don't remember where I got it, but I have been using this:
where
t
is ratio along path between start and end points.