What's easier writing a 40 min script or designing a gearbox?

I've previously developed a realistic simulation of this very thing. It can either be done simply if you just want an arcade racer, or with some complexity if you want a realistic simulation.

In a nutshell, here's what you do...

  • Initial Setup and Calculations:

    • Simulation Mode: Create an array that covers the span of the engine's RPM range, in increments of 500 or so RPM, and fill each one with the engine's torque at that RPM (use Newton Meters; 1 ft-lb = 1.355817 N-m).
    • Arcade Mode: You can probably get away with skipping the array and just defining a variable with a single torque value that will be used for all RPM's.
    • Create an array to hold your individual gear ratios, then put the final drive ratio into a variable. You'll multiply the current gear's ratio by the final drive ratio to get the actual effective gear ratio, so a 3.30 1st gear with a 4.20 final drive would be 13.86. This is both the ratio of engine RPM to wheel RPM, and output torque vs engine torque.
    • Determine the drive wheel's total radius (in meters). For example, my Corvette's rear tires are 285/35-19, so 285mm * 0.35 = 99.75mm sidewall height. The wheel's 19 inch diameter is 482.6mm, so take half of that to get the radius, and you get 241.3mm. 241.3mm + 99.75mm = 341.05mm total radius. Divide by 1000 to convert it into meters and you get 0.34105m. (For reference, this measurement equates to an overall wheel + tire diameter of about 26.85 inches)
    • Simulation Mode: You'll want to also determine the wheel's rolling circumference, so just take its total radius, double it to become a diameter, and multiply it by pi. 0.34105m * 2 * pi = 2.14288m
  • Run Loop Stuff:

    • Simulation Mode: Use the vehicle's current speed (in meters per second) to determine the engine RPM in the current gear. Get the wheel RPM by doing Speed / (Tire Circumference / 60), then multiply it by the effective gear ratio to get the corresponding engine RPM. For about 20mph, you'd have 9 m/s / (2.14288 / 60) = 252 wheel RPM * 13.86 first gear = 3493 engine RPM. Now use that RPM to get the corresponding engine torque value from the array, interpolating between the two nearest RPM increments in the array.
    • Arcade Mode: Just get your single engine torque value.
    • Multiply the engine torque by your throttle modifier (note: in real life, this can be more of an curve relative to throttle position), then multiply it by your effective gear ratio. So, 500 N-m * 0.80 throttle * 13.86 first gear = 5544 N-m output (but we're not done yet!)
    • Torque is a measure of force and distance, not force alone, so we need to divide it by the drive wheel's total radius to get just force alone. 5544 N-m / 0.34105m = 16255.681 Newtons (that's 3654 lbs)
    • From that, subtract your aero drag for the current speed (and vehicle's CdA), rolling resistance, etc. Then you get your force to apply to accelerate your mass. If you want to simulate a gear shift, cut the engine output for the duration, while still subtracting the drag and resistance forces, so you'll get a negative value and decelerate the vehicle slightly.
    • Now calculate the acceleration by dividing the resulting force by your vehicle's mass (in Kg). 15000 N / 1500 Kg = 10 m/s2
    • Apply this acceleration to the current speed, accounting for how much time the engine power is being applied for (ie how many seconds each run loop iteration is supposed to simulate). If your current speed is about 9 m/s (~20 mph) and you want each loop iteration to simulate 0.1 seconds, you'd do 9 m/s + (10 m/s2 * 0.1) = 10 m/s (new speed)

Of course there can be more complexity to all of this, and more things to simulate, so hopefully I didn't mangle any formulas too badly in this summary, but you should be able to get the idea.

/r/AskReddit Thread