About
Physisim (Physics-Simulator) is a simple graphical physics simulator with its own interpreted scripting language, Physiscript. You define particles, their properties, and the forces acting on them in a script file (.psim), and the engine reads those instructions and builds a live simulation.
Motivation
Back in the fall of 2024, I was taking a class on programming language design. Simultaneously, I was taking one of the required engineering physics classes required for my degree. I had always thought it would be helpful to watch physics homework problems play out live, in order to better understand them. I was already working on a scripting language for my design class, but at the time it had no real use. I decided to repurpose it and create what is now Physisim.
The logic behind Physiscript was to use the same 'homework' format that I was already familiar with (given x, find y). This format describes objects, their properties, and specifies what should happen to them at certain time offsets. Physiscript follows that same structure.
In order to make this work, I also had to build a (simple) physics engine, with a graphical display. This program was probably the first one I built with a dedicated clock (tick).
Writing Physiscript
Physiscript is designed to read like the description of a physics problem.
For example, PARTICLE obj1 declares a particle, and obj1 MASS 14
gives it a mass of 14 kilograms. A force is declared in the same way, using
FORCE.
Vector values are written as (x, y). For example,
f1 MAG (-2.3, 7.9) sets the components of a force. Numerical values assume
base units.
| Syntax | Meaning |
|---|---|
PARTICLE |
Declare a particle with a name |
FORCE |
Declare a named force vector |
MASS |
Set a particle's mass |
RADIUS |
Set a particle's radius |
DENSITY |
Set a particle's density |
ELASTICITY |
Set a particle's bounciness |
POS |
Set a particle's (x, y) position |
MAG |
Set the (x, y) components for a declared force. |
APPLY |
Schedule a force to be applied at (@) time, in seconds. |
With an object and force defined, APPLY f1 obj1 @ 10 reads
exactly as “apply f1 to obj1 at ten seconds.” The interpreter records that
event for the engine to apply after the specified time delta ellipses.
Physiscript Interpreter
The interpreter starts by reading the script into a buffer. A lexer splits it into tokens using spaces, newlines, and tabs. Those tokens are then stored in a dictionary and encoded as integer indexes.
Execution happens via the Current-Operation Next-Operation (CONO) table. As the interpreter walks through encoded tokens, it pairs operations, and looks up the action it should perform in the CONO table.
Physics Engine
The engine manages elapsed time, checks scheduled events, and advances the simulation. At the moment, only circular particles have been implemented.
Collisions
For particles, collision detection compares the distance between their centers with the sum of their radii. If they overlap, the handler separates them, and calculates an impulse using their components. That impulse adjusts each particle's velocity accordingly.
Examples
Basic
This example from the whitepaper places three particles along the
same line. The middle particle, c, has more mass than
the other two. A force is scheduled on particle a after two seconds.
example.psim
PARTICLE a
a MASS 0.3
a POS (200, 200)
PARTICLE b
b MASS 0.3
b POS (400, 200)
PARTICLE c
c MASS 0.54
c POS (300, 200)
FORCE f
f MAG (20, 0)
APPLY f a @ 2
Initially, all three particles are stationary. After the force is applied, the leftmost particle moves right and strikes the middle one. It rebounds to the left, while the middle particle moves right and collides with the final particle, passing the motion along.
Pool
The pool example from earlier is defined as such:
pool.psim
PARTICLE white
white MASS 0.1
white RADIUS 15
white ELASTICITY 0.4
white POS (100, 250)
PARTICLE one
one POS (300, 250)
PARTICLE two
two POS (340, 270)
PARTICLE three
three POS (340, 230)
PARTICLE four
four POS (380, 290)
PARTICLE five
five POS (380, 250)
PARTICLE six
six POS (380, 210)
PARTICLE seven
seven POS (420, 270)
PARTICLE eight
eight POS (420, 230)
PARTICLE nine
nine POS (420, 190)
PARTICLE ten
ten POS (420, 310)
FORCE f
f MAG (20, 0.3)
APPLY f white @ 2
Source and Whitepaper
The Physisim whitepaper (PDF), written December 14, 2024, contains the full design, implementation, and example scripts. The screenshots on this page can be found in it's results section.
You can also find the project source code on GitHub.