Monday, 21 February 2011

UDK Motorsport: Gears of Car

In the previous post I talked about torque curves, and Unreal's way of approximating them. My objective now is to create a manual gear box. As usual I'll first take a look at how things work in real life, then I'll see how to circumvent Unreal's annoying methods.


Remember when I said the the engine, the drive shaft and the wheels spin at the same speed? Well that would be true if gear boxes didn't exist. Why do they exist, by the way?

Have you ever managed to start moving a car while on 5th gear (where the wheels spin about as fast as the shaft)? Probably not. That's because the torque provided by the engine is not sufficient. It would take tremendously powerful engines to do such a thing (at a decent speed). Gear boxes exist to solve that problem. It works based on a mechanic principle called mechanical advantage. Look at the picture below (nicked from Wikipedia):



Because of the difference in the number of teeth, the cog on the right has a slower angular speed than the one on the left. And because the laws of the universe like when things are balanced, there has to be some sort of compensation for this loss of speed. In this case, we gain torque. And that's what gear boxes are for, reducing the speed of the wheels in order to have the torque required to move the car. Obviously, as the wheel speed is being limited, so is the overall car's max speed. That's why there are several gears, to balance the need for torque and the need for speed ­(tm).

Setting a gear is merely a matter of saying "I want the shaft to spin 3 times faster than the wheels" which is equivalent to "I want to triple the torque provided by the engine".

There's a neat way of visualising gear ratios:


This basically shows the min and max speed for each gear. Note that on the y axis is the engine's RPM.

Now that I know how this works (quite simple in the end), how to make it happen in Unreal?

Well that's simple, but not trivial. As seen before, Unreal's torque curve is based on the vehicle's speed rather than the engine's RPM. But since for a given speed, the engine provides different torques based on the current gear, this means I need to make one torque curve per gear. And of course, for the sake of consistency, the curve must match the engine torque curve's shape every time. A real pain in the bottom, as well as a potential source of errors.

However, once I've got my curves, all that is left to do is replacing on the fly TorqueVSpeedCurve when I press the gear shifting buttons.

It works quite well (though I couldn't be bothered to find nice curves):


To simulate engine brake when shifting down, I drop the torque curve into negative values above a set speed.

On a side note, it has been said on UDN but it bears repeating: when testing vehicles, the editactor console command is your best friend ever.

So, it is completely possible to make a manual gear box, it's even easy. However, the way Unreal handles the torque curve makes the whole process a bit painful. I'm pretty sure this could be simplified by managing myself the torque applied to the wheels, but that would mean doing myself things Unreal can already do.