Skip to content

shiro1307/ClothSim2-Verlet-Optimized

Repository files navigation

Cloth Physics Simulation

A real-time cloth physics simulation engine built with JavaScript and Verlet integration. Features dynamic mesh cutting, constraint solving, and comprehensive performance benchmarking.

Features

  • Real-time Verlet physics integration with constraint solving
  • Dynamic mesh topology - cut fabric and watch it separate
  • Mouse-based interactive cloth manipulation
  • Built-in performance monitoring and stress testing
  • 800+ FPS with 5,200 particles (0.8-2.3ms frame time)

Benchmarking

Test Scenarios

Scenario Grid Size Particles Edges Purpose
Baseline 40x20 800 1,520 Standard reference
Heavy 60x30 1,800 3,420 Increased complexity
Very Heavy 80x40 3,200 7,281 Stress test
Multiple Cloths 20x15 (x2) 600 each 1,140 each Multi-system

Performance Results

Metric Baseline Heavy Very Heavy
Avg FPS 1200+ 900+ 826
Avg Frame Time 0.83ms 1.11ms 1.21ms
Min Frame Time 0.60ms 0.75ms 0.80ms
Max Frame Time 1.50ms 1.80ms 2.30ms
Constraint Iterations 4 4 4

Key Metrics

  • Physics update: Sub-millisecond per iteration
  • Constraint solving: 4 passes per frame for stable simulation
  • Memory usage: Efficient Float32Array and Uint16Array storage
  • Scales linearly with particle and edge count

Getting Started

  1. Open index.html in a web browser
  2. Performance stats display in real-time on the right side

Controls

  • Mouse drag: Cut the cloth by dragging across edges
  • 1-4: Switch between test scenarios (Baseline, Heavy, Very Heavy, Multiple Cloths)
  • R: Reset current scenario

Technical Details

Physics Engine: Verlet integration with Gauss-Seidel constraint solving (4 iterations/frame). Distance constraints maintain edge rest lengths. Pinned particles simulate cloth attachment.

Edge Splitting: On cut, two particles are created at the midpoint. Original edge replaced with two half-length edges. New particles disconnect from each other but maintain original connections, causing visible separation.

Performance: Baseline (40x20, 800 particles), Heavy (60x30, 1,800 particles), Very Heavy (80x40, 3,200 particles), Multiple Cloths (two separate systems).

Project Files

  • index.html - Canvas and stats display
  • benchmarkLoop.js - Main loop with performance monitoring
  • gameLoop.js - Standard loop without benchmarking
  • physicsScript.js - Verlet integration and constraints
  • renderScript.js - Rendering and mesh generation
  • toolScript.js - Input handling
  • dataScript.js - Particle and edge data structures

License

Open for educational and personal use.

About

A real-time cloth physics simulation engine built with JavaScript and Verlet integration. Features dynamic mesh cutting, constraint solving, and comprehensive performance benchmarking.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors