After an aborted run at Rares Trading in Elite Dangerous (idea: pick up rares, go to Fehu, sell them, and then pick up missions there. Problem: takes many hours to get enough worth selling), I took a look at writing an autopilot in my javascript space game (the part that is not yet Mud-like, but I intend for it to be).

Nice: Javascript has a Math.atan2(y,x) which _possibly_ avoids the x=0 problem that normal Math.atan() solutions do. (arctangent gets you the angle to something so you know where to point the ship).

However, my code doesn’t work. My ship very often points AWAY from the target, and applies FULL THRUST FOREVER. This is NOT how you go somewhere.

There’s another aspect – I’m simulating “keyboard control”, ie, IsThrusting, IsLeft, IsRight. For the ship to feel responsive when playing the game, need to have good numbers for these. But with autopilot, there’s a lot of jigger (LRLRLRLR) because it continually overshoots its target.

**So, here’s what I have to do for the relatively final solution:**

- Set up the ship so that there’s a finer gradient of control.
- What I’ve seen other games do is, the longer you have it pressed, the more thrust you get, up to max.
- This means I need to swap out my current keyboard handler (keyup, keydown) to change to a dictionary of “when was it pressed”, so that I can calculate how long its been held.

- Or, I could go with W = 12.5% thrust, Ctrl-W = 25%, shift-W= 50%, and ctrl/shift = 100%. Almost 8 stops. More instantaneous full thrust available.
- I want to avoid something where the autopilot can take a shortcut that’s not really available to a player. So if I did the slow build up, I’d have to have the autopilot also do a slow build up.

- What I’ve seen other games do is, the longer you have it pressed, the more thrust you get, up to max.
- Change how I do the maths for the autopilot. Inputs to autopilot: Who to go to, and what distance to orbit them at when we get there.
- I see a few major layers.
- First one is “close the distance”. This would use the calculation of … http://physics.info/motion-equations/ … ouch, my brain hurts. Basically, I need to find a solution for:
- If I start at the orbit distance, and accelerate away at 75% thrust, what velocity am I at when I get to my current distance away? Reverse that. That’s the velocity towards the object that I want. Call that V1. I want it to shut off as I get close to the destination so that orbital stuff can kick in.

- Second one is orbital force. Skip this if I’m more than 2x the desired distance.
- Figure out if I’m going clockwise or counter clockwise from the destination. OR, just assume one direction for now. clockwise.
- Figure out the vector I need to be in a perfect circular orbit around the object at the desired distance. Magnitude is known (previous post on orbital mechanics has link to the article), and Direction is 90 degrees from where the destination is.
- That’s my desired vector V2.

- Combine V1 and V2. This is my desired vector V3
- Look at my current vector V4 and figure out the change that needs to happen V5 = V3-V4.
- Do a left/right thrust as needed to point me at that vector (do a smoothing function here).
- if I’m pointing in relatively the right direction, do a thrust along that vector (do a smoothing function here).

This could be really fun.

Image: wikipedia

## Leave a Reply