This is a macOS application featuring a toy physics simulation that you can play around in. I created it just to learn a little more about physics. It's been sitting around doing nothing for a while, so I thought I'd open source it.
This isn't a library or any type of system that's designed to be used in another application, and only simulates some very basic physics interactions.
Disclaimer: I'm not a physics expert. There may be much better techniques to create a physics simulation than the ones that I've used!
Physics Scene:
Golf:
Snooker:
Anti-gravity Balls:
Download the application from here.
- Clone the repository
- Open in Xcode
- Build and run the application
The app allows you to build a 'physics scene'. A scene is an environment of static physical objects that you can insert dynamic ones into.
The quickest way to try the app is to use the bundled example scenes.
- Run the app.
- From the
Scene -> Example
menu, select an example scene. - Click in the scene window to add balls to the scene.
- On the right hand side you can select
Drop
to drop balls into the scene with no momentum, orFling
to use an 'Angry Birds' flinging approach. - Adjust ball size, elasticity on the side pane.
Use the 'Add Lines', 'Add Circles', and 'Add Polyline` buttons on the side panel to enter each mode. Input usually just requires simple clicking / dragging. Each mode displays a basic instructions at the top.
Press Escape to cancel an operation or to go back to the standard interactive mode.
Press the 'Edit' button in the side panel to enter Edit mode. Grabbable handles are displayed over the scene. You can perform operations by dragging such as moving line ends, moving objects, and adjusting circle radii.
Each scene boundary wall can be enabled and disabled.
You can also temporarily enter edit mode at any other time by holding the option key.
Use the 'Grid' checkbox in the side panel to enable the grid. the plus and minus buttons can be used to alter the number of grid divisions across the scene.
When the grid is enabled, all edits and placement of objects will snap to the closest grid position.
Whilst the grid is enabled, it can be temporarily disabled during any operation by holding the control key.
There are multiple options for calculating the timestep. If you are unfamiliar with the concept of a timestep, then I recommend this awesome blog post.
Variable
Variable locks the timestep to the display rate. This means that the timestep may waver slightly, producing less deterministic results.
Semi-Fixed
Semi-fixed consumes the available time in specified time slices. If there is time left over then this is consumed in a smaller 'remainder' slice.
This approach results in fewer errors such as tunneling.
The frame rate can be specified. There is no protection against the 'spiral of death', so setting the frame rate to a value higher than your computer is able to compute in time each frame will result in the application beach-balling.
Fixed
Fixed uses a fixed time slice for each step of the simulation. Any remainder is carried over to the next frame.
This results in a completely deterministic and reproducible simulation (unless of course you change the time step!). The downside is that the amount of time consumed may not match the frame rate, as the remainder is carried over. This can produce some jitter.
Enabling 'interpolation' will render objects at the position that they would be assumed to be at based on having continued to travel on their current trajectory for the amount of the remainder time.
Similar to Semi-Fixed, it's possible to hang by configuring high frame rates.
Use the 🗑 Balls
and 🗑 Scene
buttons to remove all of the balls from the scene, or to clear the scene geometry entirely
Load one of the example scenes using Scene -> Example
and selecting a scene.
Save a custom scene using Scene -> Save
.
Load a custom saved scene file using Scene -> Load
.
Created by Steve Barnegren