In the previous post I talked about modifying the standard Unreal Engine 4 Character component so that it could climb walls. It worked great for the static platforms and columns I had placed. I figured it would cool to climb up a swinging pendulum. Should be easy, right? I just make a pile of blocks, make them physics objects hooked together with hinges, and I should be able to grab and climb them.
Well, not really.
The problem here is that the Unreal Engine 4 (UE4) Character is a kinematic object. You tell the object where it should go and the object just plops down at the location you specify. The object doesn’t participate in any running physics simulation, effectively ignoring forces and any other physical interactions from objects that collide with or pull on it. We’ll call objects that interact with the physics simulation dynamics objects to distinguish them from kinematic objects, which don’t.
Usually the UE4 Character is a kinematic object driven by your input via the keyboard, mouse, or gamepad. The character may switch to dynamics mode when you lose control and physics takes over. When would you want to enable dynamics on a player avatar? One example is when your character gets killed and then tumbles down some stairs. Also, objects like cars or planes are typically not kinematic objects since they need to bounce and turn in a realistic fashion. You could (for example) make a car that is a kinematic object but it wouldn’t have very realistic physics. You need to build your car from physics objects if you want it to skid and bounce realistically.
Okay, so the character is a kinematic object and that’s giving me the desired behavior when climbing, right? So we can just switch it to physics mode when climbing and re-use the rest of the code from the last post. That should work, right? Nope. The old code kind of assumed the climbing wall was stationary and if the climbing wall moves fast (like a swinging pendulum or a collapsing bridge) then the old code will frequently lose track of the wall and then your character just starts falling. I also wanted a more dynamic connection with the wall so that your character would swing back and forth like Wander in Shadow of the Colossus.
Really what I’m going to do here is cheat by turning the player into a dynamics object subject to physics forces and then attach the player to the nearby wall with a joint. What’s a joint? In the physics simulation, a joint is a generic term for a connection between two objects that represent some sort of physical relationship. Some examples of joints would be a hinge, a slider, and a breakable rigid joint. These joints are referred to in Unreal as constraints and you usually build them using a PhysicsConstraintComponent.
When I “turn on physics” for the player charaacter, it turns into just another physics object like a box or a tire. At that point the player character will just flop around like any other object. I can also connect a joint to the character! So while climbing I create a joint that connects the player to the nearby wall which is (possibly) another physics object. This joint is called the “grip” since it represents the point where the characters hands physically latch onto the wall.
If the grip joint is placed at a good spot, the player character will flop and dangle around in a convincing manner, especially if the wall is also moving. When the character is done climbing, I destroy the joint and switch the character back into kinematic mode.
What about moving? We can manipulate the reference frames of the joint. The joint tracks it’s position relative to the two object it connects and applies forces to the two objects using information from the reference frames. By modifying the references frame between the joint and the wall object I can actually move the joint along the wall. The character will follow along, possibly swinging or bumping as the joint moves arounds corners or over ledges. Manipulating the reference frames requires a lot of work with transforms and lower-level code though, so I won’t go over it in more detail here.
Finally, if the player is climbing on a flat horizontal surface I switch back to kinematic mode and the character starts walking. Then the climbing mode is done.