# T4 Add Spring Force using Hooke's Law

#### Find getSpringForce

Search \[TODO 1] in ClothSim.cpp, you are expected to find the following:

```cpp
// TODO: calculate spring force
glm::vec3 ClothSim::getSpringForce(int direction, int id) {

	// the rest spring length
	float lenRest = 0;
	if (direction == DIRS.NORTH || direction == DIRS.SOUTH) lenRest = restLengthZ;
	else if(direction == DIRS.WEST || direction == DIRS.EAST) lenRest = restLengthX;
	else lenRest = restLengthXZ;

	// the spring vector
	glm::vec3 vSpring = mesh->vertices[id].pos - mesh->vertices[getId(direction, id)].pos;
	// the spring length
	float lenSpring = glm::length(vSpring); // distance
	
	// the unit vector of spring direction
	glm::vec3 vSpringUnit = vSpring /lenSpring;

	// [TODO 1]: calculate spring force vector based on Hooke's law F = -k delta_x
	// 1.1 calculate the length change delta_len
	// replace 0 with your formula
	float delta_len = 0;

	// 1.2 Use Hooke's law F = -k * delta_len * vSpringUnit
	// k : spring_factor
	// replace glm::vec3(0.0) with your formula 
	glm::vec3 spring_force = glm::vec3(0.0);
	return spring_force;
}
```

#### Calculate the spring force using Hooke's Law

Complete 1.1 and 1.2 using the Hooke's Law

<figure><img src="https://3464970502-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JUKGJZ67JX02QZdPhsy%2Fuploads%2FnrkwERVLWTXz01MUEPIP%2Fimage.png?alt=media&#x26;token=1ac7be6b-054a-41d4-962e-973193865c83" alt=""><figcaption></figcaption></figure>

#### Add spring force in \[TODO 2] vertex forces

variable name

spring force: spring

Add that to forces\[v] under \[TODO 2]

#### Build and Run

Press the Space Bar, you should be able to see the cloth simulation and finally it looks like the following

<figure><img src="https://3464970502-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JUKGJZ67JX02QZdPhsy%2Fuploads%2Fxwsw5eA9Ym5nur7aqEvM%2Fimage.png?alt=media&#x26;token=e17f9d1d-3419-4e3c-a62e-c24247789015" alt="" width="375"><figcaption></figcaption></figure>

In another view

<figure><img src="https://3464970502-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JUKGJZ67JX02QZdPhsy%2Fuploads%2FifyMwPLimgo5pCyYtSUB%2Fimage.png?alt=media&#x26;token=51e51ff5-a294-46c3-b106-633f4c4f4939" alt="" width="375"><figcaption></figcaption></figure>

#### Add the wind

You can also add the wind force under \[TODO 2], press Space Bar and press v, you are going to see the effects of the wind force.

<figure><img src="https://3464970502-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JUKGJZ67JX02QZdPhsy%2Fuploads%2FEoe7nz1PP8C8nxhDPSoX%2Fimage.png?alt=media&#x26;token=c14f3557-70bd-4333-a424-476f6ba0f2d5" alt="" width="375"><figcaption></figcaption></figure>
