TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: I built an autopilot for the lunar lander game

277 pointsby szhuabout 2 years ago
I got pretty good at (and very addicted to) the lunar lander game from a few days ago...<p>so I decided to make an autopilot for the lander based on what I felt like was the best strategy! Now I can have perfect landings every time without lifting a finger :D<p>Writing the autopilot code was a lot more fun than I expected! It felt a bit like programming a robot.<p>Source code: <a href="https:&#x2F;&#x2F;github.com&#x2F;szhu&#x2F;lunar-lander-autopilot">https:&#x2F;&#x2F;github.com&#x2F;szhu&#x2F;lunar-lander-autopilot</a><p>Original lander HN post: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=35032506" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=35032506</a>

30 comments

simonhabout 2 years ago
That&#x27;s cool, but it has a very conservative descent profile and would use a ton of fuel. Having played Kerbal I&#x27;m used to attempting fuel efficient landings, which means more of a suicide burn approach, but of course this game doesn&#x27;t keep track of that.<p>There&#x27;s something timelessly appealing about lunar lander games. The very first game I ever played on a computer, written in BASIC, was a &#x27;turn based&#x27; one dimensional lunar lander game where you input how much thrust you used for each second of the descent, and then it recalculated your altitude, velocity, etc. I learned programming by rewriting it to be a real-time game where you pressed a key to fire the engine.
评论 #35092682 未加载
评论 #35096460 未加载
评论 #35094352 未加载
timmaxwabout 2 years ago
I wanted to go for a record of as many flips as possible. So I boosted up to 25,000 feet; started spinning until it was doing about one flip per second; waited a few minutes; then turned on the autopilot to come into a landing.<p>The autopilot did a perfect job of stopping the rotation and lateral motion, so the lander came down straight as an arrow. Unfortunately, the autopilot didn&#x27;t even try to decelerate! I crashed into the moon at 0.9 degrees and 770 MPH.
评论 #35091622 未加载
评论 #35098958 未加载
paraditeabout 2 years ago
Lunar lander is a one of the problems in Open AI Gym, where you test AI against standard set of problems: <a href="https:&#x2F;&#x2F;www.gymlibrary.dev&#x2F;environments&#x2F;box2d&#x2F;lunar_lander&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.gymlibrary.dev&#x2F;environments&#x2F;box2d&#x2F;lunar_lander&#x2F;</a><p>Then you have stable baselines which implements popular reinforcement learning algorithms to solve these gym problems: <a href="https:&#x2F;&#x2F;stable-baselines3.readthedocs.io&#x2F;en&#x2F;master&#x2F;" rel="nofollow">https:&#x2F;&#x2F;stable-baselines3.readthedocs.io&#x2F;en&#x2F;master&#x2F;</a><p>Shamless plug: I&#x27;ve built a series of games where you solve puzzles (2048) &#x2F; toy problems (MDP) like the lunar lander using various AI and ML algorithms.<p>You can check it out here: <a href="https:&#x2F;&#x2F;ai-simulator.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;ai-simulator.com&#x2F;</a>
评论 #35093993 未加载
评论 #35093742 未加载
评论 #35103024 未加载
Nodraakabout 2 years ago
Nice auto pilot! Very basic, but works well enough.<p>In the real world, you would derive physics equations (acceleration -&gt; velocity -&gt; position), add constraints and then solve everything to obtain an optimal trajectory (mostly in term of fuel, but you can add other constraints too, for ex due to radar-ground or Antenna-Earth visibility). I wrote a blog post about Apollo&#x27;s algorithm: <a href="https:&#x2F;&#x2F;blog.nodraak.fr&#x2F;2020&#x2F;12&#x2F;aerospace-sim-2-guidance-law&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.nodraak.fr&#x2F;2020&#x2F;12&#x2F;aerospace-sim-2-guidance-law...</a> (Described in the second section ; the first section is about a naive algorithm similar to yours that in the end did not work as well as I wanted).<p>Also, thanks for the code, I wanted to do the same, but lost motivation when I could not really expose in a satisfying way the internal state out of these JS modules (it&#x27;s not complicated in the end, but I&#x27;m simply not a frontend dev ; and I wanted to avoid forking and monkey patching everything and simply adding some JS code throught the console or something).
marketdevabout 2 years ago
While I was playing the game, the idea of traveling in a SpaceX rocket ship didn&#x27;t seem appealing to me. Now that I have experienced your autopilot feature, I feel MUCH more confident about it.
ricardobeatabout 2 years ago
It’s amazing how a simple algorithm performs so well, the autopilot is able to recover from some pretty extreme situations, and does it gracefully.<p>Adding some realism to the engine physics (firing delays, minimum firing time, power ramp up, heat limits etc) would likely make it 10x harder.
评论 #35093116 未加载
评论 #35097762 未加载
monkellipseabout 2 years ago
Nice! I also got hooked on the lander thing, really quick to pick up and good replay value! Your autopilot handles a gentle landing well. If I get the lander going sideways at any appreciable speed, then flip autopilot on, a crash is inevitable :) As a human my solution to that is simply slowing down with lots of thrust, though I still wind up turning the lander into scrap most of the time hah!
mgabout 2 years ago
It&#x27;s interesting to think about how the optimal strategy to land the lander would look like.<p>If the distance to the moon was large, I would expect 4 phases:<p>1: Turn the lander towards the moon<p>2: Constant thrust towards the moon<p>3: Turn the lander away from the moon<p>4: Constant thrust away from the moon<p>But if the initial distance is small enough, turning it around might not be worth it or even possible.<p>So the optimal strategy is probably a somewhat complex function of the initial angle and distance to the moon.
评论 #35092109 未加载
评论 #35091508 未加载
mckirkabout 2 years ago
I can confirm that with the improved auto-pilot it is possible to get a 103.6 point landing after performing 1,151 flips (and reaching a max speed of 504 mph and a height of about 38k feet), if you can spare your phone for 38 minutes.
评论 #35103701 未加载
jkonlineabout 2 years ago
&quot;Oh SNAP! I&#x27;m amazing at this!&quot; 101.1 point landing on my second try!<p>At least, that&#x27;s what I thought, until I realized I had previously activated the auto-pilot. Best #footgun today (so far!).<p>So I guess I should say: &gt; Oh SNAP! This auto-pilot is amazing at this!<p>Thanks!
dropbox_minerabout 2 years ago
This is great but I&#x27;d like to see an autopilot that can do the hardest crash
huevosabioabout 2 years ago
Love it! I started playing by disabling autopilot, putting the ship in complicated positions (e.g. spinning uncontrollably) and then turning the autopilot on, it&#x27;s fun to which conditions it can recover.
szhuabout 2 years ago
Update: I improved the algorithm.<p>The autopilot now scores a &quot;perfect landing&quot; almost every single time, it lands very noticeably faster, and the code no longer contains a complicated, trial-by-error formula!<p>I got my first 103+ point landing! (It does this about half the time now)<p><a href="https:&#x2F;&#x2F;user-images.githubusercontent.com&#x2F;1570168&#x2F;224399420-5a8d688a-9306-4499-aea0-5e9e212b438e.png" rel="nofollow">https:&#x2F;&#x2F;user-images.githubusercontent.com&#x2F;1570168&#x2F;224399420-...</a>
Aperockyabout 2 years ago
A career in KSP is calling for you (Landing on the mun automatically):<p><a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=TY63i8V1-DA">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=TY63i8V1-DA</a><p>The most I&#x27;ve used linear algebra since college:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;Aperocky&#x2F;ksp_eng&#x2F;blob&#x2F;master&#x2F;lib&#x2F;space_lib.ks#L301">https:&#x2F;&#x2F;github.com&#x2F;Aperocky&#x2F;ksp_eng&#x2F;blob&#x2F;master&#x2F;lib&#x2F;space_li...</a>
khalidxabout 2 years ago
This is awesome! The closest I got without the autopilot was 94 or so. The autopilot consistently scores 100+
ehmorrisabout 2 years ago
Woah this is awesome! Someone on Twitter made an autopilot with ML but this seems a lot better.<p><a href="https:&#x2F;&#x2F;twitter.com&#x2F;_s_w_a_y_a_m_&#x2F;status&#x2F;1633468475611004928?s=20" rel="nofollow">https:&#x2F;&#x2F;twitter.com&#x2F;_s_w_a_y_a_m_&#x2F;status&#x2F;1633468475611004928...</a>
bjd2385about 2 years ago
Really cool! The fact that astronauts handled the landing in the Apollo missions is nerve wracking
000ooo000about 2 years ago
Cool, neat code too :) Now do <a href="https:&#x2F;&#x2F;race-condition.reaktor.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;race-condition.reaktor.com&#x2F;</a>! I wanted to pour a few hours into race condition but I got it to complete a lap and never went back.
curiousgalabout 2 years ago
Best I could score was 102.0 with a combination of manual and autopilot. Neat!
josephcsibleabout 2 years ago
One suggestion: if the lander is within about 10 feet of the ground and is still either swaying back and forth or going really fast laterally, it should thrust upwards to buy more time to correct those things.
评论 #35095060 未加载
tired_and_awakeabout 2 years ago
Simple and fun, thanks for sharing! Have you thought of adding a wordle style variation - just a daily puzzle that&#x27;s a different start orientation + velocity? Just a thought.
评论 #35096758 未加载
iandanforthabout 2 years ago
I&#x27;m impressed by the simplicity of the solution, well done.
评论 #35095578 未加载
handonamabout 2 years ago
this is great! The one thing i think to optimize it is to predict how late to start the engine to minimize its usage. The last I got was 730 times used, at 53seconds.
评论 #35095851 未加载
m00dyabout 2 years ago
very similar to <a href="https:&#x2F;&#x2F;www.gymlibrary.dev&#x2F;environments&#x2F;box2d&#x2F;lunar_lander&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.gymlibrary.dev&#x2F;environments&#x2F;box2d&#x2F;lunar_lander&#x2F;</a>
评论 #35096488 未加载
hawskiabout 2 years ago
Would it be possible to put the autopilot in a bookmarklet for the original game?
评论 #35091949 未加载
tiffanyhabout 2 years ago
Mobile?<p>How to you trigger autopilot on mobile?
评论 #35091045 未加载
tommicaabout 2 years ago
What about adding a limited fuel supply?
评论 #35093394 未加载
kevinwangabout 2 years ago
And now we&#x27;ve gone full circle
评论 #35095418 未加载
_ZeD_about 2 years ago
uhm... it doesn&#x27;t seem to work, at least on my workstation (firefox on a debian)
评论 #35096595 未加载
antegamisouabout 2 years ago
Now do it with LQR :D