This lecture covers the basic usage of PhysicsMaterials and the MassOverride component.
Learning Objectives
- Creating and applying PhysicsMaterials
- Basic physics concepts (Mass, Volume, Density, Restitution and Friction)
- Using the MassOverride component
Level Setup
- Command : New Project
- Create a new project using the {nav icon=clone, name=Empty 2D Project} template
- Command : CreateSprite
- Select : Sprite object
- In the
Properties Window
- Rename Sprite object to
RedSquare
- Under Transform
- Set Translation to
[-3, 0, 0]
- Under Sprite
- Set VertexColor to
Red: [R:255, G:0, B:0, A:1.00]
- Add Component : RigidBody
- Under RigidBody
- Set Velocity to
[4, 0, 0]
- Add Component : BoxCollider
- Duplicate the RedSquare object object
- In the
Properties Window
- Rename it to
BlueSquare
- Under Transform
- Set Translation to
[3, 0, 0]
- Under Sprite
- Set VertexColor to
Blue: [R:0, G:0, B:255, A:1.00]
- Under RigidBody
- Set Velocity to
[-4, 0, 0]
- Select : LevelSettings object
- In the
Properties Window
- Under GravityEffect
- Set Active checkBox to
false
- Under DragEffect
- Set Active checkBox to
false
- Command : PlayGame
Mass
In Zilch Engine, any object with a RigidBody is automatically assigned mass. An object's mass represents its weight and is determined by its Volume and Density (M = D * V
). Volume is calculated from a combination of the Collider type along with its size defining properties and the object's scale, while its Density is defined by the object's PhysicsMaterial. By altering these properties we can manipulate how objects interact physically with each other.
NOTE: An object with RigidBody but with no Collider will be assigned a mass of 1 while an object with a Collider but no RigidBody can be thought of as having infinite mass.
In our constructed scenario so far we see two objects colliding with each other in a space with no gravity or drag. A quick look on each object's RigidBody component shows the current Mass . Since both objects have the same physics properties (mass, restitution, velocity magnitude, etc) we conclude that the collision is symmetric and both objects apply an equal force to each other when they collide.
Volume
Let's explore what happens when we give them different Volumes:
- Select : RedSquare object
- In the
Properties Window
- Under Transform
- Set Scale to
[2, 2, 2]
- Command : PlayGame
Since we've made the object bigger, its mass has also increased which we can verify by checking the RigidBody on RedSquare object. We can observe that the more massive object imparts a larger force onto the smaller object than the smaller does to it.
In the next section, we'll explore tweaking an object mass by altering its density.
Density
- Command : Add Resource
- Create a PhysicsMaterial named
Dense
- In the
Library Window
- Under PhysicsMaterial
Double-Click
Dense resource- In the
Properties Window
- Set Density to
8
- Select : RedSquare object
- In the
Properties Window
- Under Transform
- Set Scale back to
[1, 1, 1]
- Under BoxCollider
- Set Material resource to
Dense
Similar to the previous case, the more massive object imparts a greater force despite both objects having the same volume. Lastly, let's look into another way of setting an object's mass.
MassOverride
The MassOverride component allows you to set an object's mass regardless of its Volume or Density. It has a dependency on RigidBody.
MassOverride Properties | |
---|---|
Active checkBox | Whether this component is currently overriding the RigidBody's mass |
Mass | The value RigidBody's mass is being overrided by) |
RecomputeMass | Recalculates and sets MassOverride's mass property to the default value |
- Select : RedSquare object
- In the
Properties Window
- Add Component : MassOverride
- Command : PlayGame
In adding the MassOverride component, we just reset the RedSquare object's mass to 1 despite it still having the Dense resource PhysicsMaterial
Restitution
Another useful feature of PhysicsMaterial is Restitution: a value that represents the object's bounciness. It is represented by a value from 0 to 1 and denotes how much of its energy is preserved after the collision (0 being no energy preserved and 1 being 100% of energy preserved). You may also set Restitution to a value greater than 1 (by right-clicking the field) to cause the object to bounce away with greater energy.
Create a PhysicsMaterial named
Bouncy
In the
Library Window
Under PhysicsMaterial resource
Double-Click
Bouncy resourceIn the
Property Window
Set Restitution to
1
Set RestitutionImportance to
1
Create a PhysicsMaterial named
NotBouncy
In the
Library Window
Under PhysicsMaterial resource
Double-Click
the NotBouncy resource resourceIn the
Property Window
Set Restitution to
0
Set RestitutionImportance to
1
Select : BlueSquare resource
In the
Properties Window
Under BoxCollider
Experiment by setting PhysicsMaterial enum to
Bouncy
andNotBouncy
Friction
Friction is a coefficient that represents how much energy is lost when objects slide against one another. Each PhysicsMaterial provides a friction value and their combination will determine the friction force used for the collision between the two objects. The higher the friction value, the more energy will be lost per frame except when either object has a friction value of 0, in which case no energy will be lost.
NOTE: Similar to the case with restitution, PhysicsMaterials have a FrictionImportance property, which determines which object's Friction value to use in a collision.
Select : LevelSettings object
In the
Properties Window
Under GravityEffect
Set Active to
true
Create a PhysicsMaterial named
HighFriction
In the
Library Window
Under PhysicsMaterial resource
Double-Click
HighFriction resourceIn the
Property Window
Set Friction to
2
Set FrictionImportance to
1
In the
Properties Window
Rename Sprite object to
GreenSquare
Under Sprite
Set Sprite to
Green: [R:0, G:255, B:0, A:1.00]
Under Transform
Set Translation to
[0, -1, 0]
Set Scale to
[10, 0, 0]
Add Component : BoxCollider component
Under BoxCollider
Set Material resource to
HighFriction
Related Materials
Manual
Reference
Classes
Commands
Development Tasks
- T2562
- T141