Blender vehicle physics tutorial

A long time ago I toyed with the idea of making an entire game by myself. Being a driving game, I spent a long time with the basics stuff like vehicle physics to make sure the driving part is stable, bug-free, and most importantly, fun.

I never finished it. But ever since I posted the above video, I have been getting a lot of inquiries as to how I did the vehicle physics system in Blender. So here is a quick guide followed by the sample file.

The key to stable driving dynamics is to use the built-in PhysicsConstraints module within Blender’s game engine. PhysicsConstraints is a python wrapper for the Bullet physics engine. No any homemade setup I’ve seen is as good as what’s already provided by Bullet Physics. So we’ll use that.  Because there isn’t a graphical interface for setting up the vehicle physics yet in Blender, we need to write a few lines of Python script.  The key component is the car body object, which is linked to the 4 wheels through a logic brick connection so that the script would recognize the linked objects as wheels. (As you can see, all the actual driving logic is all happening on the car body, the wheels have no real logic brick attached).

The entire setup is a bit complex, since there is a mix of logic brick and python. Take a look at the sample file if you are lost.  When one runs the game, the following happens:

0. World is initialized, but we don’t really care about that here.

1. The car body is initalized, scripts.carInit() is ran. In this script, it initializes the car as a “vehicle constraint”, aka constraint type11, and stores it as a Python object called vehicle.  The same script then looks for the 4 wheel by access a gamelogic actuator with specific names (in this case, wheel1, wheel3, etc), then the script attaches the wheels to the car body using the settings specified in the script. Variables such as RollInfluence, SuspensionStiffness, and TyreFriction can all be set on a per-tire basis once the vehicle object is created. The job of carInit() is now done.  Our car body is now considered to be a vehicle by the Blender game engine, and it will behave like one.

2. Every frame, scripts.carHandler() is ran. This script does the actual moving of the car, it applies engine force and steering to the vehicle object. But this script gets the user input (keyboard sensor inputs) from another source. (See #3 below)   Vehicle objects have methods such as applyBreaking(), applyEngineForce(), getWheelRotation(), getNumWheels, which you can all call.

3. Every time a key is pressed, script.keyHandler() is ran. It figures out which key is pressed and set the intermediate variable so that #2 (i.e. scripts.carHandler) would know how much throttle to apply, where to steer, etc.  This script is separated from scripts.carHandler() not because of technical limitations, but by design so that it’s easier to manage the code.

That’s all there is to it.  If the script layout is a bit confusing, it’s because it is.  I originally intended it to be a bigger project, thus everything is separated into nice neat functions.  Again, you can DOWNLOAD the whole setup for Blender 2.5 from here.

Controls:
Arrow Keys to move the car
Space bar for handbrake
R to reset car if it flips over
number row 1,2,3,4,5 to change camera

Hope that helped.

14 thoughts on “Blender vehicle physics tutorial

  1. Hi,

    I am Sandeep from Marketing department of Packt Publishing.

    I would like to inform you that we have recently published a book titled “Blender 2.49 Scripting” by Michel Anders

    I was wondering if you would be interested in reviewing this book? On your confiramtion I would be happy to provide you with a review e copy.

    Regards,
    Sandeep

  2. The physics looks very nice from the video. How do you force the car to stay on top of the ground?

  3. This is pretty awesome. There are some issues with the ground collision (you go through it if you brake too hard or hit it from high up). Still pretty good.

    Best thing for me is that you made sure to make it look good as well as run well. The shaders give it a little extra “oomph”. Even if technically there is no difference, it FEELS like a better car game because it LOOKS like a better one.

  4. Just a question. As you mention, the key component is the fact that the car body can directly access the wheel actuators (dummy visibility actuators here).

    I’m a noob in blender. Could you tell me in few words how you can add to the body car an actuator whose owner is actually a wheel ?

    Thanks for any help, web pointer….

    Thomas

  5. Thomas, you need to select both the car and the wheel object, and then the make a link from the controller of the body to the actuator of the wheel.

  6. Nice game :)

    It’s exactly the type of game I’m making, but I can’t find good tutorials…
    Could you make a tutorial if you have the time plz? Or could you pass me some info… anything that helped you…
    Thanks

    Sticky

    PS sorry for my bad english

  7. This really is a nice race game.
    The problems concerning falling through the ground were solved for me by making the car not a ‘Ghost’ in the Logic panel.

  8. to fix the car from going through the ground turn off ghost for the “bare” object
    great game way beyond my level

  9. nice work but i have a problem. when i run the game it lags at a terrible 5-6 frames per second. even though its running on my laptop its not a joke. its an i5 2410m with turboboost to 2.9 ghz and the graphics card is a nice nvidia gt550m. why is it lagging? is there a way to fix it?

  10. WEw!!!!you Are Amazing.wow!..well if you have example kindly please give me in my email.RrafeyelL@yahoo.com.??You remember Your Game?? (Blender vehicle physics tutorial) posted april 17 2010…i want other example like that because i want to be a professional game programer but now im only a nooob… please kingly help me Thanks..hope you help me take care…well im using blender 2.61 now.and im confuse waaaaaaaa!………hope you will understand me thnksssss… GODBLESS

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>