Physics in Unreal Engine is responsible for the simulation and collision of all physical actors, which means any motion, like falling or applying a force or interaction between them is done using physics. At the time of this book was written, Unreal used PhysX version 3.4, which was used for all simulations.
UE4 includes a built-in high-performance custom physics engine called Chaos Physics Engine, which (at the time of writing this book) is only available through the GitHub source. Chaos Physics is expected to be production-ready in UE4 version 4.26.
Raycasting (also known as trace) is the process of sending an invisible ray from one location to another location in the game world and determining if the ray hit anything. The hit result contains information that you can use to alter the game state.
In this chapter, you learn about Simulation, Collision, and Trace.
Simulation
- 1.
Open the Blueprint and select MeshComponent (Inherited) from the Components tab (see Figure 5-1).
- 2.
In the Details panel, scroll down to the Physics category (see Figure 5-2).
Simulate Physics: Enables or disables physics simulation. If this option is grayed-out, you don’t have a collision set up on the selected mesh. For Skeletal Mesh, you need a physics asset setup, and for Static Mesh, you need a simple collision setup.
MassInKg: The mass of the object in kilograms.
Linear Damping: The drag force applied to a linear movement, which determines the movement in a straight line.
Angular Damping: The drag force applied to an angular movement, which is the rotational equivalent of linear movement.
Enable Gravity: Determines if the physics simulation should have the force of gravity applied.
Collision
In Unreal, every object with Collision is assigned an object type, which defines how it interacts with other object types. Some object types can block, some can overlap, and others can ignore. An object can also define how it should react to traces using trace channels. By default, there are two types of trace channels: Visibility and Camera.
Blocking collisions occurs when two objects collide and cannot pass through (e.g., an object hitting a wall).
Overlap collisions occur when two objects overlap each other (e.g., an object falling into water).
Simulation Generates Hit Events: If true, this physics body calls a native On Component Hit event when a collision is a success.
Phys Material Override: Assign a physics material (not to be confused with the material in the next chapter) to define properties such as Friction, Restitution, and Density.
Generate Overlap Events: If true, this physics body calls a native On Begin Overlap event when the overlap starts and calls an On End Overlap event when overlapping ends.
Can Character Step Up On: Determines if the player character can step onto this object. If No, the player is rejected if they try to step on it.
No Collision: There is no representation for any type of collision. This means no overlapping or blocking.
Query only: The object can only trigger overlap events, and no rigid body collisions; the object cannot block any other objects.
Physics only: The object can interact with other objects but does not support overlaps.
Collision enabled: The object can overlap and block other objects.
Let’s now look at defining a new trace type and using it in the game to pick up an item.
Using Trace (Raycast) to Pick up an Item
So how does the trace know what to hit? It is done using a trace channel. Unreal Engine comes with two trace channels: Visibility and Camera. In this section, you create our own trace channel, set it to an item, and use trace to detect it.
After pressing the Accept button, you see your newly created trace under the Trace Channels category. You can now access this trace channel under every mesh component to enable or disable it.
To use the trace channel, you first open your previously created Blueprint Actor and select the MeshComponent (inherited) from the Components tab. Assign the Collision that includes Static Mesh. In our case, you use the 1M_Cube static mesh.
If you have not added a third-person project, you can do so by clicking the green Add New button in the Content Browser. Select either Add Feature or Content Pack. In the dialog, select Third Person and click +Add to Project.
In the Event graph, right-click and search for Input Trace (it is under Input → Action Events). You can add the Trace event, which is invoked when you press the E key. Right-click again on the graph and search for Line Trace By Channel. (There are other types of traces as well, like Box, Capsule, and Sphere. Read more about them at https://docs.unrealengine.com/en-US/Engine/Physics/Tracing/Overview/index.html).
Line Trace By Channel is the primary node that does the tracing from the start location to the end location. Figure 5-4 illustrates where the trace starts from the Camera and shoots to the direction the Camera is facing. To create that setup, right-click the graph and search for GetFollowCamera (no spaces) and select the Get Follow Camera node. This is the getter node for the Camera included with the character in Components. (Alternatively, you can drag the Follow Camera from the Components tab onto the graph to create the node).
Now you need the trace to end at a specified length where the Camera is looking. To do that, drag a wire from Follow Camera, and search for and select GetWorldRotation, which represents the world rotation of the Camera Component. To find the direction, drag a wire from the GetWorldRotation blue output, and search for and select GetForwardVector.
The GetForwardVector node represents the direction, but it returns a unit vector, which means it is in the 0–1 range and is not useful in our context (https://en.wikipedia.org/wiki/Rotation_matrix). So you multiply it by a higher number to determine how far the trace should go.
Drag a wire from the GetForwardVector node’s yellow output, search for multiply and select the vector * float node. After selecting this node, enter 5000 in the green box of the newly created multiply node. The 5000 is the length of our trace. Connect the multiply node output to the End input of the Line Trace node.
You are not completely done, but you did finish the trace setup. If you play the game now and press E (or the key you used for Trace), it might appear that nothing is happening, but you are tracing. To check this, click the None drop-down button at Draw Debug Type and change it to For Duration. Press the Play button again and start the tracing. You now see a red line originating from your Camera that shoots toward where you are looking.
Even though you finished the trace, you are still not using any information from the hit result. Let’s fix that. Drag a pin from the red Return Value of the Line Trace, search, and select Branch node. The return value is a boolean (either true or false). It is true if the hit is successful (if it hits any object with MyItemTrace, trace response is blocking; in our case, it is the Blueprint actor we created). It is false if the trace didn’t hit anything. Drag another wire from the Out Hit node and select Break Hit Result. The resulting node is called a Struct node, which is a container containing other data types.
Destruction Using Physics
In this section, you use the Apex destruction plugin to create a destructible mesh and interact with it in the game. When writing this book, Unreal Engine was making the transition from Physx to Chaos Physics System, rendering this section obsolete in the future. I don’t cover Chaos because it is not production-ready and requires the GitHub source version instead of the launcher version.
Fracture Mesh (area 1 in Figure 5-18) fractures the mesh into multiple chunks. The number of chunks is based on the Cell Site Count under the Voronoi section (bottom-right corner).
Preview Depth 0 (area 2 in Figure 5-18) is the chunk depth.
Explode Amount (area 3 in Figure 5-18) is a slider that moves the fractured pieces apart.
Destructible Settings (area 4 in Figure 5-18) is the panel where you adjust various destructible settings.
Fracture settings (area 5 in Figure 5-18) is the area where you decide the number of chunks to generate.