truck-and-trailer problem
Back to raytracing page


In August 2002, there was an interesting discussion in the povray newsgroup in how a trailer will follow its truck on a given path in a realistic way and how this could be implemented in POV. So I developed and posted a model I will present in more detail here.

It was a really interesting theme. So many thanks to Thomas who had the idea of it all and greetings to all the other ones in this discussion.

Please excuse the truck driving 'into' the pole - this model does not have a collision-detection or any angle limits.

 


the mathematical model


Since a real physical simulation would be too much for this (taking into consideration the masses and center of gravity of each piece, the speed and even the friction of the ground etc.), a very simple mathematical model will result in a nice natural movement:

Let's build a simple trailer with a single axle and a triangle to the front where it can be pulled by hand. In the following, this simplest trailer will be called 'pole' because we only need to describe the length of it for our model. The width of the axle does not affect the behavior.

In the drawings, every pole has a back axle (AB) orthogonal to the pole's direction, a link at the front where to pull it (C) and one at the back (center of the axle (M)) where we can connect the next pole. This will be the origin of the pole's coordinate system.

  The problem to solve is: how can we describe a simple movement of this pole when pulling the link (C) to any given point (P)?

Here are the rules this pole can be moved without hurting physics:

 
1. Turn the pole around its origin (M) towards the target (P).   2. Move the pole along the line (MP) until its link (C) hits target (P).

To get a smooth and natural movement, we have to turn, move, turn, move, turn, move... several times until we are there.

 

Watch the right wheel first moving a little backwards in this 5-step-simulation of a hard turn.

Every step was constructed by first turning towards, then moving towards the next red point.

 

 

some math

All we have to know to get the new position of the pole are the current position of the pole's origin (M) and the length of the pole. And, of course, the new position where the link shall be (P). In the following, we use the variables 'PosX, PosY' for (M), 'NewX, NewY' for (P), 'Length' for the trailer's length and 'Angle' for the target angle.

 

First get the vector d pointing from (M) to (P) - the blue diagonal line:

dx = NewX - PosX
dy = NewY - PosY

get the angle we have to turn the pole (it is the same angle as in (P) of the blue triangle) using arcus tangens:

Angle = atn ( dx / dy )

 

get the new position of (M) which is the old position plus vector d shortened by the length (red line) of the trailer:

d = sqrt ( dx ^ 2 + dy ^ 2 )
tomove = d - length
PosX = PosX + dx / d * tomove
PosY = PosY + dy / d * tomove

All we have to do now is to draw a model of our pole (with the model's origin at (M)), turn it by 'Angle' and move it to 'PosX, PosY'.

 

 
vehicles

With the combination of several poles we can build any vehicles from cars to truck trains.

The first pole follows the path, the second follows the origin of pole #1, the third follows the origin of pole #2 and so on...

Note that the construction of a simple car already needs two poles: the first one is invisible and describes the point the driver is aiming at (see the red line). We can use the angle of this invisible pole to draw realistic steering front wheels - which will be implemented in POV below.

Here are some vehicles that can easily be build with this model:

  A truck with a two-wheeled trailer (using 3 poles)

  A truck with a four-wheeled trailer (using 4 poles)

 

 

flash simulation


To test the look-and-feel of this model, I used flash action script. Try to drive backwards - it behaves like a real truck!

 

 

 


POV implementation


// Here is the 'heart' POV-code for the mathematical model. This macro will alter PosX, PosY and Angle of a defined pole array by calling it with the trailer's index and the target position:

#macro driveTo (Pole, NewX,NewY)

//get the vector [axle --> new position]
#declare dx=NewX-PosX[Pole];
#declare dy=NewY-PosY[Pole];

//get the new position of the pole
#declare d=sqrt(dx*dx+dy*dy);
#declare tomove=d-Length[Pole];
#declare PosX[Pole]=PosX[Pole]+dx/d*tomove;
#declare PosY[Pole]=PosY[Pole]+dy/d*tomove;

//get the angle of the pole
#declare Angle[Pole]=0;
#if (d!=0)
#declare Angle[Pole]=atan2(dx,dy)/pi*180;
#end
#end

// Then we have to define the pole array:

#declare Poles=2;

#declare PosX=array[Poles]
#declare PosY=array[Poles]
#declare Length=array[Poles]
#declare Angle=array[Poles]

#declare PosX[0]=10;
#declare PosY[0]=0;
#declare Length[0]=4;

#declare PosX[1]=0;
#declare PosY[1]=2;
#declare Length[1]=3;

// Calling the macro (pole #0 follows the actual path coordinates, pole #1 follows the origin of pole #0 and so on...) looks like this:

//defining any target
#declare
PathX=2;
#declare PathY=5;

driveTo (0,PathX,PathY)
driveTo (1,PosX[0],PosY[0])

// Now, the arrays contain the actual position and angle of each pole and we can do the drawings. On the x-z-plane (a simple white box, 1 unit wide (x-direction), 2 units high (y-direction) and with the defined length(z-direction)) this will look like the following code:

#declare Pole=0;
box {<-0.5,1,0> <0.5,2,Length[Pole]>
pigment{color rgb 1}
rotate y*Angle[
Pole]
translate <PosX[
Pole],0,PosY[Pole]>
}

#declare Pole=1;
box {<-0.5,1,0> <0.5,2,Length[Pole]>
pigment{color rgb 1}
rotate y*Angle[
Pole]
translate <PosX[
Pole],0,PosY[Pole]>
}

 

 
more problems

For a fully functional POV animation we need a little more:

  • since we have to know the last position of each trailer to get the next (and POV does not store the variables from frame to frame) we have to re-compute the whole movement step by step up to the current frame
  • realistic steering front wheels will follow the angle of the first (invisible) pole but use the coordinate system of the second one
  • we have to think about good initial positions of the trailers, which affects the direction each trailer is linked in the first step
  • we need a function for a path the truck will follow
  • we should think about the camera where to look at (and in bigger scenes, where to put it)
  • and last but not least, we need a 3D truck model with wheels, axles, poles and containers

All of this is solved and commented in the POV-source to the right. Render it as an animation with 100 frames and a clock from 0 to 1 and you will get the following:

QuickTime movie (320x240, 4 secs, 471k)

An interesting thing: the truck was never told to drive backwards and turn around. The only rule was to follow the blue line to the right. Uncommenting the red arrow of pole #0 in the source code will help you to understand this behavior...

 


related links

PovRay newsgroup