# Physics 2D

## Applying Physics Preview

### What kind of animation can be created by applying physics?

* The built-in Physics Engine in Unity handles the physics for gameobject interactions and the various effects like **gravity, acceleration, collisions**, etc.
* For the NFT avatar, you might want to use the hinge joint for the hair simulation or the spring joint for jiggling

### Overview: How to apply physics in Unity

* In order to create these types of simulations, you will need to implement 2D physics something that Unity excels at.

{% tabs %}
{% tab title="Hinge joint2D" %}

### Hinge joint2D - ENG Tutorial

{% embed url="<https://youtu.be/abJShSnAujU>" %}
Hinge Joint2D (ENG)
{% endembed %}

### Hinge joint2D - KOR Tutorial

{% embed url="<https://youtu.be/DPxsJFzpCco>" %}
Hinge Joint2D (KOR)
{% endembed %}
{% endtab %}

{% tab title="Spring joint2D" %}

### Spring joint2D - ENG Tutorial

{% embed url="<https://youtu.be/02crLOoyWiE>" %}
Spring Joint2D - part 1 (ENG)
{% endembed %}

{% embed url="<https://youtu.be/RMyf6LLiQGs>" %}
Spring Joint2D - part 2 (ENG)
{% endembed %}

### Spring joint2D - KOR Tutorial

{% embed url="<https://youtu.be/_0MSv1Sde10>" %}
Spring Joint2D - part 1 (KOR)
{% endembed %}

{% embed url="<https://youtu.be/Xzgjl3Y2hMs>" %}
Spring Joint2D - part 2 (KOR)
{% endembed %}
{% endtab %}
{% endtabs %}

## Settings for specific cases

### Case 1: an example of physics acting in the same direction as gravity

1. Bones are set up and positioned at an irregular location
2. No <mark style="color:purple;">`2D Box Collider`</mark> is needed for the head bone

#### Setup

* Apply weight of the helmet part to static or higher hierarchy bone in this case, <mark style="color:purple;">`BoneHead.`</mark>
* Apply evenly distributed weight to the strap with the desired number of bones. 6 bones are used to execute a chain-like movement.

<figure><img src="https://1381431913-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6CBs7vCLqptAiFUTrCYl%2Fuploads%2FphCUZlahLq2biUKhVktw%2FImage%20-%2032.png?alt=media&#x26;token=4d86e18d-a8fe-45a0-8bd8-80a87f28122f" alt=""><figcaption><p>Helmet bone hierarchy example</p></figcaption></figure>

#### Problem

* The root bone of the strap becomes disjointed when the scene is played.
  * This is caused when the <mark style="color:purple;">`Body Type`</mark> of the <mark style="color:purple;">`[Rigidbody 2D]component`</mark> is set to <mark style="color:purple;">`Dynamic`</mark>

{% embed url="<https://youtu.be/MHo4ziHD2dQ>" %}
Problem - the root bone of the strap is disjointed with helmet
{% endembed %}

#### Solution

* Apply <mark style="color:purple;">`Static`</mark> for <mark style="color:purple;">`Body Type`</mark>
* This will result root bone of the strap following its mother bone when the scene is played.

<figure><img src="https://1381431913-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6CBs7vCLqptAiFUTrCYl%2Fuploads%2Fqsppa0c8sKtMO24E5Hhu%2FImage%20-%2033.png?alt=media&#x26;token=a1bcc630-f697-4f97-94d4-d55613f6c891" alt=""><figcaption><p>Body Type: Dynamic -> Static</p></figcaption></figure>

{% embed url="<https://youtu.be/i0rpQsM3j7Q>" %}
Bones behave correctly along with helmet
{% endembed %}

#### Finessing edges of the sprite image

* **Problem**
  * by Default, in <mark style="color:purple;">`[Hinge Joint 2D]`</mark>, there’s no limit is set in angles that will cause breaking edges when the avatar is projecting dynamic movements.
* **Solution**
  * Apply angle limits by enabling the check box of <mark style="color:purple;">`Use Limits`</mark> and input values in <mark style="color:purple;">`Angle Limits`</mark>
* For this particular image, It’s best to have a direct child bone, which is the 2nd bone of the strap, and leave other bones under it without any <mark style="color:purple;">`Angle Limits`</mark> applied.

{% embed url="<https://youtu.be/FEF3NphHj-I>" %}
Problem - The edges of the strap look broken because there is no limit on the angle of movement.
{% endembed %}

{% embed url="<https://youtu.be/6R7-Fd9x4wA>" %}
By limiting the angle of movement of the strap, the strap edges are not broken.
{% endembed %}

### Case 2: an example of physics acting in the opposite direction to gravity.

1. Bones are set up and positioned at an irregular location
2. Needs to bleed out of the scene
3. Needs to float upwards

**Setup**

* Apply weight to the head part to static or higher hierarchy bone in this case, <mark style="color:purple;">`BoneHead.`</mark>
* Apply evenly distributed weight to the rainbow with the desired number of bones. 3 bones are used to execute a chain-like movement.

**Finessing for the expected outcome**

* To bleed out the image outwards, reposition the last bone upwards
* Since the Rainbow needs to float, set <mark style="color:purple;">`Gravity Scale`</mark> to a negative value in <mark style="color:purple;">`[Rigidbody 2D]`</mark>
* It’s best to have direct child bone, which is the 1st bone of the rainbow, in <mark style="color:purple;">`Static`</mark> for <mark style="color:purple;">`Body Type`</mark> in <mark style="color:purple;">`[Rigidbody 2D]`</mark>
* Apply angle limits for the remaining bones by enabling the check box of <mark style="color:purple;">`Use Limits`</mark> and input values in <mark style="color:purple;">`Angle Limits`</mark>

<figure><img src="https://1381431913-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6CBs7vCLqptAiFUTrCYl%2Fuploads%2FvU4Nhi2MXkVl13oLT4kq%2FImage%20-%2034.png?alt=media&#x26;token=ea94c9f8-867c-4e4a-ab5e-6e6d23381f8f" alt=""><figcaption><p>Enter a negative value for Gravity scale</p></figcaption></figure>

<figure><img src="https://1381431913-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6CBs7vCLqptAiFUTrCYl%2Fuploads%2FsHshfo8R1plbFOdttweU%2FScreenshot%25202022-07-14%2520085600.png?alt=media&#x26;token=37d7442e-62a5-4349-a289-42d18c02d1fb" alt=""><figcaption><p>For 1st bone of chain-movement, set 'Body Type' to 'Static'</p></figcaption></figure>

**Result**

* Rainbow bleeds out
* Limited angle set to minimize movements of the rainbow

{% embed url="<https://youtu.be/5Zx0HNEJ2Lk>" %}
Rainbow is acting in the opposite direction to gravity
{% endembed %}
