Skip to content

Commit f92fcc9

Browse files
committed
A few small changes and add README.md
1 parent 0c42acc commit f92fcc9

3 files changed

Lines changed: 109 additions & 7 deletions

File tree

README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Spring Mass Simulation
2+
3+
![Simulation Screenshot](image.png)
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.

image.png

16.1 KB
Loading

spring.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
#include "raylib.h"
33
#include <math.h>
44

5-
#define FPS 120
5+
#define FPS 100
66
#define WIDTH 960
77
#define HEIGHT 680
88
#define THICKNESS 5
99
#define MASS_LENGTH 100
1010
#define FLOOR_Y (HEIGHT*0.6)
11-
#define SPRING_LEN 20
11+
#define SPRING_LEN 30
1212
#define SPRING_THICKNESS 2
1313
#define K 50
14-
#define X_REST 300
14+
#define X_REST 350
1515

1616

1717

1818
float mass_x = 600;
1919
float v = 30;
2020
float a = 2;
21-
float f = 0.3;
21+
float f = 0.5;
2222

2323
void drawSpring()
2424
{
@@ -73,7 +73,7 @@ int main()
7373

7474
{
7575

76-
InitWindow(WIDTH, HEIGHT, "ElasticCore: A Spring on Mass Simulation");
76+
InitWindow(WIDTH, HEIGHT, "ElasticCore: A Mass on Spring Simulation");
7777

7878
SetTargetFPS(FPS);
7979
float dt;
@@ -91,8 +91,8 @@ int main()
9191
mass_x += v * dt;
9292
drawMass();
9393
drawFloor();
94-
DrawText("Spring Mass Simulation", WIDTH/10, 80, 30, RED);
95-
printf("%f\n", mass_x);
94+
DrawText("Mass on Simulation", WIDTH/10, 80, 30, RED);
95+
9696
EndDrawing();
9797
}
9898

0 commit comments

Comments
 (0)