Cover
embedded systems

On compilers to a physics engine

Introduction
On compilers to a physics engine

I have never used a physics engine in my life. Now, here I am building a game engine for the Teensy 4.1, and making good progress, at that! Here are my thoughts so far:

It is thrilling (and a little scary) working in an area where I really have no idea what to do next. But it reminds me a lot of how I made my first interpreter, VVTBI. For starters, wait, why does that say 15 years ago!?!? a friend of mine used to challenge me to work on a new, difficult project every weekend. The language for the interpreter is "Very, Very, Tiny Basic." Amusingly, I found an academic copy of an interpreter of a very similar language: "Very Tiny Basic". Stay with me – yes, they're different.

Some background

What I did was build my own from scratch, using the academic copy as a reference and with my own take on the project design. Once I finished, I had written my first LR(1) recursive descent parser and lexer, something that left an imprint on me and that I would write many more times later. I would then use that project as my own reference implementation, write recursive-descent parsers again and again, and finally learn about grammars, parser generators, and parser combinators.

Ultimately, I built parsers and lexers - starting from that first interpreter - so many times that today I could probably build a full-stack compiler to a target ISA in a single weekend for a small language.

Hello, physics

I have realized that that is what I'm doing today with luya. I found my reference physics engine, Box2D-lite, ported it, and made it accessible for embedded devices. Now I will always have a physics engine to use for embedded and future 2d projects. Some of the geometry and linear algebra I haven't thought about in more than 10 years, such as translation matrices, but I made sure to document the code in a way where I - or someone else who wants to use this with no experience - will never forget and can easily get started.

The future

Obviously, today, working on projects is a little different: AI has a grip on software engineering. But when you use it just right, you are still able to learn and create your reference implementation in a way where you aren't just "vibe coding" - you use it to fill the gaps in the same way a lecturer would. In 2012, I was lucky to find an academic copy of a VTBI interpreter with such good documentation; if I hadn't, I would never have been able to get started in compiler theory. I ported box2d-lite for embedded devices, then used AI to document it for me and help me write the tests for correctness.

Now I have a well-written embedded physics engine and understand the differences between a physics engine and a renderer pipeline. Things I won't ever forget. Of course, there is one caveat that should be mentioned. The engine is well-written because I have the expertise in C++ to verify and prove it. In terms of learning and education, AI isn't very helpful for things that you cannot verify yourself.

That part is up to you.

Jahan Addison
View Comments
Previous Post

The year of the chip!