Skip to content
This repository was archived by the owner on Mar 28, 2026. It is now read-only.

ucc-et/cloth-sim

Repository files navigation

Cloth Simulation (Rust + SDL2)

This project is a cloth-simulation written in Rust using SDL2 for rendering and input capturing. This was implemented for the course Introduction to computational physics tought by Prof. Dr. Michels @ TU Darmstadt. This is my first Rust project, so some things are intentionally verbose, commented or structured in a way that made it easier to understand for me.

Cloth Demo GIF

How to run

You need:

Then:

cargo run

There is also a binary attached called 'cloth-simulation'. That can be executed via terminal (if rust and the needed packages are installed)

./cloth-simulation

If you run cargo build --release it will create a release folder in the target folder, there you can access your own executable


Features

  • Simulate a 2D cloth using Verlet integration
  • Use distance constraints to model fabric behavior
  • Supports:
    • gravity
    • damping
    • stiffness
    • bending resistence
    • grabbing via left-mouse-click
    • tearing via right-mouse-click
  • when running this application, you will have two windows:
    • one for the simulation
    • one for parameter control and performance statistics

Controls

Physics Simulation Window

  • Left Mouse - grab and pull cloth
  • Right Mouse - tear the sticks
  • ESC - quit

Control Window

  • ↑ / ↓ - select parameter to adjust
  • ← / → - change the value of the parameter
  • R - rebuild cloth with current settings

How the simulation works

Particles

Each cloth point is a particle that stores:

  • its current position
  • its previous position
  • acceleration

→ velocity is not explicitly set anywhere

Verlet Integration

instead of Euler integration, where each point stores its position, and a velocity, this project implements Verlet integration. Verlet integration is more stable for constraint-based systems like in this case, the cloth.

The update rule for a particle is:

$x_{n+1} = x_n + (x_n - x_{n-1}) + a * \Delta t^2 $

Where:

  • $x_n$ is the current position
  • $x_{n-1}$ is the previous position
  • $a$ is teh acceleration (which in this case is either gravity or mouse force)
  • $\Delta t^2$ is the time step

Velocity is then 'implicitly' sotred as: $v \approx x_n - x_{n-1}$

Constraints

The constraints that are used here are the sticks, which connect two particles. Each stick enforces that the distance between two particles is the rest_length defined for each. The enforcing of the constraints is applied by adjusting the particle positions directly. To simulate the stiffness of the cloth, constraints are solved multiple times per frame (constraint_iterations).

in this project currently are two types of constraints. The structural constraints, which form the grid, but also bending constraints, which are used for longer-ranging connections.

This approach makes the cloth more stable, and is also used in games and other real-time simulations (TODO: source).

Performance

The control window shows some performance metrics like FPS, frame time and physics update time.

References / Sources

About

This repository contains a cloth simulation in rust and sdl2. It was developed for a course at TU Darmstadt called 'Introduction to computational physics'

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages