This project is a 2d particle simulation implemented in Python, featuring optimized collision detection and resolution. The simulation includes rendering capabilities to visualize the particles and their interactions.
- Particle generation with random initial positions and velocities.
- Gravity and wall constraints.
- Collision detection using spatial partitioning for efficiency.
- Collision resolution with impulse-based physics.
- Rendering support to visualize the simulation.
-
Clone the repository:
git clone https://github.com/noahfarr/pysics.git cd pysics
-
Install the dependencies:
poetry install
-
Activate the virtual environment:
poetry shell
To run the simulation, you need to have a renderer implemented. This project already has a RaylibRenderer
implemented.
-
Ensure you have the renderer set up and import it in the simulation script.
-
Create a script to run the simulation:
from pysics.render import RaylibRenderer from simulation import Simulation if __name__ == "__main__": renderer = RaylibRenderer((800, 600)) # Set the window size as needed sim = Simulation(n_particles=100, dt=0.01, renderer=renderer) sim.simulate(total_timesteps=1000, render=True)
All of these options can be set in the config.py file.
- Run the script:
python main.py
simulation.py
: Contains theSimulation
class and related logic.particle.py
: Contains theParticle
class with physics properties and methods.render.py
: Contains theRenderer
andRaylibRenderer
classes for visualizing the simulation.config.py
: Contains a dataclass to configure the parameters of the simulation.pyproject.toml
: Configuration file forpoetry
, listing dependencies and project metadata.
- Spatial Partitioning: Efficiently detects potential collisions by dividing the simulation space into a grid.
- Vectorized Operations: Utilizes NumPy for efficient mathematical operations.
- Reduced Function Call Overheads: Simplifies and inlines frequently called functions.
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.