A Quick Video:
How to do this is explained on this page.
I have been working on getting vehicles to behave in Blender for some time now and have finally arrived at a system that seems to be foolproof. Some principles:
- The best way to control the position of a vehicle during an animation, taking into account turns, hills and bumps is to use a Curve as a path.
- The best method to accurately control the position of the vehicle on the curve is to use a Curve Modifier, rather than a Follow Path Constraint. (discuss amongst yourselves, but my mind is made up).
- The best way to rig a vehicle is to use an Armature rather than umpteen object constraints (partake of further discussion, if required, which it is not).
But, we have a problem, Armatures cannot have Curve Modifiers, only Mesh objects can. But, Armatures can be parented to Object so they follow their every move and orientation.
So we will start off with a curve and a mesh object. In order to steer the steerable wheels, we also need a target object to move the steering as the vehicle progresses along its path. So we need a curve and two objects:
Here we have two plane objects and a curve,
Note! The curve has a switch-back in it, this means any other method of moving the vehicle and having the wheels rotate properly will not work, hence why it is there. Also the two plane objects are set not to render.
Next we need to know how far apart the vehicle axles are – or if it is a three-axle vehicle, the distance from the centre of the two rear axles to the centre of the front axle. This is used to keep the planes a set distance apart, this precludes using a Follow Path Constraint by the way.
The two planes are named Fix and Target, fix is for the vehicle and is on the rear axle centre, target is for the steering and is on the front axle centre.
In order to keep them apart by the required distance, we add a Driver to the Target object’s X Transform (since its Curve Modifier works along the X axis), here is the driver:
To add a driver:
- RMB Click the Transform Box, select Add Drivers => “Single Manually Add Later”.
- Go to Graph Editor and Set Drivers mode – key N to see the right menu.
- Var in this case is the X Location of Fix Object in World Space.
- Scripted Expression is var + 5 – this keeps the object 5 units in front of Fix.
- You must check “Autorun Python Scripts” in User Prefs => File Tab for scripted expressions to work by the way.
Now when you move Fix, Target will move 5 units in front of it. Some more principles we must take note off:
- Vehicles,when turning do so about a point outside and in line of the rear axle, or centre of two rear axles.
- The steering axle describes a larger radius than the rear axle.
- The two steering wheels both point towards the centre of rotation, so they turn different amounts (Ackermann Steering Principle).
The comments above do not apply if you are “drifting” by the way.
The next stage is to make an armature with a “root” bone, this will be the major bone to which all others are parented. We also need two bones for the rear wheels and the steering bones. I have added these below:
Note! The two arm bones point towards the centre of the back axle, to make them I made a bone from steering centre to centre of back axle then scaled it down.
- Rear wheel bones are parented to root
- front wheel bones are parented to the arm bones.
- Arm bones are parented to root.
- Rack bones are parented and connected, to their arm bones.
- Steer-control is parented to root
- Steer-IK is parented to steer-control.
Now we add two IK Constraints to the two rack bones with steer-IK as the target and a chain length of 2. Now when we rotate the steer-control bone, the steering works:
Next we must turn our attention to the wheels’ rotation. We need some principle first:
- A round object of 1 unit radius, when rolled will rotate 1 Radian for every 1 unit it moves forward. Therefore a Radian is the angle subtended by 1 unit around the circumference of the rolled object.
- This can be expressed in degrees as 360 / 2 * pi, or about 57.28835136855506 degrees.
- A round object of 2 units radius will rotate 1/2 Radian when moved forward 1 unit.
- The formula therefore is rotation = distance moved divided by radius.
So, in order to rotate the wheels correctly we must know their radius, we can then use this to put into a driver for each wheel axle:
My wheels here are 0.6 units diameter – hence the driver is var / 0.6 where var is the forward movement of the fix object – everything related back to fix! You may need to add a minus sign to make the wheel turn the right way – this you will see when you move the fix object. The simply copy the driver to the other wheel bones, by RMB click the purple box => Copy Driver then go to the next bone and RMB Click => Paste Driver.
Now all we need do is parent the Armature, in Object Mode, to fix and the wheels move as they should. Then “Bone” parent the wheels to their respective bones (Select wheel, SHIFT+Select wheel bone, key CTRL+P => “Bone”. You will not see them rotating unless you add a feature that you can see turning of course!
Now we have the small matter of steering the wheels. This is accomplished by adding a new bone – steer-track – to the rig, it is positioned at the centre of the back axle and is parented to the root bone. This bone needs a Damped Track Constraint so it points towards the target object as below:
Note! We have a little problem here in that the target object is NOT moving in World Space around the curve, as far as it is concerned, it is moving along the X axis, so we need to add another object, I used an Empty, parented to the target object for the Damped Track Constraint to use as a target.
Now if you move the target object, this bone follows it. To complete the procedure, we need to make steer-control rotate when steer-track rotates, by a desired ratio.
Here is the project now:
So what I have done is to put the vehicle on a fixed radius curve, then added three empties (the black radial lines) at the centre of the curve. The view above shows the front wheels both aligned toward the centre of rotation as they should be. The driver for the steer-control is set at var * 2 where var is the X rotation in Local Space of the steer-track bone. This is where you check the value for the driver, inserting the appropriate factor for your vehicle length. We could do all the maths to calculate this, but there is no need with this simple setup and check procedure.
There are a few things to watch out for, one is when the vehicle comes off a curve onto a straight, here it is best to actually mod the curve slightly, since you can never drive a car from a fixed radius to straight in a nano-second, I tend to shape the curve to be more precise to real life, showing a gradual opening out of the turn.
In this example below:
I have set the target object to be only 3 units away, rather than 5, from the fix object, then modified the driver so it still keeps the wheels in line. The advantage of this is that the system is now only looking 3 units ahead, so is more attuned to what is happening quickly rather than further down the line. This helps to control the steering better when coming off curves onto straights, or off straights onto curves. You can experiment with this, just remember to re-check your drivers so the wheels point towards the centre of radius of the curve.
All that is left to do is to add some keyframes, or other methods, to animate the vehicle down the track. The curve of the track can also be used to form the road, by adding a suitable Bevel Object. I make these as a profile of vertices, then convert this to a curve object (ALT+C => Curve From Mesh) then add this as the curves bevel object.
Finally, you will want to look at the resolution of the curve, I use at least 64, otherwise the animation is “clunky” rather than smooth. Then just add some bodywork and you’re done!
Should probably add some windows as well, might make it easier to drive.
No sooner said than done!
OK, so I will spend some time on materials also, I know it’s only a tutorial…
Cheers, Clock. 😛
OK, lets stick a trailer on the back of a truck:
The truck is going to be rigged exactly the same as the car, so you know how to do that, if you have read the above and absorbed it into your grey matter.
Some principles of trailers:
- They always follow the truck, assuming no breakages, at a set distance away.
- They always describe a smaller radius arc than the truck when going around a corner.
- The transition into and out of the corner is incredibly difficult to define mathematically, but can be done.
- Their axle, or centre of their axles always points towards the centre of the curve, incidentally also the pivot point for the truck.
The second point and pictures later on, illustrate why you should not try to pass an articulated truck on your push bike when it is turning, unless you want to look like a pizza that is.
The easiest way to track the trailer is to have a second curve that the trailer axles follow, rather than try to turn the trailer by mathematical formula, although this is possible and not too hard if you are good at maths like me! So we add a new curve for the trailer to follow, we add a new fix object to run along the curve, but we don’t add any sort of driver for it because as I am sure you have worked out by now, the trailer wheels go a shorter distance than the truck wheels when going around a corner.
Here is a picture, what is wrong with it?
The answer is that the trailer wheels are not pointing at the centre of the curve, the selected empty, this is because the track for the trailer is not right. It is in fact too large a radius, the trailer is too far out and the curve needs adjusting thus:
In the image above the curve profile is still not right, as it is not tangential to the trailer, so we correct this by shifting the curves vertices, which you will see in the next image below. In this next image below, the wheels point at the centre of the curve and the curve is tangential to the trailer, all is well.
The next thing to note is that the trailer wheels should use the position of the trailer fix object, not the truck fix object to determine their rotation, otherwise they go too fast.
Having got the track right, so the wheels are in the correct position, we then need to add some keyframes to move the truck around the track, as we did before with the car, then some keyframes to move the trailer fix object around its track:
Here I have advanced the truck around the track, then moved the trailer fix object so it sits under the centre of the trailer axles, then keyframed its X location. I did this several times to keep it where it needs to be. This is a bit of trial and error, but it saves doing all the complex maths.
There, that didn’t hurt too much did it? There are many papers published on the ‘net that go into precise detail for all the maths involved in this, they make good reading, but it is almost impossible to recreate the maths in Blender, even in a Python script. I have done this to make the trailer be at the correct position on a curve, but not yet spent sufficient hours to make the transition from straight to curve work perfectly. Doing it by this method I have described works perfectly well, so I shall not pursue the perfect mathematical model any further.
Below we can see the the truck has successfully negotiated most of the corner without knocking any cones over:
Here’s a nice little document all about the maths for doing this and it has some nice pictures to explain things:
From that paper above, we can find this image:
The truck starts just over halfway up the left side and is the outside track, the trailer axle starts at bottom left and is the inside track. This will give you a good idea of how to shape your trailer curve. I can remember dot-matrix printers, shows my age I guess. You will note that the trailer wheels are not going to be pointing at the curve centre all the time, this is because it is a very tight turn and some of the trailer wheels are going to “scrub” and leave black marks on the road. There are some extreme examples of this in the paper that also show when the trailer starts to go backwards during even tighter turns.
We also find this one to show how to calculate the offset and various radii of the axles of the articulated lorry, also known as a “truck-semitrailer” on the wast side of the Big Pond:
For multi-axles trailers, just use the midway point between the trailer axles to get length L2.
I should check this before pressing Publish, I should also come home sober at night. I should not bite the heads off miniature poodles, so many things one should or should not do…..