具有一定惯性的路径算法

发布于 2024-11-27 12:11:39 字数 188 浏览 0 评论 0原文

我正在尝试开发一款游戏。

图像演示概念

我有一个起点和起始向量(蓝色),接下来我在屏幕上绘制路径(黑色)我想以一定的惯性或有限的角度和步数跟随每一个转弯,这应该会导致一条红线。

您对如何编写此类算法有什么建议吗?

I’m trying to develop a game.

image demonstrating concept

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城之時有危險 2024-12-04 12:11:39

您只需创建一个微分方案,即离散时刻的模型速度和源点坐标。例如,假设您修复了一些 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) 是一个函数,描述将您的轨迹拉至定义路径的“力”。它仅为非负xes 定义。

此模型将您的轨迹拉至定义路径中最接近轨迹上当前建模位置的下一个点。

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]))...

您必须在此处调整dtKdt 应足够小以使轨迹平滑。较大的K使轨迹更加接近精确定义,较小的K使其更加宽松。

编辑现在实际上当我想了一下,我明白力函数f的选择不好,因为重力允许空间速度,即你的轨迹的能力如果初始速度太大,就会无限地飞离目标。因此,您应该构造另一个函数,可能只是类似于 f(x) = K xf(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 as v0. We start at x0.
Say y[j] are the points of the black path.

Let x1 = x0 + v1 * dt, where v1 = v0 + (y[k(x0)+1] - x0) * f(abs(y[k(x0)+1] - x_0)). Where
k(x0) is the index of the nearest to x0 point among y[j],
f(x) is a function characterizing the 'force' pulling your trajectory to that of the defined path. It defined for nonnegative xes 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), where K should be adjusted experimentally to be giving natural desired results.

Then x2 = x1 + v2 * dt, where v2 = 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, where v[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 and K here. dt should be small enough for the trajectory to be smooth. Bigger K makes trajectory more close to defined precisely, smaller K 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 of f(x) = K x or f(x) = K x ^ alpha, where alpha > 0. You see, this scheme is quite general, so you should experiment.

倦话 2024-12-04 12:11:39

另一种选择是做这样的事情......

  1. Compute the average value of k points in the target path
     to get (<x>,<y>).
  2, Compte the angle between the most recent point in the path
     and (<x>,<y>) and turn that way; if the angle is too big,
     turn as hard as possible.
  3. Recompute (<x>,<y>) for the next set of k elements by sliding
     the window by 1; repeat step 2.

这可能会产生相当合适的行为。我会讲一个例子,但这会相当乏味。请注意,这与 unkulunkulu 概述的方法类似,但在方法方面略有不同。

Another option would be to do something like this...

  1. Compute the average value of k points in the target path
     to get (<x>,<y>).
  2, Compte the angle between the most recent point in the path
     and (<x>,<y>) and turn that way; if the angle is too big,
     turn as hard as possible.
  3. Recompute (<x>,<y>) for the next set of k elements by sliding
     the window by 1; repeat step 2.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文