|
| 1 | +# Spring Mass Simulation |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +A physics-based simulation of a spring-mass system built with Raylib. This project demonstrates the mechanics of a mass connected to an anchor point via a spring, visualizing the spring's deformation and the mass's motion. |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +This simulation renders a mass sliding horizontally on a floor, connected to a fixed anchor point above by a spring. The spring dynamically stretches and compresses as the mass moves, with a zigzag coil pattern that accurately represents real spring mechanics. |
| 12 | + |
| 13 | +The mass moves at a constant velocity to demonstrate how the spring adapts to changing distance between the anchor and the mass. |
| 14 | + |
| 15 | +## Features |
| 16 | + |
| 17 | +- Real-time spring visualization with dynamic coil deformation |
| 18 | +- Zigzag spring pattern that stretches along the axis between anchor and mass |
| 19 | +- Smooth 120 FPS animation |
| 20 | +- Minimal, clean codebase for easy understanding and modification |
| 21 | +- Cross-platform support (Windows, Linux, macOS) via Raylib |
| 22 | + |
| 23 | +## Building and Running |
| 24 | + |
| 25 | +### Prerequisites |
| 26 | + |
| 27 | +- C compiler (gcc, clang, or MSVC) |
| 28 | +- Raylib library installed and linked |
| 29 | + |
| 30 | +### Compilation |
| 31 | + |
| 32 | +Compile the project using your preferred compiler. For example, with gcc: |
| 33 | + |
| 34 | +```bash |
| 35 | +gcc spring.c -o spring_simulation -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 |
| 36 | +``` |
| 37 | + |
| 38 | +Note: Linker flags may vary depending on your system and Raylib installation. |
| 39 | + |
| 40 | +### Running |
| 41 | + |
| 42 | +After compilation: |
| 43 | + |
| 44 | +```bash |
| 45 | +./spring_simulation |
| 46 | +``` |
| 47 | + |
| 48 | +## Controls |
| 49 | + |
| 50 | +- The simulation runs automatically upon launch |
| 51 | +- The mass moves continuously to the right at a constant velocity |
| 52 | +- Close the window to exit |
| 53 | + |
| 54 | +## Code Structure |
| 55 | + |
| 56 | +- `drawSpring()` - Renders the spring with a zigzag coil pattern between anchor and mass |
| 57 | +- `drawMass()` - Draws the rectangular mass object |
| 58 | +- `drawFloor()` - Renders the floor line |
| 59 | +- `main()` - Main game loop handling initialization, updates, and rendering |
| 60 | + |
| 61 | +## Configuration |
| 62 | + |
| 63 | +Key constants are defined at the top of the file for easy customization: |
| 64 | + |
| 65 | +- `FPS` - Target frames per second (default: 120) |
| 66 | +- `WIDTH`, `HEIGHT` - Window dimensions |
| 67 | +- `SPRING_LEN` - Number of coil segments in the spring |
| 68 | +- `SPRING_THICKNESS` - Thickness of spring lines |
| 69 | +- `SPRING_ELASTICITY` - Spring width offset for coil visualization |
| 70 | +- `MASS_LENGTH` - Size of the mass block |
| 71 | +- `FLOOR_Y` - Y-position of the floor |
| 72 | + |
| 73 | +## How It Works |
| 74 | + |
| 75 | +The spring is drawn by calculating the vector from the anchor point to the mass position. This vector is normalized to determine the spring's main axis direction. A perpendicular vector is then calculated to define the zigzag offset direction. |
| 76 | + |
| 77 | +For each segment of the spring: |
| 78 | +1. A point is positioned along the main axis based on segment progress |
| 79 | +2. A perpendicular offset is applied, alternating left and right for each segment |
| 80 | +3. This creates the characteristic coil pattern of a spring |
| 81 | + |
| 82 | +The result is a spring that visually stretches and compresses as the mass moves, maintaining realistic proportions and appearance. |
| 83 | + |
| 84 | +## Future Enhancements |
| 85 | + |
| 86 | +Potential improvements to expand the simulation: |
| 87 | + |
| 88 | +- Add physics-based spring motion using Hooke's Law |
| 89 | +- Implement user controls to interact with the mass (drag and release) |
| 90 | +- Add damping and friction effects |
| 91 | +- Multiple masses and springs for complex systems |
| 92 | +- Adjustable spring constants and mass values via UI |
| 93 | +- Real-time physics visualization (force vectors, velocity indicators) |
| 94 | +- Pendulum mode for vertical oscillation |
| 95 | + |
| 96 | +## License |
| 97 | + |
| 98 | +This project is provided as-is for educational purposes. |
| 99 | + |
| 100 | +## Credits |
| 101 | + |
| 102 | +Built with Raylib - A simple and easy-to-use library to enjoy videogames programming. |
0 commit comments