具有一定惯性的路径算法
我正在尝试开发一款游戏。
我有一个起点和起始向量(蓝色),接下来我在屏幕上绘制路径(黑色)我想以一定的惯性或有限的角度和步数跟随每一个转弯,这应该会导致一条红线。
您对如何编写此类算法有什么建议吗?
I’m trying to develop a game.
I have a starting point with and starting vector (blue), next I draw on screen the path (black) which I want to follow with a certain inertia, or limited angle and step each turn that should result a red line.
Do you have any tips on how to program such algorithm?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需创建一个微分方案,即离散时刻的模型速度和源点坐标。例如,假设您修复了一些
dt = 0.1 sec
,起始速度由蓝色矢量指定为v0
。我们从x0
开始。假设 y[j] 是黑色路径的点。
令 x1 = x0 + v1 * dt,其中 v1 = v0 + (y[k(x0)+1] - x0) * f(abs(y[k(x0)+1) ] - x_0))。哪里
k(x0)
是y[j]
中最接近x0
点的索引,f(x)
是一个函数,描述将您的轨迹拉至定义路径的“力”。它仅为非负x
es 定义。此模型将您的轨迹拉至定义路径中最接近轨迹上当前建模位置的下一个点。
f(x)
的一个很好的例子是对引力进行建模:f(x) = K/(x * x)
,其中K
code> 应该通过实验进行调整,以给出自然的期望结果。然后 x2 = x1 + v2 * dt,其中 v2 = v1 + (y[k(x1) + 1] - x1) * f(abs(y[k(x1) + 1) ] - x_1)) 等等:
x[n+1] = x[n] + v[n+1] * dt
,其中v[n+1 ] = v[n] + (y[k(x[n]) + 1] - x[n]) * f(abs(y[k(x[n])+1] - x[n]))...
您必须在此处调整
dt
和K
。dt
应足够小以使轨迹平滑。较大的K
使轨迹更加接近精确定义,较小的K
使其更加宽松。编辑现在实际上当我想了一下,我明白力函数
f
的选择不好,因为重力允许空间速度,即你的轨迹的能力如果初始速度太大,就会无限地飞离目标。因此,您应该构造另一个函数,可能只是类似于f(x) = K x
或f(x) = K x ^ alpha
的函数,其中阿尔法> 0 。你看,这个方案非常通用,所以你应该尝试一下。
You can just create a differential scheme, i.e. model speed and coordinate of the source point at discrete moments in time. Say you fix some
dt = 0.1 sec
for example, starting speed is given by blue vector asv0
. We start atx0
.Say
y[j]
are the points of the black path.Let
x1 = x0 + v1 * dt
, wherev1 = v0 + (y[k(x0)+1] - x0) * f(abs(y[k(x0)+1] - x_0))
. Wherek(x0)
is the index of the nearest tox0
point amongy[j]
,f(x)
is a function characterizing the 'force' pulling your trajectory to that of the defined path. It defined for nonnegativex
es only.This models pulling your trajectory to the next point in the defined path to that closest to current modeled position on the trajectory.
A good example of
f(x)
could be one modeling the gravitational force:f(x) = K/(x * x)
, whereK
should be adjusted experimentally to be giving natural desired results.Then
x2 = x1 + v2 * dt
, wherev2 = v1 + (y[k(x1) + 1] - x1) * f(abs(y[k(x1) + 1] - x_1))
and so on:x[n+1] = x[n] + v[n+1] * dt
, wherev[n+1] = v[n] + (y[k(x[n]) + 1] - x[n]) * f(abs(y[k(x[n])+1] - x[n]))
...You'll have to adjust
dt
andK
here.dt
should be small enough for the trajectory to be smooth. BiggerK
makes trajectory more close to defined precisely, smallerK
makes it more relaxed.Edit now actually when I thought a little bit, I understand that selection of the force function
f
was not good, as gravitational force allows space velocities, i.e. ability for your trajectory to fly away from the desired one infinitely if the initial speed is too big. So you should construct another function, possibly just something along the lines off(x) = K x
orf(x) = K x ^ alpha
, wherealpha > 0
. You see, this scheme is quite general, so you should experiment.另一种选择是做这样的事情......
这可能会产生相当合适的行为。我会讲一个例子,但这会相当乏味。请注意,这与 unkulunkulu 概述的方法类似,但在方法方面略有不同。
Another option would be to do something like this...
This could make for fairly appropriate behavior. I'd walk through an example, but this it would be fairly tedious. Note that this is similar to the method unkulunkulu outlines, but a little different in terms of an approach.