Skip to content

Commit

Permalink
Added GIF and README
Browse files Browse the repository at this point in the history
  • Loading branch information
MagiRomanya committed Apr 2, 2022
1 parent 11af819 commit 98c261c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Binary file modified cloth
Binary file not shown.
Binary file added cloth.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 17 additions & 11 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
4 changes: 1 addition & 3 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 98c261c

Please sign in to comment.