Note that there are some explanatory texts on larger screens.

plurals
  1. PO2d trajectory planning of a spaceship with physics
    primarykey
    data
    text
    <p>I'm implementing a 2D game with ships in space.</p> <p>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.</p> <p>My problem is that I don't know how to move my spaceships properly on a 2D physics-enabled world. More concretely:</p> <p>A ship of mass <code>m</code> is located at an initial position <code>{x, y}</code>. It has an initial velocity vector of <code>{vx, vy}</code> (can be <code>{0,0}</code>).</p> <p>The objective is a point in <code>{xo,yo}</code>. The ship has to reach the objective having a velocity of <code>{vxo, vyo}</code> (or near it), following the shortest trajectory.</p> <p>There's a function called <code>update(dt)</code> 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:</p> <pre><code>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 </code></pre> <p>The first <code>???</code> 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 <code>???</code> part consists on how to calculate the impulse angle on a given dt.</p> <p>We are in space, so we can ignore things like air friction.</p> <p>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.</p> <p>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.</p> <p>Thanks a lot.</p> <p>EDIT: Beta provided a valid strategy for this and Judge kindly implemented it directly in LÖVE, in the comments.</p> <p>EDIT2: After more googling I also found <a href="http://opensteer.sourceforge.net" rel="noreferrer">openSteer</a>. It's on C++, but it does what I pretended. It will probably be helpful to anyone reaching this question.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload