# Soft Body Simulation

RK4, spring coef. 8000, damper coef. 20

## Overview

This project simulates a flexible cube falling on the floor, using a framework developed with C++ and OpenGL by GPLab, NCTU. For all the videos of result, please check out the last section.

The cube is constructed by a set of particles, which are connected with 3 kinds of springs. With these particles and springs, we can integrate the spring forces and damper forces to estimate the next positions for each particle. In this case, I implemented Explicit Euler Method and Runge-Kutta 4th-Order Method to solve the differential equations. This is a simple approach to simulate solid deformable objects.

Finally, because it’s a plane there, I also used a simple method to resolve the point-plane collision detection, which can bounce back the cube to the air.

## Mass-Spring System

Cube of Particles

As the image above, these particles constructs the cube. In this simulation, it took 1000 particles to form a 10x10x10 cube. Then, the images below shows each kind of the springs connected between particles.

### Structural Springs

Structural Springs connect from each particle to its 6 adjacent neighbors, including up, down, left, right, forward, backward. These springs establish the basic structure of the cube.

### Shearing Springs

Shearing Springs connect from each particle to its 20 diagonal neighbors. These springs can prevent the cube from excessive shearing.

### Bending Springs

Bending Springs connect from each particle to its 6 second adjacent neighbors, kind of like the structural springs but these springs skip the first neighbor instead. They can prevent the cube from folding all over.

## Integration

### Euler Method

Illustration of the Euler Method (from Wikipedia)

The blue curve in this graph is the unknown real values, while the red one is the approximation by Euler Method. From A0 to A1, this method only takes the status at position A0 to estimate the position after a small time step. In this case, physically, the Euler Method takes the net force at A0 and derive the acceleration to approximate the position at A1 with a small time step.

Because the Euler Method is a first-order method, which means it needs a rather small time step, otherwise the local error (error per step) and the global error (error at a given time) would be very big resulting in unstable system. However, the Euler Method is rather low-cost and also a basic method which is easy to implement.

### Runge–Kutta 4th-Order Method

Illustration of the RK4 (from Department of Physics, Drexel University)

First, we define the following equations (h is the step size, h > 0).

- y
_{n+1}= y_{n}+ (h/6)(k_{1}+ 2k_{2}+ 2k_{3}+ k_{4}) - t
_{n+1}= t_{n}+ h

For k_{1}, k_{2}, k_{3} and k_{4}:

- k
_{1}= f(t_{n}, y_{n}) - k
_{2}= f(t_{n}+ (h/2), y_{n}+ (h/2)k_{1}) - k
_{3}= f(t_{n}+ (h/2), y_{n}+ (h/2)k_{2}) - k
_{4}= f(t_{n}+ h, y_{n}+ hk_{3})

Which means:

- k
_{1}is the increment based on the slope at the**beginning**of the interval, using**y**._{n} - k
_{2}is the increment based on the slope at the**midpoint**of the interval, using**y**._{n}+ (h/2)k_{1} - k
_{3}is the increment based on the slope at the**midpoint**of the interval, using**y**._{n}+ (h/2)k_{2} - k
_{4}is the increment based on the slope at the**end**of the interval, using**y**._{n}+ hk_{3}

The RK4 method is a fourth-order method, meaning that it’s rather high-cost and more precise than the Euler Method. It’s local error is on the order of O(h^{5}), while the global error is order O(h^{4}).

## Collision Detection

There is one thing left to handle is the point-plane collision when the cube is falling right on the plane. For every particle, once it satisfies the following conditions, it would be considered that the collision is happening.

- The particle’s position is close enough to the plane.
- The particle is moving toward the plane.

When the particle and the plane are collided, the system would revert the normal component of the velocity. A coefficient smaller than 1 to represent the energy loss could be applied to the reverted velocity. So this is an simple way to handle the point-plane collision.

## Results

- Euler Method, spring coef. 8000, damper coef. 20

- Euler Method, spring coef. 800, damper coef. 60

- RK4, spring coef. 8000, damper coef. 20

## Leave a Comment