# T5 Collision Detection with a Sphere

Search \[TODO 4] in ClothSim.cpp

Here we are using a sphere of radius 4.0 centred at (0, 4.0f, 0.0f).

```cpp
				// Advanced: Test Sphere Intersection
		float sphere_radius = 4.0f;
		float sphere_friction = 0.8f;
		glm::vec3 sphere_center = glm::vec3(0, 4.0f, 0.0f);
		
		pos = mesh->vertices[v].pos;

		// [TODO 4]: sphere intesection
		// 4.1 replace false with checking if the vertex position falls into the sphere
		if ( false ) {

			// if it is true: 
			// push back the vertex position back to the sphere surface

			// 4.2 calculate the outward vector direction
			// from the sphere center to the vertex position pos
			// glm::vec3 outDir = ???;

			// 4.3 normalise the vector using glm::normalize(glm::vec3)
			// glm::vec3 outDirUnit = ???;

			// 4.4 push back the vertex position back to the sphere surface
			// using sphere_center, outDirUnit and sphere radius
			// mesh->vertices[v].pos = ???;

			// 4.5 downscale the vertex velocity using sphere_friction (< 1)
			// velocities[v] *= ???;
		}
```

#### Check if your vertex position falls into the sphere

We can check that by calculating the distance between the vertex and the sphere center.

Hint: use glm::length(glm::vec3) to calculate the distance

#### If the vertex position falls into the sphere

Follow step 4.2 to 4.4 to recover the vertex position back to the sphere surface.

4.2 and 4.3: calculate direction from the sphere center to the current vertex postion

4.4 Move back the vertex to the sphere surface

4.5 Reduce the vertex velocity by a factor of sphere\_friction ( 0.8 in the code)

### Build and Run

Build and run your programme.

Pressing the **Space Bar**, you are going to see something like the following:

<figure><img src="https://3464970502-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JUKGJZ67JX02QZdPhsy%2Fuploads%2FnPfULEJKKU3heeBmQmrm%2Fimage.png?alt=media&#x26;token=b0df4c41-69f6-4444-8053-e1b333ba77ec" alt="" width="375"><figcaption></figcaption></figure>

<figure><img src="https://3464970502-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JUKGJZ67JX02QZdPhsy%2Fuploads%2FtXA415qtGfKEmt8V7Xui%2Fimage.png?alt=media&#x26;token=7b8a14c3-a65f-407f-8902-99f2fb364188" alt="" width="375"><figcaption></figcaption></figure>

Press v to enable wind, you are going to see the following:

<figure><img src="https://3464970502-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JUKGJZ67JX02QZdPhsy%2Fuploads%2FyfHjkWNECNooUj8ZbqEP%2Fimage.png?alt=media&#x26;token=25328105-8a0a-405d-8b8d-18c99a59d448" alt="" width="375"><figcaption></figcaption></figure>

Change the ground boundary checking after TODO 3 from -5.0 to 0.0 leading to the following. You can play with different sphere radius and center position.

```cpp
		if (mesh->vertices[v].pos.y < 0.0)
			mesh->vertices[v].pos.y = 0.0;
```

<figure><img src="https://3464970502-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JUKGJZ67JX02QZdPhsy%2Fuploads%2FAS988sj3SiXdzsEKUJDQ%2Fimage.png?alt=media&#x26;token=5c2bb3f8-5a93-45a1-9703-da850a3fc51c" alt="" width="375"><figcaption></figcaption></figure>
