Programming a driving simulator, how hard can it be?

  • Thread starter eran0004
  • 50 comments
  • 21,261 views
Some clutches also have damper springs to soften the response, so I just wanted to be clear about what kind of spring I was talking about :)

I have got so much info going in at the moment mate i am forgetting things i used to know. And getting things confused all the time.

I think 5 hours sleep a night since Christmas haven't helped. Well remember a few post's i was on about syncing the Hertz? well i used the wrong term again i used old arcade pcb timing as in hertz to monitor. I think you need something like deltatime in unity, it smooths everything out there's example script's and video in the link that explain's better than i could.

https://unity3d.com/learn/tutorials/modules/beginner/scripting/delta-time

Maybe you could use it in your blender simulation
 
I have got so much info going in at the moment mate i am forgetting things i used to know. And getting things confused all the time.

I think 5 hours sleep a night since Christmas haven't helped. Well remember a few post's i was on about syncing the Hertz? well i used the wrong term again i used old arcade pcb timing as in hertz to monitor. I think you need something like deltatime in unity, it smooths everything out there's example script's and video in the link that explain's better than i could.

https://unity3d.com/learn/tutorials/modules/beginner/scripting/delta-time

Maybe you could use it in your blender simulation

I've found this as well, from the author of the car physics tutorial I'm using. The actual web site doesn't exist anymore, but fortunately it's still available through the internet archive.

http://web.archive.org/web/20050204163259/http://home.planet.nl/~monstrous/tutstab.html

Hopefully it will solve the issues.
 
I've found this as well, from the author of the car physics tutorial I'm using. The actual web site doesn't exist anymore, but fortunately it's still available through the internet archive.

http://web.archive.org/web/20050204163259/http://home.planet.nl/~monstrous/tutstab.html

Hopefully it will solve the issues.


That's full on, I glaze over at sites like that takes me a few hours to Google all the terms ☺. Driving sims are so demanding I take a step forward and 3 back it seem's.

Have you got your head around your bogging down yet or is it still a mystery ?
 
That's full on, I glaze over at sites like that takes me a few hours to Google all the terms ☺. Driving sims are so demanding I take a step forward and 3 back it seem's.

Have you got your head around your bogging down yet or is it still a mystery ?

Well, I think I know what I need to do. But I don't know how to do it. I need to learn more about python scripting before I can go any further.
 
Hi, just read a bit through, this is truly a great effort! :bowdown:

A quick tip with the bogging issue: You should try to get the physics simulation rate decoupled from the graphics frame rate. As you are integrating from acceleration to velocity and again from velocity to position, the integration is sensitive to the time step lengths, and can blow up if the the time steps get too long. A rough outline to do this would be:

Code:
# constants
physics_time_step = 0.005 # sec, i.e. 200Hz
# you can tweak this for more accuracy (lower) or less CPU burden (higher)

# this has to be done every frame
dt = get_time_beween_last_frame_and_now()

psteps = floor(dt/physics_time_step) # find out how many physics steps we have to do
for i in range(psteps):
    a = Force()/Mass() # calculate accelerations
    alpha = Torque()/Inertia() # calculate angular accelerations

    # do physics integration with small time steps
    v += a * physics_time_step
    p += v * physics_time_step
    omega += alpha * physics_time_step
    phi_rad += omega * physics_time_step
# now you have to do the last time step
last_step = dt - psteps * physics_time_step
a = Force()/Mass() # calculate accelerations
alpha = Torque()/Inertia() # calculate angular accelerations

v += a * last_step
p += v * last_step
omega += alpha * last_step
phi_rad += omage * last_step

# now apply the position changes and rotations
body.applyRotation(phi_rad)
body.applyTranslation(p)

If the code in the loop doesn't take longer than the actual frame time, the whole thing runs in real time with a lag of one frame. If the graphics rendering takes a little longer for some frames, it doesn't bother the physics calculation.

Hope this helps! 👍 Will try this out too if i can find the time! :)
 
Last edited:
Hi again! Almost three years later, I decided to pick this up where I left it. I've learned a thing or two about programming and looking back on the code, it's pretty messy. I started to clean it up and figure out what each line of code is supposed to do, what it actually does, and if it makes sense mechanically.

It runs pretty stable now. The only issue is that the wheel tends to add negative torque to the drivetrain when it comes to a stop while connected to the engine, causing everything to start spinning in reverse. Other than that, it seems solid.

Here's a video of it in action. There is no user interface, so you'll just have to figure out when the gear changes come. But basically, it starts in neutral, then shifts up from 1st through 5th, then skips a gear down to 3rd and skips another one down to 1st, then neutral and finally the ignition is switched off.

 
That *Broing* sound was not expected. :lol:

I don’t know what you’re talking about, this is cutting edge sound tech! A single VW Polo sample: volume controlled by throttle and pitch by engine speed. Don’t understand why PD, SMS or T10 didn’t think about that :lol:
 
@eran0004 I have a PDF copy of Milliken and Milliken's Race Car Design which includes nearly all the vehicle dynamics equations you may need for such an undertaking, feel free send me a PM if you'd like a copy.
 
It's been a while, but here we go:



Seems pretty free from glitches, and the handling is great considering I'm driving this with a keyboard, so the inputs are either on or off. Clutch and transmission are both manual.

The only issue I need to fix is that braking is reduced when the clutch is engaged. I suspect that BGE's default vehicle constraints can't handle engine force and braking force at the same time (hard to say though since the documentation is really poor), so I might need to add an exception that says that if brakes are engaged, do not apply engine force (or perhaps, if engine force is negative, apply it as a braking force).

What I'm most proud of is the clutch script, because it's smart enough to handle all sorts of different situations, like when the car is stationary, when you're shifting at high speeds and went you want to start while facing uphill. Instead of directly modifying the clutch, the script sets a clutch target and then adds or subtracts from the clutch frame by frame until the target is met.

Code:
    #clutch release 
    if logic.car['clutchPressed'] == False and logic.car['clutch'] > 0:
        x = logic.car['rpm']
        y = logic.car['clutch_rpm']

        if x > 2500:
            if x > y+1000:
                if x > 4000:
                    clutchtarget = 0
                else:
                    k = -1/3000
                    m = 4/3
                    clutchtarget = k*x+m
                cRelease = clutchDelta*0.5
            else:
                clutchtarget = 0
                cRelease = clutchDelta
        elif x > 800:
            # y = a(x-h)**2+k
            a = -1/1700**2
            k = 1
            clutchtarget = a*(x-800)**2+1
          
            k = (clutchDelta-clutchDelta*0.1)/1700
            m = clutchDelta-k*2500
            cRelease = k*x+m
        else:
            clutchtarget = 1
            cRelease = clutchDelta
          
        if logic.car['clutch'] > clutchtarget:
            logic.car['clutch'] -= cRelease
        if logic.car['clutch'] < clutchtarget:
            logic.car['clutch'] = clutchtarget
          
    if logic.car['clutch'] > 1:
        logic.car['clutch'] = 1
    elif logic.car['clutch'] < 0:
        logic.car['clutch'] = 0

driving_controls.png
 
Last edited:
After struggling to come up with a fairly decent driving model through Godot Game Engine, I have been looking for a few other ways to develop a proper racing/driving game model. So I must say I have been trying to get into Visual Studio and Blender Game Engine. So your work is inspiring me, @eran0004 . I been closely following some of your videos just to try to come up with some kind of prototyping of a racing/driving environment so I can further enhance my willingness to model locations and vehicles.

I am thinking mere basics rather than re-invent Gran Turismo or some more proper sim racer. Something a bit funny I kind of realize is that I don't need meshes of tires and wheels to simulate tires going to work driving. Though, of course, it's a good bit awkward to have a vehicle that somehow sticks on the road without any tires (unless it's some anti-gravity ship of course). There is still a lot for me to learn. One of the reasons why I liked Godot is because I had better progress developing material than I have with Unity, Unreal Engine 4, Löve2D, or doing programming from scratch. If need be, I'll try to learn Python just to make a decent 3D racing game.

As for this topic in general, I think programming a driving simulator is only as difficult as you make it and as complex as you want to make it. Attention to detail and things like that are always considerations and factors in this department. There's also the kind(s) of racing/driving you try to implement. Lots of factors to consider in regards to making the most complete driving sim you can.
 
I am thinking mere basics rather than re-invent Gran Turismo or some more proper sim racer. Something a bit funny I kind of realize is that I don't need meshes of tires and wheels to simulate tires going to work driving. Though, of course, it's a good bit awkward to have a vehicle that somehow sticks on the road without any tires (unless it's some anti-gravity ship of course). There is still a lot for me to learn. One of the reasons why I liked Godot is because I had better progress developing material than I have with Unity, Unreal Engine 4, Löve2D, or doing programming from scratch. If need be, I'll try to learn Python just to make a decent 3D racing game.

Technically you can run a simulation as text only, you don’t need any visual representation. The physics definition of the car is enough.

For an internal combustion engine you need to define:
  • Power curve
  • RPM operating range
  • What will happen when you leave the RPM range
  • Some kind of idling function, which can either be a fixed minimum throttle setting (which is used on simple engines) or an auto throttle (which is used on modern cars).
  • Resistances in the engine. Primarily friction and pressure (friction is constant and pressure depends on engine speed and throttle position).
  • Inertia of engine and flywheel.
  • Optional: starter engine.
Then you have the clutch, which is quite delicate (as everyone who drives a manual may know). For a simple clutch you just need to apply a factor to all the torques that are being transmitted through it and make that factor depend on the state of the clutch pedal.

For a more realistic clutch you should calculate the friction between the clutch plates and have the torques depend on that.

And if you don’t have an actual clutch pedal you need to simulate the operation of the clutch: how fast should it engage? How should engagement depend on engine speed and wheel speed? Think about what you’re doing when you drive a manual car and try to write down some rules that describes how you operate a clutch pedal.

Gearbox is fairly simple, just use the gear ratios to multiply torque and divide speed. Use negative ratio for reverse gear. Optional: define friction and inertia of gearbox.

The final drive is basically a gearbox with just a single gear ratio. For a simple drivetrain, just divide the torque equally between the powered wheels. For a more realistic approach, model a differential gear that distributes torque and speed based on the load on each wheel.

The wheel is technically a lever, meaning that its radius have the same impact on force and speed as a gear ratio. The radius of the wheel multiplies speed and divides force (torque = force * radius => Force = torque / radius).

Finally you need to calculate the friction / mechanical grip between the tyre and the road. In the vehicle handler I’m using in Blender it’s done automatically, which is both good and bad. Good because I don’t need to worry about it and bad because I can’t change it if I want to. Same with suspension.

A good idea is to use SI units throughout the physics simulation, it simplifies everything a lot and you don’t need to use any magical constants to convert between arbitrary units. The SI units are:


Mass: kg
Distance: m
Time: s
Speed: m/s
Acceleration: m/s^2 (Speed/time)
Force: N (mass*acceleration)
Angle: radians (360° = 2*pi*radians)
Angular velocity: radians/s
Torque: Nm (Force * radius)
Work: J (Force * distance)
Power: W (Work / time, or Force * speed, or Torque * speed)
 
I do find this interesting.

I'd love to attempt even a numerical simulation, but I'd like to start much further upstream, like with atmospheric air, and fuel, and end up with traction at a contact patch... simulating the input variables at each stage, and not just something that simulates the output alone.

Sadly neither my grasp on the wide array of Physics necessary, and the scripting skills necessary are up to scratch.
 
I do find this interesting.

I'd love to attempt even a numerical simulation, but I'd like to start much further upstream, like with atmospheric air, and fuel, and end up with traction at a contact patch... simulating the input variables at each stage, and not just something that simulates the output alone.

Sadly neither my grasp on the wide array of Physics necessary, and the scripting skills necessary are up to scratch.

I had to study a lot of Wikipedia articles and YouTube videos on physics just to understand the difference between torque and power :P But in the end it was worth it, and it’s actually really simple once you understand how it’s all connected.

Theoretically you can simulate everything down to molecular level, but there are two problems:

1. The more variables you introduce, the greater the risk of bugs and the harder it is to troubleshoot problems with the simulation. You can end up with a tiny error high up in the chain (say that you forgot to define what happens when track temperature goes below freezing) that causes a massive explosion at the end (perhaps the drivetrain explodes due to sudden and massive blip in frictional forces).

2. The computer can only perform so many calculations per second. At some point you get a more accurate simulation by simply letting the physics run at a higher framerate than by introducing more variables.
 
Hi. Great work on your simulator. (This is also my first ever post! :D - Sorry if it's considered necroposting... :boggled:)

Some time ago I attempted my own simulation and I'm finally coming back and rewriting most of my code. So far, all is well, although I do need some clarification.

I'm mainly stumped on the topic of a transmission and getting my engine on some wheels, especially with your torque based approach. For my simulation I have chosen to use the friction based clutch system you proposed on the first page of the thread. I can't seem to wrap my head around how to calculate torque applied to my wheel in every frame - and would my initial clutch plate torque collected from my transmission (before calculating frictional forces) have anything to do with it?

My only solution has been treating the torque applied to the clutch as the torque applied to the wheel as well, just dividing it.
Running back over my code now with a slightly better understanding, this solution makes absolutely no sense, and I am thrown back to where I was.

I'm also very confused on if using a torque based model is a good idea in the first place, as the transmission to my understanding is almost entirely rigid.

I hope that all is well.
Even though it's most likely been a while since you've touched this project, I also hope you're able to provide me with some much needed help. Thank you!:cheers:
 
I'm mainly stumped on the topic of a transmission and getting my engine on some wheels, especially with your torque based approach. For my simulation I have chosen to use the friction based clutch system you proposed on the first page of the thread. I can't seem to wrap my head around how to calculate torque applied to my wheel in every frame - and would my initial clutch plate torque collected from my transmission (before calculating frictional forces) have anything to do with it?
Assuming that the clutch is not slipping, the torque applied to the wheel is the same as the torque output of the differential. If you don’t have a differential then the wheel torque is the same as the output torque of the gearbox, divided by the number of powered wheels (and minus any frictional losses along the way). The output torque of the gearbox is the torque at the flywheel multiplied by the total gear ratio (gear ratio*final drive ratio).

If the clutch is slipping, then the torque transmitted between the flywheel and the gearbox is determined by the friction between the clutch plates.
I'm also very confused on if using a torque based model is a good idea in the first place, as the transmission to my understanding is almost entirely rigid
It depends on what you want to do, I guess. A very simple vehicle model could just apply a force to the car that’s proportional to the power of the engine and inverse to the speed of the car:

If speed < x: Force = power/x
Else: Force = power/speed

x is some cutoff speed below which you want a constant force, otherwise you’ll get infinite acceleration when the speed is infinitely close to 0 and a division by zero error when the speed is zero.



I hope that all is well.
Even though it's most likely been a while since you've touched this project, I also hope you're able to provide me with some much needed help. Thank you!:cheers:
Thanks! All is well :) The project is a few years old, not sure if I would use the exact same solutions now if I did it again, but I’ll be happy to help if you have any more questions!
 
Back