#### Overview of this book

This book gives readers practical insight into the mathematical and physics principles necessary to properly implement physics within Unreal Engine 4. Discover how to manipulate physics within Unreal Engine 4 by learning basic real-world mathematical and physics concepts that assist in the implementation of physics-based objects in your game world. Then, you'll be introduced to PhAT (Physics Asset Tool) within Unreal Engine 4 to learn more about developing game 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 to create real-world physics in Unreal Engine 4. Lastly, you will create a working Vehicle Blueprint that uses all the concepts covered in this book, as well as covering advanced physics-based topics.
Table of Contents (15 chapters)
Unreal Engine Physics Essentials
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Physics Asset Tool
Constraints
Physics Damping, Friction, and Physics Bodies
Materials
Advanced Topics
Index

## Scalars and vectors

These are definitions to describe the motion of objects. Both are unique. In mathematics, scalars are defined as quantities that are described as a single numerical value, whereas vectors are defined as quantities that are described as a numerical value and direction. The examples of scalar quantities include length, area, volume, speed, mass, temperature, and power, whereas the examples of vector quantities include direction, velocity, force, acceleration, and displacement.

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 right-click 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`.

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.

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 actor-based 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.

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.

11. Now, when we repeatedly click on the Compile button at the top, we can see the color of the material change each time.