top of page

I finally ported my C++ demo code to a JavaScript demo that runs in a web browser using ChatGPT. The first version of this code was written in 2003 for a SIGGRAPH course on simulating and controlling nature. I subsequently showcased it during many invited and keynote talks to illustrate how optimization and machine learning work in the context of physical simulation, more precisely, constrained optimization. There are four ingredients: 1) a state, 2) a control, 3) a cost function, and 4) a constraint.

I illustrate constrained optimization with a simple example of a cannon trying to hit a target. In this case, the control is the angle at which the cannon shoots the ball. The target is a user-specified point that you can move with the right mouse button. The cost function is the squared distance between the target's position and where the cannonball lands. Finally, the ball is constrained to lie on the parabolic path determined by the initial position and velocity of the cannonball. The goal is to hit the target, which you can achieve by changing the angle of the cannon manually. Notice that there are two solutions. By using the gradient, we can find a minimum automatically by following the negative gradient. You can see this in action by enabling "gradient descent." Depending on the current angle, you will end up in one of the minima. By enabling the "smoothing term," you can add a cost term that minimizes travel time, resulting in a global minimum.

Some thoughts on using AI: at first, I tried to have the AI create the web app from a detailed description of what I wanted to achieve. I thought that starting with the cannonball simulation and then progressively adding features would work, as I usually code. However, the AI constantly rebuilt the app from scratch, introducing bugs and a new appearance each time! After half a day, I gave up. I then instructed the AI to take my old C++ code with OpenGL, GLUT, and GUI and create a web JavaScript app. After a few minutes, it produced the app above, which I then tweaked manually since I know the rudiments of JavaScript. So, will AI replace programmers? Maybe, with a better intermediate language—a generic high-level language that can then be translated into a local dialect like C++ or JavaScript, much like compilers eliminated the need for writing everything in assembly language.

This demo was shown at the SIGGRAPH 2019 special Nvidia event.

https://developer.nvidia.com/siggraph/2019/video/sig903-vid

©2018-2026 by Jos Stam.

bottom of page