物理学飞船的2D轨迹规划 [英] 2d trajectory planning of a spaceship with physics

查看:250
本文介绍了物理学飞船的2D轨迹规划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实施的空间与船舶2D游戏。

I'm implementing a 2D game with ships in space.

为了做到这一点,我使用的是爱情,它包装的Box2D和Lua。但我相信,我的问题可以由任何人使用物理比我更深入的了解来回答 - 所以伪code被接受作为响应

In order to do it, I'm using LÖVE, which wraps Box2D with Lua. But I believe that my question can be answered by anyone with a greater understanding of physics than myself - so pseudo code is accepted as a response.

我的问题是,我不知道如何正确地移动我的飞船上的2D物理功能的世界。更具体地说:

My problem is that I don't know how to move my spaceships properly on a 2D physics-enabled world. More concretely:

船舶质量 M 位于初始位置 {X,Y} 。它有一个初始速度矢量 {VX,VY} (可 {0,0} )。

A ship of mass m is located at an initial position {x, y}. It has an initial velocity vector of {vx, vy} (can be {0,0}).

我们的目标是在 A点{XO哟} 。该船具有到达具有速度的目的 {VXO,vyo} (或附近),按照最短的轨迹。

The objective is a point in {xo,yo}. The ship has to reach the objective having a velocity of {vxo, vyo} (or near it), following the shortest trajectory.

有一个名为更新(DT)功能被频繁调用(即每秒30次)。在此功能,该船可以通过应用冲动,以自身修改其位置和轨迹。的脉冲的幅度是二进制:你可以把它应用在给定的方向,或者不应用它的话)。在code,它看起来是这样的:

There's a function called update(dt) that is called frequently (i.e. 30 times per second). On this function, the ship can modify its position and trajectory, by applying "impulses" to itself. The magnitude of the impulses is binary: you can either apply it in a given direction, or not to apply it at all). In code, it looks like this:

function Ship:update(dt)
  m = self:getMass()
  x,y = self:getPosition()
  vx,vy = self:getLinearVelocity()
  xo,yo = self:getTargetPosition()
  vxo,vyo = self:getTargetVelocity()
  thrust = self:getThrust()

  if(???)
    angle = ???
    self:applyImpulse(math.sin(angle)*thrust, math.cos(angle)*thrust))
  end
end

第一个 ??? 是那里表明,在某些情况下(我猜),这将是更好的不要冲动,离开船漂 。第二个 ??? 部分包括如何计算在一个给定的DT冲动角度。

The first ??? is there to indicate that in some occasions (I guess) it would be better to "not to impulse" and leave the ship "drift". The second ??? part consists on how to calculate the impulse angle on a given dt.

我们在太空中,因此我们可以忽略的东西像空气摩擦。

We are in space, so we can ignore things like air friction.

虽然这将是非常好的,我不会找人$ C C此$我;我把code有,所以我的问题是清楚的理解。

Although it would be very nice, I'm not looking for someone to code this for me; I put the code there so my problem is clearly understood.

我需要的是一个战略 - 这种攻击的一种方式。我知道一些基本的物理学,但我不是专家。例如,就这个问题有名字吗?诸如此类的事情。

What I need is an strategy - a way of attacking this. I know some basic physics, but I'm no expert. For example, does this problem have a name? That sort of thing.

多谢了。

编辑:测试版提供了一个有效的战略,这一点,并恳请法官直接在爱情方面实现了它,在评论

Beta provided a valid strategy for this and Judge kindly implemented it directly in LÖVE, in the comments.

EDIT2:经过谷歌搜索我也发现 openSteer 。这是在C ++中,但它确实是我pretended。这可能会有助于达到人这个问题。

After more googling I also found openSteer. It's on C++, but it does what I pretended. It will probably be helpful to anyone reaching this question.

推荐答案

这就是所谓的运动规划,和它的不平凡。

It's called motion planning, and it's not trivial.

下面是一个简单的方式来获得非最佳轨迹:

Here's a simple way to get a non-optimal trajectory:

  1. 停止。适用推力相反的速度的方向,直到速度为零。
  2. 计算的最后一站,这将是第一个相反的,从开始即得到该船X0和V0稳步推进。起点将在距离|从X0 ^ 2 /(2 *推力)| V0。
  3. 获取到起点(然后进行最后一站)。获取从一个站立一点到另一点很简单:向推,直到你已经完成一半了,然后向后推,直到你停止。
  1. Stop. Apply thrust opposite to the direction of velocity until velocity is zero.
  2. Calculate the last leg, which will be the opposite of the first, a steady thrust from a standing start that gets the ship to x0 and v0. The starting point will be at a distance of |v0|^2/(2*thrust) from x0.
  3. Get to that starting point (and then make the last leg). Getting from one standing point to another is easy: thrust toward it until you're halfway there, then thrust backward until you stop.

如果你想快速和肮脏的方法来最佳的弹道,你可以使用迭代的方法:先从非最佳方法,以上;这是推力角只是一个时间序列。现在尝试做该序列的变化不大,保持了接近目标序列群体。拒绝最坏的打算,实验用最好的 - 如果你觉得大胆你可以把这个遗传算法 - 而且运气好的话它会开始圆边角

If you want a quick and dirty approach to an optimal trajectory, you could use an iterative approach: Start with the non-optimal approach, above; that's just a time sequence of thrust angles. Now try doing little variations of that sequence, keeping a population of sequences that get close to the goal. reject the worst, experiment with the best -- if you're feeling bold you could make this a genetic algorithm -- and with luck it will start to round the corners.

如果你想确切的答案,用变分法。我会采取裂缝在那,如果我成功了,我会在这里发表了答案。

If you want the exact answer, use the calculus of variations. I'll take a crack at that, and if I succeed I'll post the answer here.

编辑:下面是一个简单的问题的精确解

Here's the exact solution to a simpler problem.

假设,而不是一个推力,我们可以指向任何方向,我们有四个固定的推进器指向的{+ X + Y,-X,-Y}方向。在任何特定时间,我们将击发至多一个+/- X和至多一个+/-的Y(有在烧成+ X和-X没有点在同一时间)。所以,现在的X和Y的问题是独立的(它们不是在最初的问题,因为推力必须X和Y之间共享)。我们现在必须解决的一维问题 - 并申请了两次

Suppose instead of a thrust that we can point in any direction, we have four fixed thrusters pointing in the {+X, +Y, -X, -Y} directions. At any given time we will firing at most one of the +/-X and at most one of the +/-Y (there's no point in firing +x and -X at the same time). So now the X and Y problems are independent (they aren't in the original problem because thrust must be shared between X and Y). We must now solve the 1-D problem -- and apply it twice.

有原来的最佳轨迹包括推压在一个方向上,那么其他,并且不准备回到第一个试。 (惯性是有用的,只有当其它轴的解决方案将需要更长的时间比你让你有时间去杀人。)先解决了速度问题:假设(WLOG),你的目标速度大于你的初始速度。为了达到目标速度,您将需要一段时间的推力(+)持续时间

It turns out the best trajectory involves thrusting in one direction, then the other, and not going back to the first one again. (Coasting is useful only if the other axis's solution will take longer than yours so you have time to kill.) Solve the velocity problem first: suppose (WLOG) that your target velocity is greater than your initial velocity. To reach the target velocity you will need a period of thrust (+) of duration

T = (Vf - Vi)/a

(我用Vf的:最终的速度,VI:初始速度,A:推力的大小)

(I'm using Vf: final velocity, Vi: initial velocity, a: magnitude of thrust.)

我们注意到,如果这就是我们所做的一切,该位置将不出来正确的。最终的实际位置将是

We notice that if that's all we do, the location won't come out right. The actual final location will be

X = (Vi + Vf)T/2

所以我们要增加一个修正

So we have to add a correction of

D = Xf - X = Xf -(Vi+Vf)T/2

现在让位置出来吧,我们添加了一段推力在一个方向上的之前的是,和之后,在相反的方向相同时期。这将使最终速度不受干扰,但给我们一些位移。如果这第一周期(和第三)的持续时间为t,则我们从它得到的位移

Now to make the location come out right, we add a period of thrust in one direction before that, and an equal period in the opposite direction after. This will leave the final velocity undisturbed, but give us some displacement. If the duration of this first period (and the third) is t, then the displacement we get from it is

d = +/-(at^2 + atT)

在+/-取决于我们是否推力+然后 - 或 - 那么+。假设它的+。 我们解决了二次:

The +/- depends on whether we thrust + then -, or - then +. Suppose it's +. We solve the quadratic:

t = (-aT + sqrt(a^2 T^2 + 4 a D))/2a

和我们就大功告成了。

这篇关于物理学飞船的2D轨迹规划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆