TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

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

277 点作者 szhu大约 2 年前
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 条评论

simonh大约 2 年前
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 未加载
timmaxw大约 2 年前
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 未加载
paradite大约 2 年前
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 未加载
Nodraak大约 2 年前
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).
marketdev大约 2 年前
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.
ricardobeat大约 2 年前
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 未加载
monkellipse大约 2 年前
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!
mg大约 2 年前
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 未加载
mckirk大约 2 年前
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 未加载
jkonline大约 2 年前
&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_miner大约 2 年前
This is great but I&#x27;d like to see an autopilot that can do the hardest crash
huevosabio大约 2 年前
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.
szhu大约 2 年前
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>
Aperocky大约 2 年前
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>
khalidx大约 2 年前
This is awesome! The closest I got without the autopilot was 94 or so. The autopilot consistently scores 100+
ehmorris大约 2 年前
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>
bjd2385大约 2 年前
Really cool! The fact that astronauts handled the landing in the Apollo missions is nerve wracking
000ooo000大约 2 年前
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.
curiousgal大约 2 年前
Best I could score was 102.0 with a combination of manual and autopilot. Neat!
josephcsible大约 2 年前
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_awake大约 2 年前
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 未加载
iandanforth大约 2 年前
I&#x27;m impressed by the simplicity of the solution, well done.
评论 #35095578 未加载
handonam大约 2 年前
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 未加载
m00dy大约 2 年前
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 未加载
hawski大约 2 年前
Would it be possible to put the autopilot in a bookmarklet for the original game?
评论 #35091949 未加载
tiffanyh大约 2 年前
Mobile?<p>How to you trigger autopilot on mobile?
评论 #35091045 未加载
tommica大约 2 年前
What about adding a limited fuel supply?
评论 #35093394 未加载
kevinwang大约 2 年前
And now we&#x27;ve gone full circle
评论 #35095418 未加载
_ZeD_大约 2 年前
uhm... it doesn&#x27;t seem to work, at least on my workstation (firefox on a debian)
评论 #35096595 未加载
antegamisou大约 2 年前
Now do it with LQR :D