As this is my first post in 2012, I just want to start by wishing you all a happy new year!
For the past few weeks I have been tackling a whole bunch of really weird and interesting problems in UDK, and one of the most interesting has been how Epic handles gravity. Anyone who’s tried to change how gravity works in the Unreal Engine will know that this is near-impossible without access to native code. The reason is that physics, for the most part, isn’t handled by UnrealScript; it’s hard-coded and untouchable. Gravity will ALWAYS point straight down with a force of about 400.0 (whatever that happens to translate to in the real world).
Nevertheless, I did a lot of research, watched a lot of YouTube videos, and waded through tons of old forum posts for a few days trying to figure out a way to make it work. Many of the solutions I saw involved either changing the Pawn’s physics to PHYS_Flying or PHYS_Spider or making custom ladder volumes. In each case the aim was not to control gravity but to cheat it — with gravity turned off and the Pawn free-floating, one could simulate gravity’s pull with a quick nudge in one direction.
The problem? These solutions involve a whole bunch of manual positioning, rotation, and collision detection. To give you an idea of the complexity, consider a game in which it’s possible to walk sideways on a wall. You’d have to make sure the Pawn was free-floating the moment it touched the wall. You’d have to rotate the skeletal mesh to face the wall and position it normal to whatever surface it was standing on. If it hit something, you would have to handle how the Pawn responded to the hit (with the new “gravity” direction in mind). Most of the example sources I found online didn’t work at all.
Although you can’t tell from the picture, in this game you can change the direction of gravity. Hit ‘W’ and the whole map flips 90 degrees clockwise and the ball is sent falling in another direction. Hit ‘S’ and the map flips 90 degrees counterclockwise. It’s not perfect yet, but collision is a breeze and moreover the system is simple.
So…how? The answer is that the ball is not a Pawn at all, but a KActor. Pawns are cool because they streamline a lot of the things that you’d expect a sentient, controllable actor to do (such as move, handle weapons, etc.). But one thing a Pawn is lacking in is physics. By completely getting rid of Pawn and dumping in a Karma Actor, I was able to control gravity easily. Want the ball to float up? Easy, just AddForce(vect(0,0,400.0));
I’ll have a video of this game in action up on my YouTube channel soon enough. But for now, go mess around with KActors! It’s amazing just how much they can do 🙂