diff --git a/README.md b/README.md new file mode 100644 index 0000000..6221913 --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# Bounce + +A first approach to simulating cloth. It models cloth as a bunch of nodes connected by springs and atracted by gravity to the floor. + +## Features: + * Runge Kutta 4 solver + * Raylib to display the 2D results. + * Runs in real time. (At least for 40x40 = 1600 nodes). + +## To improve: + * Spring's rest length is zero. This makes the cloth want to collapse on itself. + * The code is really ugly. A new more clear way of handeling interactions is needed. + * Add a way to interact with the cloth while the program is running. + * Try to make it 3D + +## Output: +![Output of the cloth simulation. The cloth is only supported by the top corners](./cloth.gif) +Output of the cloth simulation. The cloth is only supported by the top corners. diff --git a/cloth b/cloth index 347f2df..b998e8b 100755 Binary files a/cloth and b/cloth differ diff --git a/cloth.gif b/cloth.gif new file mode 100644 index 0000000..5ca8194 Binary files /dev/null and b/cloth.gif differ diff --git a/main.c b/main.c index 29b2313..98adfaa 100644 --- a/main.c +++ b/main.c @@ -178,14 +178,14 @@ void genParticleGrid(Particle particles[] ,int* Nparticles, int nx, int ny, floa } } } -#define Nx 30 -#define Ny 30 +#define Nx 40 +#define Ny 40 #define DeltaX 20.f #define DeltaY 20.f #define index(i,j) ((i)*Ny + j) int Springs(float x, const float y[], float f[], int size){ - const float K = 10.0f; + const float K = 25.0f; const float dump = 1.0e-1f; const float d0 = DeltaX; const float g0 = -5; @@ -231,12 +231,12 @@ int Springs(float x, const float y[], float f[], int size){ f[size/2 + index(i,j)*2+1] += K*( (y[index(i,j-1)*2+1] - y[index(i,j)*2+1] ) ) - (y[index(i,j)*2+size/2+1])*dump -g0; j = 0; - /* // X coordinate */ - /* f[size/2 + index(i,j)*2] += K*( (y[index(i+1,j)*2] - y[index(i,j)*2] ) + (y[index(i-1,j)*2] - y[index(i,j)*2] ) ) - (y[index(i,j)*2+size/2])*dump; */ - /* f[size/2 + index(i,j)*2] += K*( (y[index(i,j+1)*2] - y[index(i,j)*2] ) ) - (y[index(i,j)*2+size/2])*dump; */ - /* // Y coordinate */ - /* f[size/2 + index(i,j)*2+1] += K*( (y[index(i+1,j)*2+1] - y[index(i,j)*2+1] ) + (y[index(i-1,j)*2+1] - y[index(i,j)*2+1] ) ) - (y[index(i,j)*2+size/2+1])*dump - g0; */ - /* f[size/2 + index(i,j)*2+1] += K*( (y[index(i,j+1)*2+1] - y[index(i,j)*2+1] ) ) - (y[index(i,j)*2+size/2+1])*dump - g0; */ + // X coordinate + f[size/2 + index(i,j)*2] += K*( (y[index(i+1,j)*2] - y[index(i,j)*2] ) + (y[index(i-1,j)*2] - y[index(i,j)*2] ) ) - (y[index(i,j)*2+size/2])*dump; + f[size/2 + index(i,j)*2] += K*( (y[index(i,j+1)*2] - y[index(i,j)*2] ) ) - (y[index(i,j)*2+size/2])*dump; + // Y coordinate + f[size/2 + index(i,j)*2+1] += K*( (y[index(i+1,j)*2+1] - y[index(i,j)*2+1] ) + (y[index(i-1,j)*2+1] - y[index(i,j)*2+1] ) ) - (y[index(i,j)*2+size/2+1])*dump - g0; + f[size/2 + index(i,j)*2+1] += K*( (y[index(i,j+1)*2+1] - y[index(i,j)*2+1] ) ) - (y[index(i,j)*2+size/2+1])*dump - g0; } // BOOTOM LEFT int i = 0; @@ -269,12 +269,18 @@ int main(void){ //particles[index(1,1)].x += 20; //particles[index(1,1)].y += 20; + int pause = 1; SetTargetFPS(60); while (!WindowShouldClose()) { - calculateNextStep_rk4(particles, Nparticles, 0.0f, 0.1f); + if (IsKeyPressed(KEY_SPACE)) { + if (pause) pause = 0; + else pause = 1; + } + if (!pause) { + calculateNextStep_rk4(particles, Nparticles, 0.0f, 0.1f); + } BeginDrawing(); ClearBackground(RAYWHITE); - //DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); drawParticles(particles, Nparticles); EndDrawing(); } diff --git a/makefile b/makefile index 440e63b..501f9e4 100644 --- a/makefile +++ b/makefile @@ -5,7 +5,5 @@ # @version 0.1 all: - cc -o cloth main.c -lm -lraylib && ./cloth - - + cc -o cloth main.c -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 && ./cloth # end