▲ | raincole 8 days ago | ||||||||||||||||||||||
Off topic, but where should one start learning writing physical simulation? Several years ago I ran into this project [0] and got overwhelmed even the algorithm can be written in 88 lines of C++. I realized that out of all CS topics, physical simulation is probably the one I knew the less (not saying I'm a compiler/database expert or something, but at least I've implemented a toy compiler and some basic data structures used in database. When it comes to physical simulation my bran just draws a blank.) | |||||||||||||||||||||||
▲ | IIAOPSW 8 days ago | parent | next [-] | ||||||||||||||||||||||
Probably what you want is "numerical methods" and "computational physics". "Physical simulation" is a very broad scope, so your code for simulating fluids is going to be very different from your code for simulating planetary orbits, and at times it may feel a bit ad hoc. But at its foundation physical laws are written in differential equations and linear algebra. So whatever algorithm lets you numerically integrate several inter-related variables is going to be broadly applicable to simulating any physical phenomena. At the simplest end of the spectrum you just naively approximate integration by brute force. Eg at each step just update your physical state variables by doing velocity += acceleration, position += velocity. This is called Euler's method, and while simple, it accumulates unacceptable errors rather quickly in most circumstance. The more advanced approach is to use a method like Runge Kutta. In circumstances where you have some known property, like say energy conservation, you can implement a method which explicitly imposes the constraint. This is good for cases where the motion is highly periodic as it prevents the numerical error from accumulating exponentially in orbits that spiral out of control. Of course at some point you'll have to grapple with the issue of if you are simulating trajectories of free particles or values of neighboring grid points in a field. This question of how best to encode physical systems and simulate them cuts to the heart of physics. I'll leave it at the old cliche "information is physical" | |||||||||||||||||||||||
▲ | maccard 8 days ago | parent | prev | next [-] | ||||||||||||||||||||||
Rigid body simulations are much much simpler. There’s a siggraph course from 2001 [0] which is a bit of a dense read but it will bring you all the way up to a full blown rigid body simulation and understanding the math behind it too. | |||||||||||||||||||||||
| |||||||||||||||||||||||
▲ | Cthulhu_ 8 days ago | parent | prev | next [-] | ||||||||||||||||||||||
One thing that helped me was doing some tutorials for pico-8, an intentionally 'weak' game platform, one of which is a platform game with a simple / understandable inertia / gravity simulation (jumping, running left/right; think Mario). It was understandable enough with an x / y position for the character and a delta-x / delta-y representing their current speed. Every frame the dx / dy would get changed depending on player input and/or character state. Ex: if player presses jump button, set state to 'jumping' and dy to 1. Every frame, dy = dy * 0.9. When dy <= 0, set state to 'falling'. Every frame, dy = dy * 1.1 until dy = 1 (terminal velocity). Then add some collision detection. I think those basics are also behind the simpler physics simulations, the 'falling sand' types would be ideal for an application like this. | |||||||||||||||||||||||
| |||||||||||||||||||||||
▲ | maurits 8 days ago | parent | prev | next [-] | ||||||||||||||||||||||
For statistical mechanics, I really liked [1]. Comes with loads of python programs. To see what might peak you interest, the videos in [2] could be a good starting point. [1]: https://www.coursera.org/learn/statistical-mechanics [2]: https://matthias-research.github.io/pages/tenMinutePhysics/i... | |||||||||||||||||||||||
▲ | stormfather 8 days ago | parent | prev | next [-] | ||||||||||||||||||||||
Just go to Perplexity or something like that, its well trodden ground. And essentially, what you're doing is discretizing the relevant differential equations and getting that to run in a 2D or 3D cellular automaton. I'll give you a simple example. For diffusion of heat between 2 points, the rate of change (first derivative) is proportional to the difference in temp between them. So you make an update rule for points on a grid that says "calc the average difference of a cell's temp with that of its neighbors, multiply by some constant, and that is the amount to update this cell at this time. Run that for every cell in parallel, many times." Then you tack on a visualization and you can watch the heat diffuse. A fun example would be the cooling of the proto-Earth. You can watch the crust form. Heat diffusion is a good starter problem. So is gravitational interaction. | |||||||||||||||||||||||
▲ | dawnofdusk 8 days ago | parent | prev | next [-] | ||||||||||||||||||||||
Essentially all physics simulations are either particle based or based on integration of differential equations (although in a computer both approaches involve a discretization which makes them somewhat computationally similar). You can consider reading through Numerical Recipes which is sort of the bible for this stuff for physicists, but it is aimed at scientific audiences with weak CS background. Something like Computer Simulation of Liquids by Allen could be a good start too. Let's be clear that the approach I'm talking about here is focused more on physical correctness: if you are a game designer it's not important that your fluid simulations are physically correct and more that it looks physically correct to a player, and there are a variety of more heuristic techniques for something like that. | |||||||||||||||||||||||
▲ | ethan_smith 8 days ago | parent | prev | next [-] | ||||||||||||||||||||||
The Nature of Code by Daniel Shiffman is an excellent entry point - it teaches fundamentals of physics simulations with clear examples in Processing/p5.js. | |||||||||||||||||||||||
▲ | pornel 7 days ago | parent | prev [-] | ||||||||||||||||||||||
For water simulation, look towards learning compute shaders. Eulerian (grid-based) simulation is one of the classic examples. |