Logo

Ljung

.dev

A project using this tutorial series is tracked on GitHub. The revision after this part is 88c48a4.

Hej!

In this part of the the tutorial series we will look at implementing gravity, and at the end we will have a our character move down slopes and fall down from edges!

Gravity is actually quite simple. It is simply a constant force being applied "downwards". For Unreal this is negative Z, but it can be anything you want (for example making a game with planets and their own gravity!).

Disclaimer: I'm not that good at physics so things might not be physically accurate...but they don't have to be! The nice thing about games is that it only have to seem realistic (or not). With that said, we'll try to make use of forces, velocity, acceleration etc. so that it hopefully makes sense.

Also, for this tutorial we won't bother with drag (air resistance) or anything else besides the basics.

First, since we're introducing an entirely new force unrelated to our input, we should rename our horizontal movement input for clarity and move the velocity update to the move section:

cpp
1/** SimpleMovementComponent::TickComponent */
2
3...
4
5// Calculate input force
6const FVector& Input = ConsumeInputVector().GetClampedToMaxSize2D(1.0f);
7const FVector HorizontalMovement = Input * MoveSpeed;
8
9// Move
10const FRotator& Rotation = UpdatedComponent->GetComponentRotation();
11Velocity += HorizontalMovement;
12
13...

The effect is the same but now we have a bit cleaner structure to work with.

Applying Gravity

From the movement component, gravity can be fetched by calling GetGravityZ(). This is a nice helper that grabs the current gravity. This can be overridden per world but by default it uses the global gravity value set in Project Settings -> Engine -> Physics, which by default is -980.0. Notice that it is already nicely expressed as a negative value, so we can multiply it with FVector::UpVector directly to get a downwards force.

Note: -980.0 is the same as Earths average gravitational acceleration expressed in Unreal units, equal to 9.8m/s^2.

Let's add a section just before the move step where we calculate our vertical movement:

cpp
1// Apply gravity
2const float GravityForce = GetGravityZ();
3const FVector VerticalMovement = FVector::UpVector * GravityForce;

And add it to our velocity in our move step:

cpp
1Velocity += HorizontalMovement + VerticalMovement;

We also need to reset our Z velocity whenever we hit ground, otherwise it will constantly accumulate and cause lots of trouble, so set it to 0 within our hit check:

cpp
1if (Hit.IsValidBlockingHit())
2{
3  Velocity.Z = 0;
4  
5  ...
6}

Now let's try it:

Ok, this kind of works...but we're falling way too fast. The reason? Well we've actually been cheating/simplifying how (horizontal) movement is implemented.

Both horizontal and vertical movement are represented by variables that correspond to cm/s we want to move. The difference is that gravity actually accumulates! In real life things don't immediately start falling at max speed. They accelerate.

Realizing this and analyzing the code we can actually see that we're adding the speed of 9.8m/s every tick. Instead we want to accumulate so that we roughly equal a speed of 9.8m/s after 1 second (that is, 9.8m/s/s). The reason it's different with horizontal movement is that we're resetting XY velocity each frame (in thie

Luckily once we figure this out the fix is very simple; we just need to multiple our gravity force by DeltaTime one extra time:

cpp
1const float GravityForce = GetGravityZ() * DeltaTime;
2const FVector VerticalMovement = FVector::UpVector * GravityForce * DeltaTime;

We should now fall at a much more acceptable rate:

If you find this too slow or too fast you can always add an extra multiplier to GravityForce and tweak it to your liking.

Terminal Velocity

An optional but recommended step is to also factor in terminal velocity. This means that we won't accumulate speed indefinitely and instead clamp our falling speed. To do that we simply grab our current physics volume and use its TerminalVelocity. In case we're not in any specific physics volume it falls back to the global value set in Project Settings -> Engine -> Physics which by default is 4000.0. We can then clamp our velocity Z (after applying our movement):

cpp
1const float TerminalVelocity = GetPhysicsVolume()->TerminalVelocity * DeltaTime;
2
3...
4
5Velocity += HorizontalMovement + VerticalMovement;
6Velocity.Z = FMath::Max(Velocity.Z, -TerminalVelocity);

We also need to include the physics volume header at the top:

cpp
1#include "SimpleMovementComponent.h"
2
3#include "GameFramework/PhysicsVolume.h"

Results

(I've added an on screen debug message to help visualize)

And, as an added bonus, we can now also walk down slopes (albeit a bit choppy - we will fix that later on!):

Our final tick function now looks like this:

cpp
1void USimpleMovementComponent::TickComponent(float DeltaTime, ELevelTick TickType,
2                                             FActorComponentTickFunction* ThisTickFunction)
3{
4  Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
5
6  if (ShouldSkipUpdate(DeltaTime))
7  {
8    return;
9  }
10
11  if (!PawnOwner || !UpdatedComponent)
12  {
13    return;
14  }
15
16  // Reset velocity
17  Velocity.X = 0;
18  Velocity.Y = 0;
19
20  // Calculate force
21  const FVector& Input = ConsumeInputVector().GetClampedToMaxSize2D(1.0f);
22  const FVector HorizontalMovement = Input * MoveSpeed;
23
24  // Apply gravity
25  const float GravityForce = GetGravityZ();
26  const FVector VerticalMovement = FVector::UpVector * GravityForce * DeltaTime;
27
28  // Move
29  const FRotator& Rotation = UpdatedComponent->GetComponentRotation();
30  const float TerminalVelocity = GetPhysicsVolume()->TerminalVelocity;
31  Velocity += HorizontalMovement + VerticalMovement;
32  Velocity.Z = FMath::Max(Velocity.Z, -TerminalVelocity);
33  const FVector MovementDelta = Velocity * DeltaTime;
34
35  FHitResult Hit;
36  SafeMoveUpdatedComponent(MovementDelta, Rotation, true, Hit);
37
38  if (Hit.IsValidBlockingHit())
39  {
40    Velocity.Z = 0;
41    HandleImpact(Hit, DeltaTime, MovementDelta);
42    SlideAlongSurface(MovementDelta, 1.0f - Hit.Time, Hit.Normal, Hit, true);
43  }
44
45  UpdateComponentVelocity();
46}
47

And that's it! Next up we will look at a more active application of gravity: jumping! 🪂