Unreal Engine Physics Essentials - Sample Chapter
Unreal Engine Physics Essentials - Sample Chapter
"Community
Experience
Distilled"
E x p e r i e n c e
D i s t i l l e d
Sa
m
pl
C o m m u n i t y
Devin Sherry
Katax Emperore
Unreal Engine
Physics Essentials
ee
Katax Emperore
Devin Sherry
board games, science magazines, comic books, and graffiti painting. Katax was
introduced to the IT world when he got his first game platform, Fire Attack from the
Game & Watch series by Nintendo, back in the 80's. While spending hours on it, he
became curious about the process of creating games.
As a teenager, Katax owned Amiga 500 by Commodore on which he played
hundreds of games. However, one stuck with him: Shadow of the Beast by
Psygnosis. He was enamored by the quality of the graphics, music, and FX sounds
involved in the game. Katax realized that he would like to learn to create such
games, and this was the first step. Today, he can design and develop any game on
various web pages and platforms alike.
Devin Sherry is originally from Levittown, Long Island in the state of New
York, USA. He studied game development and game design at the University
of Advancing Technology where he obtained a bachelor's degree of arts in game
design in 2012.
During his time in college, Devin worked as a game and level designer with a group
of students called Autonomous Games on a real-time, strategy style, third-person
shooter game called The Afflicted using Unreal Engine 3/UDK. It was presented at
the Game Developers Conference (GDC) in 2013 in the the GDC Play showcase.
Currently, Devin works as an independent game developer located in Tempe,
Arizona, where he works on personal and contracted projects. His achievements
include the title Radial Impact, which can be found in the Community Contributions
section of the Learn tab of Unreal Engine 4's Launcher. You can follow Devin's work
on his YouTube channel, Devin Level Design, where he educates viewers on game
development within Unreal Engine 3, UDK, and Unreal Engine 4.
Preface
Giving readers practical insight into the principles of mathematics and physics
necessary to properly implement physics within Unreal Engine 4, this book covers
everything one needs to know in order to create a game world.
Discover how to manipulate physics within Unreal Engine 4 by learning from
scratch the basic real-world concepts in mathematics and physics that assist in the
implementation of physics-based objects in your game world. Then, be introduced to
PhAT (Physics Asset Tool) within Unreal Engine 4 to learn more about developing
physics objects for your game world. Next, dive into Unreal Engine 4's collision
generation, physical materials, blueprints, constraints, and more to get hands-on
experience with the tools provided by Epic Games to create the effect of the real
physical world in Unreal Engine 4. Lastly, you will create a working Vehicle Blueprint
that uses all the concepts covered in this book, and also cover topics related to
advanced physics.
Preface
Chapter 6, Materials, discusses physical materials and how to utilize them to create a
realistic game world.
Chapter 7, Creating a Vehicle Blueprint, takes a look at applying the skills learned in the
previous chapters to create the physical body and blueprint of a working vehicle.
Chapter 8, Advanced Topics, covers advanced topics and troubleshooting techniques
in physics.
For the purpose of this chapter, we will want to open Unreal Engine 4 and create
a simple project using the First Person template by following these steps.
Let's start by first launching the Unreal Engine launcher and choosing Launch from
the Library tab, as seen in the following image:
For the sake of consistency, we will use the latest version of the editor. At the time
of writing this book, the version is 4.7.6. Next, we will select the New Project tab
that appears at the top of the window, select the First Person project template with
Starter Content, and name the project Unreal_PhyProject:
[2]
Chapter 1
Now that we have the game engine open, we can now continue with our lesson.
Units of measurement
To begin this section, we want to first define measurement and what exactly we
will measure in the context of Unreal Engine 4. In a general sense, the definition of
measurement is determining the size, length, or the amount of something (such as
distance), the length/width/height of an object, or the volume of a particular space.
In the context of Unreal Engine 4, we will measure the lengths of each component of
a 3D vector in the 3D space and the X, Y, and Z dimensions. For the 2D game world,
we will measure the X and Y axes. In the real world, we can use the U.S. Standard
and the European Standard units of measurement to measure distance.
In the U.S., we can use the standard of lengths that involve the use of inches (in), feet
(ft), yards (yd), and miles, whereas in Europe, there is the standard of lengths in place
that includes millimeters (mm), centimeters (cm), meters (m), and kilometer (km).
For our convenience and as a point of reference, here are a set of conversion charts
between the U.S. and the European units of measurements. For more conversions,
refer to this free conversion website at http://converter.eu/length/.
[3]
12 in
1 in
0.0833333 ft
1 yd
3 ft
1 yd
36 in
1 Mile
1,760 yd
1 Mile
5,280 ft
1 Mile
63359.999 in
0.1 cm
1 cm
10 mm
1 cm
0.0099999 m
1m
100 cm
1m
1000 mm
1 mm
0.000999999 m
1 km
1000000 mm
1 km
100000 cm
1 km
1000 m
25.4 mm
1 in
2.54 cm
1 in
0.0254 m
1 in
0.0000254 km
1 ft
304.8 mm
1 ft
30.48 cm
1 ft
0.3048 m
1 ft
0.0003048 km
1 yd
914.4 mm
1 yd
91.44 cm
1 yd
0.9144 m
1 yd
0.0009144 km
[4]
Chapter 1
1 Mile
1609344 mm
1 Mile
160934.4 cm
1 Mile
1609.3439999999998 m
1 Mile
1.609344 km
The value of 100.0 in this property equates an Unreal Unit to 1 cm. For example,
by changing the World to Meters property to the value of 1 (as shown before), it
will equate 1 uu to 1 m and a value of 1,000 will result in 1 uu equaling 1 mm. For
the purposes of this project, we will leave the default value of 100 so that an Unreal
Unit will equal 1 cm, but for future reference, this is the World settings property you
would want to alter in order to change this conversion ratio.
[5]
[6]
Chapter 1
The wall height is 300(uu)H to 400(uu)H. A value of 400 uu will produce a slightly
taller wall, whereas a value of 300 uu will result in a slightly shorter wall, but any
value between 300 uu and 400 uu will work just fine.
The wall depth (thickness)is 10(uu)D to 20(uu)D. The value of the wall thickness
depends greatly on the material that the wall is made of. For example, a brick wall
would be thicker than a wall made of plaster.
[7]
Staircases
[8]
Chapter 1
The value of the staircase width will depend on the area that the staircase is placed
in, so the dimensional measurement of width will vary. The following image has a
step length of 30(uu)L, a step height of 15(uu)H, a step width of 200(uu)W, and 20
steps in total:
By default, the unit snapping grid follows the notion that 1 uu equals 1 cm, but if
we were to follow the power of 2 unit snapping scale, we have this option. In Editor
Preferences under the Level Editor section, there is an option for Viewports. Under
Viewports, there is a subsection labeled Grid Snapping and an option to enable/
disable the Use Power of Two Snap Size, as shown in the following screenshot:
When it comes to unit snapping, follow the measurement that works best for you.
Unit snapping is a very important aspect when it comes to placing assets in your
game world. It can be a lifesaver when it comes to avoiding clipping or Z fighting
between two objects. Unit snapping is also crucial when it comes to creating
proper distances between objects, such as creating hallways or alleyways between
buildings. In the end, it will save a lot of time and effort to take unit snapping into
consideration at the beginning stages of level development and particularly during
the white box stages of level design. There will also be specific instances when
placing objects in our game world where unit snapping is not necessary, such as
placing debris on the ground, placing paper on a desk, or any other objects that don't
require specific distances between themselves and other in-game objects. When it
comes to these instances, Unreal Engine 4 gives us the ability to toggle unit snapping
on and off by clicking on the grid icon, as shown in the following screenshot. Lastly,
we can also snap our objects to the grid or the floor of our environment by pressing
the End key. Alternatively, we can press Ctrl + End to snap an actor to the grid. If we
ever need to change the key bindings for these actions, we can navigate to the Edit
Menu | Editor Preferences | Keyboard Shortcuts to make any changes.
[ 10 ]
Chapter 1
[ 11 ]
In Maya, we can change the units of measurement by clicking on Window from the
toolbar. Now, select Settings/Preferences from the drop-down window and then
Preferences. In the Preferences dialogue box, select Settings. Under Working Units,
we can change the linear units to centimeter.
[ 12 ]
Chapter 1
[ 13 ]
[ 14 ]
Chapter 1
In Unreal Engine 4, the 3D axes are labeled differently, as displayed in the preceding
image. Instead of the "up" axis being the y axis, in Unreal Engine 4, the "up" axis is
labeled as the z axis. The "forward" axis is then the y axis instead of being the z axis,
as depicted in the preceding image.
It should also be discussed that Unreal Engine 4 uses a left-handed coordinate
system, which means that the positive direction for the X axis is on the right-hand
side, the positive direction for the Z axis is upward, and the positive direction for
the Y axis is forward. In the left-handed coordinate system, the positive rotation of
an axis is always in the clockwise direction. We can see this reflected in the transform
section of the details panel when an object is selected and is either moved or rotated.
[ 15 ]
In Unreal Engine 4, the X axis is labeled as a red-colored arrow; the Y axis is labeled
as the green arrow, and the Z axis as the blue arrow. In the editor, you can toggle
the transformation type of a selected object between translation, rotation, and
scale by either repeatedly pressing on the spacebar or by toggling between the W
(translation), E (rotation), and R (scale) keys. The viewport depicted in the preceding
image is known as the perspective viewport. This is the only 3D viewport in Unreal
Engine 4 and can be accessed using the Alt + G shortcut. When you work on a 3D
game, Unreal Engine 4 offers three 2D viewports: the top, side, and front perspective
viewports to take advantage of when you place objects in your game world.
[ 16 ]
Chapter 1
[ 17 ]
Lastly, let's briefly discuss how rotation works in 3D programs, such as Unreal
Engine 4. In the real world, the three different types of rotation of an object are Yaw,
Pitch, and Roll. These rotations are defined as follows:
Chapter 1
In the real world, the pitch, yaw, and roll rotations of an object can be visualized by
looking at how a plane can rotate, as shown in the following image:
[ 19 ]
In Unreal Engine 4, the use of scalar and vector values is very common, especially
in blueprints and materials. In the context of the material editor, scalar values are
simply numerical values, whereas vectors are actually the colors of RGBA or red,
green, blue, and alpha. In the Material editor, we can use both scalar and vector
parameters to influence the color and intensity of the material itself.
In the preceding screenshot, we are using a vector parameter node in our material to
dictate the color of the material itself. By default, the vector parameter in the material
editor contains the values for red, green, blue, and alpha; the alpha value controls
the opacity of the color. In the material example, the scalar parameter controls the
strength of the emissive value of the material. By increasing or decreasing this value,
the material's brightness will get brighter or dimmer. To recreate this, we can rightclick on our Content Browser, select Material and name this MAT_Example, and
double-click on the material to open the Material editor. Perform the following steps:
1. Right-click on an empty space in the Material editor and search for the
Vector parameter. Set its RGBA values to 1.0, 0.5, 2.0, and 1.0 respectively.
We can name this parameter as Material Color.
2. Next, let's right-click and search for the Scalar parameter. Then, set its
numerical value to 5 and name this parameter as Material Color Intensity.
[ 20 ]
Chapter 1
3. To create a multiply node, we can either right-click and search for this node,
or just hold the M key and left-click on the blank space in the Material editor
to create the multiply node.
4. Now, we can multiply the color output Material Color vector by the
numerical output of the Material Color Intensity scalar and plug the result
into the Emissive Color input of the material itself to create a bright and
intensive purple material.
5. For additional color, we can plug the color output of the Material Color
vector parameter into the Base Color input of the material as well.
In the Blueprints of Unreal Engine 4, the scalar and vector parameters serve similar
purposes (as seen in the material editor). The vector variable in blueprint scripting
holds the values for X, Y, and Z values and is used to dictate the location and
direction, whereas the rotator variables holds the Roll, Pitch, and Yaw rotation
values. When it comes to scalar variables in blueprints, there are many options to use
(such as integers or floats) because scalar values are only numerical values with no
direction associated to them.
[ 21 ]
As shown in the preceding image, we can split the structure pin for the rotator and
vector variables by right-clicking on the vector values and selecting Split Struct
Pin. So, we can edit each direction individually using float scalars to affect each. At
the same time, instead of using individual scalar values by right-clicking on one of
the split float values and selecting the Recombine Struct Pin option, we can also
recombine the structure pin for these variables so that we can edit these values with
vectors or rotators respectively.
Another interesting use of materials and blueprints is that you can dynamically
change the value of the scalar and vector parameters in the event graph or the
construction script of the blueprint.
As shown in the preceding image, we can create a dynamic material instance from a
static mesh in our blueprint that uses the material example we made earlier, which
uses the vector and scalar parameters. Here, we can set the Material Color vector
parameter, split the color structure into four unique float values of RGBA (red, green,
blue, and alpha) and then use the random float in the range node to create random
colors for the material.
To recreate this, we first need to create a new blueprint by right-clicking on our
content browser and selecting the Blueprint class and then Actor to create an actorbased blueprint. Next, double-click on this new blueprint to open the Blueprint
editor. Perform the following steps:
1. Select the Viewport tab at the top of the editor so that we can add our
components for this example blueprint. For the base of this blueprint actor,
we want to add a scene component to the root of the actor so that the other
actors we add can be attached to this component.
[ 22 ]
Chapter 1
2. From the Add Component tab, select the Scene component option and
name it ROOT.
3. Now, we want to add the shape of a plane to our blueprint so that we can see
our material on an object. Under the StarterContent folder in Shapes, select
the Shape_Plane Static mesh so that it is highlighted in the content browser.
4. With the Shape_Plane mesh highlighted, let's go back to our blueprint.
Under the Add Component tab, there will be an option for Static Mesh
(Shape_Plane). Name this component whatever you like and rotate/orient
the mesh in the 3D viewport as necessary.
5. Now, back in our Content Browser, let's select our material so that it is
highlighted. Then, back in the blueprint, we can apply this material to our
plane mesh by selecting the plane in the Components tab and clicking on the
arrow next to the Element 0 option in the Materials section.
6. With our material applied to our Static Mesh, we can now navigate to our
Construction Script to script the behavior that will randomly change the
color of this material each time the blueprint initializes.
7. In the Construction Script tab, let's grab the Get variable of our plane static
mesh by keeping CTRL pressed and clicking and dragging the variable from
the variables section to the left-hand side of the editor. From this variable
pin, we can search for Create Dynamic Material Instance. Make sure that the
material we created is selected for the Source Material variable in that node.
8. From the return value of the Create Dynamic Material Instance node,
we can promote this value to a variable that we can then reference in the
blueprint whenever we like. Name this variable whatever you like.
9. Now, we can drag the pin from the variable output of the newly promoted
material instance variable and search for the Set Vector Parameter node.
Here, we need to provide this node with the name of the vector parameter
that we want to change and color values that we want to enter. If you
remember, we named our vector parameter Material Color.
10. To randomize the color, we need to drag from the value input variable of
the Set Vector Parameter Value node and search for Make Linear Color.
For the RGB values, we can use Random Float in range nodes that have a
minimum value of 0.5, a maximum value of 1.0, and a constant alpha value
of 1 to randomize the color.
[ 23 ]
11. Now, when we repeatedly click on the Compile button at the top, we can see
the color of the material change each time.
[ 24 ]
Chapter 1
[ 25 ]
In Unreal Engine 4, the blueprint assets that utilize the Projectile component, such
as bullets, rockets, or any other kind of projectiles used in our game, can edit the
coefficient of friction and other physics-based properties. From the first person
project that was created earlier in the chapter, we can navigate to the Content
folder in the Content Browser and then to the FirstPersonBP folder and select the
Blueprints folder. In this folder, we can select the FirstPersonProjectile blueprint.
Then, in the Viewport, we can select the Projectile component to view some of the
physics properties, as shown in the following screenshot:
By increasing the Friction property, we can cause this projectile to come to a stop
more quickly, whereas decreasing this property will result in the projectile coming
to a stop over a longer period of time. We can alter this property until we can get
the behavior we want. For more examples of physics-based properties featured in
the Unreal Engine 4 blueprints, feel free to investigate the FirstPersonCharacter
blueprint and select the CharacterMovement component.
Chapter 1
In more simple words, this law focuses on the principle that a change in an object's
velocity can only occur if this object is accelerating in a particular direction, and a
positive or negative acceleration can only take place if an external force is acting on it.
[ 27 ]
In Unreal Engine 4, we can see this law of motion in action by playing in the editor
and clicking on the left mouse button while aiming at the ground to see the ball
bounce in the opposite direction that it was fired at and moving at a speed equal to
the one in which it was fired at, except that we have friction applied to this projectile,
so it loses some of its initial velocity due to the friction.
[ 28 ]
Chapter 1
To bring all of these laws of motion together, what we can do is add a box collision
component to the FirstPersonCharacter blueprint, set its Collision Presets to
BlockAll, and attach it to the FirstPersonCamera component by dragging it onto the
FirstPersonCamera component in the Components tab.
Now if we play in the editor, we can start running into the physics cubes in the
FirstPersonExampleMap (which is default to the First Person project template) and
see forces applied to them, which is equal to the mass of the player multiplied by the
players' current acceleration value.
[ 29 ]
Potential Energy: This denotes the energy that an object has due to its
location in the 3D space
Mechanical Energy: This specifies the sum of kinetic and potential energies
Heat: This denotes the amount of thermal energy being transferred in the
direction of decreasing temperature
These are just a few examples of energy that exist in the real world, so feel free
to perform additional research on the concept of energy because this section will
only cover the surface of the topic. When it comes to the concept of energy and the
conservation of energy, Unreal Engine 4 follows these properties as well through its
built-in physics engine.
As discussed in the previous section of this chapter regarding the Newtonian
principles, forces are any interaction that tends to change the motion of an object.
This can also be referred to as concepts (such as pushing and pulling) and contains a
magnitude and direction, making it a vector quantity. Forces can be caused by gravity,
magnetism, wind, or even the pushing or pulling of an object by a person or machine.
[ 30 ]
Chapter 1
In Unreal Engine 4, we can add forces through blueprint scripting, and we can
perform this in a few ways. The first method is a function called Add Force. This acts
a lot like a thruster and adds a linear burst of energy in the specified direction.
You can add this type of force to any component associated with our blueprint,
or you can apply this force to any component that is hit by a component of our
blueprint. We can also see that there is a bone name property in this function. This
means that we can apply force to a bone if one exists in our blueprint.
The second method of applying forces to our blueprint components or to
components in our game world is through the Add Radial Force function:
This function allows you to specify a location in the 3D space. Here, the source of
the radial force begins and then specifies a radius and strength using Float values.
Lastly, we can apply one of the two methods of Falloff for the radius, which is either
a constant fall-off or a linear fall-off. All bodies in this radius will be affected by this
force, so make sure that you take this into consideration when you apply radius and
strength values.
[ 31 ]
Summary
In this chapter, we discussed a handful of mathematical and physics-based concepts
that are necessary to grasp in order to understand how physics works in Unreal
Engine 4. We looked at the different units of measurement that exist in the American
and European standards of length and how they convert from one to another. We
also looked at Unreal Units (uu). Then, we discussed the common measurements for
walls, doorways, characters, and stairs.
Next, you learned a little bit about scientific notation, how it works, and how it
is used. We looked at some basic and advanced examples of conversions from
numerical to scientific notations.
Additionally, you learned about the 2D and 3D coordinate systems and how they
are used in the real world and in Unreal Engine 4. We also investigated the different
2D and 3D viewports that exist in Unreal Engine 4 and the important functions they
serve when you create game worlds.
Furthermore, you learned about the scalar and vector properties and how they are
applied in the real world and in Unreal Engine 4. We also looked at examples of how
the values of scalars and vectors are used in the Material editor and in blueprints.
We also looked at each of the three Newtonian laws of motion and provided realworld and Unreal Engine 4 examples for each law.
Lastly, you learned about the different forces and energy that exist in the real-world
and provided examples on how to apply forces in blueprints.
Now that we have a base understanding of real-world mathematics and physics
concepts and how they are used in Unreal Engine 4, we can now move on to the
Physics Asset Tool (PhAT) in Unreal Engine 4.
[ 32 ]
www.PacktPub.com
Stay Connected: