Applying Physics to Entities 

# Course Introduction

Let's apply the laws of physics to entities and make them collide with each other. For how to create physics in your world, see [Using Physics](/docs?postId=757%7B%22target%22:%22_self%22%7D).

# PhysicsRigidbodyComponent

If you add a PhysicsRigidbodyComponent to an entity, it will be controlled by the physics engine. You can also set the values that influence physics calculation such as the entity's coefficient of resistance, coefficient of friction, mass value, etc. As long as an entity has PhysicsRigidbodyComponent and PhysicsColliderComponent, it can collide with other entities that obey the laws of physics. The main properties of PhysicsRigidbodyComponent are as follows.

[{"Property":"BodyType","Explanation":"Static: Treated as an entity with an infinitely large mass and can collide with Dynamic types. Dynamic: Entities behave according to the laws of physics. It can collide with other Entities that can collide with physics.Kinematic: Treated as an entity with an infinitely large mass, but unlike the Static type, it can move by specifying a speed."},{"Property":"Mass","Explanation":"Sets the mass value. Calculates the density value using the values of the area and mass of the Collider."},{"Property":"Friction","Explanation":"Determines the coefficient of friction. The smaller the value is, the better it slides."},{"Property":"Restitution","Explanation":"Sets the Entity's coefficient of restitution. 0 is close to a perfectly inelastic collision, and 1 is close to a perfectly elastic collision."},{"Property":"AngularDamping","Explanation":"Sets the coefficient of resistance to changes in angular velocity (rotational speed) when an entity moves in a circle. The larger the value is, the more force is required to change the angular velocity."},{"Property":"LinearDamping","Explanation":"Sets the coefficient of resistance to linear velocity changes when performing linear motion. The larger the value, the more force is required for a linear velocity change."},{"Property":"CollisionDetectionMode","Explanation":"Sets the detection method for physics collisions.Discrete: Checks for collisions intermittently. Depending on the speed and size of the object, collisions may not be detected. The higher the speed or the smaller the size, the more likely it will pass without detecting a collision.Continuous: Continually checks for collisions. Prevents objects from passing through without colliding. This computation requires more resources."},{"Property":"FixedRotation","Explanation":"If true, the object does not rotate due to physics interactions."},{"Property":"GravityScale","Explanation":"Sets how much the entity is affected by the gravity value of PhysiecsSimulatorComponent. When the Gravity value is (0, -10), if the GravityScale value is 1, the gravity value that the entity received is (0, -10), and if the GravityScale value is 2, the gravity value is (0, -20)."},{"Property":"SleepingMode","Explanation":"Sets the sleep mode.StartAwake: Starts out awake, but can convert to sleep.StartAsleep: Starts in a sleeping state, but wakes up on impact.NeverSleep: Unavailable to convert to sleep mode."}]

> Learn more
> 
>   * Coefficient of Restitution: The ratio of the relative velocities of two colliding objects before and after the collision.
>   * Inelastic Collision: Kinetic energy is lost upon collision.
>   * Perfectly Inelastic Collision: Kinetic energy is lost upon collision, and after collision, the two objects move as one.
>   * Perfectly Elastic Collision: A collision in which kinetic energy is conserved upon impact.
> 


# Function Description

Depending on the PhysicsRigidbodyComponent function, you can apply a force to an entity in a specific direction to make it move or perform a circular motion (rotational motion). The ApplyForce series function and the ApplyImpulse series function are affected by the mass value of the physical rigid body corresponding to the Entity. The velocity setting functions in the SetVelocity category can specify a velocity independent of the mass value.

#### Force-Applying Functions

The `ApplyForce()` and `ApplyTorque()` functions can apply a continuous force. This is when you want to continuously apply force in a specific direction by pressing a specific key. The parameter force means force. It is affected by the mass value of the physics rigid body corresponding to that Entity.

#### Impulse-Applying Functions

The `ApplyAngularImpulse()` and `ApplyLinearImpulse()` functions can temporarily apply short shocks. This is related to short-impact actions, such as firing a cannon or swinging a bat. It is affected by the mass value of the physics rigid body corresponding to that entity. The parameter Impulse refers to the amount of impulse and is used to temporarily apply a short shock.

#### Velocity-Setting Functions

The `SetAngularVelocity()` and `SetLinearVelocity()` functions can set the velocity of an entity independent of its mass value.

# PhysicsColliderComponent

You can size the entity's colliding body and set an offset. It is mainly used with PhysicsRigidbodyComponent, but entities used without PhysicsRigidbodyComponent are treated as a Static type. Refer to [Entity Collision] for how to set the type, size, and offset of the colliding body (/docs/?postId=175%7B%22target%22:%22_self%22%7D).

[{"Property":"ColliderType","Explanation":"Depending on the classification, you can set the shape of the colliding body differently. Circle: Creates a circular colliding body. Box: Creates a rectangular colliding body.Polygon: Creates a polygonal colliding body."},{"Property":"Collider","Explanation":"Pressing the Edit button can set the size of the colliding body."},{"Property":"CollisionGroup","Explanation":"Sets up a collision group that can determine if there is a collision or not. You can set it up by creating a new collision group."},{"Property":"Density","Explanation":"Sets the density value to use when UseDensity of the attached PhysicsRigidbodyComponent is true."},{"Property":"UseCustomPhysicalProperties","Explanation":"Friction and Restituion values are applied to physics rigid bodies. If false, the value of the connected PhysicsRigidbodyComponent is used."},{"Property":"EnableContactEvent","Explanation":"Sets whether PhysicsContactBeginEvent and PhysicsContactEndEvent occur. If false, neither Event occurs."},{"Property":"IsSensor","Explanation":"If true, no physical interaction will occur, but a collision event will occur."},{"Property":"ClientOnly","Explanation":"If true, physics operation occurs in the Client, and functions and property changes are possible in the Client space. Physics calculation results are not synchronized with other Clients. If false, functions and property changes are possible in the Server space. Physics calculation results are synchronized with other Clients."}]

#### Differences in Physics Calculations by Execution Space

Most of PhysicsRigidbodyComponent's Property and Function is divided into the available and accessible execution space depending on PhysicsColliderComponent's ClientOnly property value. They are computed in their respective physics Worlds according to ClientOnly, so they do not collide or affect each other.

  * If ClientOnly is True: The physics rigid body of the Entity is generated only in the Client, and the physics operation is calculated in each Client. Results are not synchronized with other Clients. It is possible to use related functions and change most of the properties only in the Client.
  * If ClientOnly is False: The physics rigid body of the Entity is generated in the Server and the Client, and the location of the physics rigid body is synchronized between the Server and all Clients according to the result of the physics calculation. It is possible to use related functions and change most properties only in the Server.



# Setting Collision Group

By setting a collision group, you can set whether or not entities to which the laws of physics apply collide with each other according to your needs. Specify the CollisionGroup of PhysicsColliderComponent and select whether or not the groups collide. When you start the world after creating gravity on the map and placing a dynamic type physics entity with various elasticity on top of the map, the entities will descend and bounce off each other. For details on how to set up a collision group, see [Creating Collision Groups](/docs?postId=754%7B%22target%22:%22_self%22%7D).

# Example

You will need to apply physics to the world using PhysicsInteractable from PhysicsSimulatorComponent and TileMapComponent to see the example.

#### Throwing Objects on the Floor into the Air

Let's throw an entity placed on the ground into the air when the J key is pressed. Entities that are raised will fall to the bottom due to gravity. [gif](16601123764643cfe3adec2534fb68a22b98573a3713c)

  1. Place the object with PhysicsRigidbodyComponent and PhysicsColliderComponent added on the scene.
  2. After generating a new script component, write the following to apply force to move the object to the entered location when J is pressed.


    
    
    @Component
    script JumpController extends Component
        method void Jump()
        self.Entity.PhysicsRigidbodyComponent:ApplyLinearImpulse(Vector2(0,10))
        end
    
        @EventSender("Service", "InputService")
        handler HandleKeyDownEvent(KeyDownEvent event)
        --------------- Native Event Sender Info ----------------
        -- Sender: InputService
        -- Space: Client
        ---------------------------------------------------------
    
        -- Parameters
        -- local key = event.key
        ---------------------------------------------------------
    
        if key == KeyboardKey.J then
            self:Jump()
        end
        end
    end
    

#### Colliding Physics Entities with Collision Groups

  1. Place several objects with PhysicsRigidbodyComponent and PhysicsColliderComponent added, and set each Mass and Restitution values differently.
  2. Add a new physics group to CollisionGroup and set them to collide with each other as shown below. [png](16601112357375799a6ba9d5e4f17b1d7096d2d0e02ba)
  3. Click Start to see if entities collide with each other. [gif](16601137220417735c37efe64470ab00e710e1c460242)



##### Reference Guide

  * [Using Physics](/docs?postId=757%7B%22target%22:%22_self%22%7D)
  * [Creating Collision Groups](/docs?postId=754%7B%22target%22:%22_self%22%7D)
  * [Using Various Physics Joints](/docs?postId=760%7B%22target%22:%22_self%22%7D)