Chap. 06B --- Radio-Controlled Movement

Radio-Controlled Movement Basics

If you have ever driven a radio-controlled car, when you push forward on the stick, it moves not in the direction the driver is facing, but in the direction the car is facing. If you tell it to turn left, it turns to the car's left, not to the driver's left. We call this radio-controlled movement: movement in which directions are expressed relative to the vehicle's own perspective.

This sort of "radio-controlled movement" has a long history in graphics. Probably the first example was a language called Logo which contained a feature called "Turtle Graphics", in which you could give an imaginary turtle commands like "move forward," "turn left 3 degrees," and so forth. The turtle would leave behind a trail, and this is how you did graphics in Logo.

Wild Pockets doesn't contain the commands "move forward," "turn left," and "turn right," but you can get the same effect very easily.

How to Get Radio-Controlled Movement

As mentioned earlier, the commands setPosition, setHeading, and so forth, all take an optional relative-to parameter. What may not be immediately obvious is that relative-to parameter can be the object itself, like this:

car:setPosition(0,1,0,car)

Think of this as occuring in two steps: first, it calculates (0,1,0) relative to the car: ie, the point one meter in front of the car. Then, it moves the car to this point. So the net effect is that the car moves forward one meter.

Here's another example:

car:setHeading(3,car)

In this example, we calculate a rotation which is 3 degrees left of where the car is currently oriented. Then, we set the car's rotation. The net effect is that the car turns 3 degrees to the left. (If we had said -3, the car would turn 3 degrees to the right.)

Command Equivalents

Here is a list of commands that do similar things to what turtle graphics commands do:

Move forward   obj:setPosition(0,distance,0,obj)
Move backward   obj:setPosition(0,-distance,0,obj)
Strafe right   obj:setPosition(distance,0,0,obj)
Strafe left   obj:setPosition(-distance,0,0,obj)
Turn left   obj:setHeading(degrees,0,0,obj)
Turn right   obj:setHeading(-degrees,0,0,obj)

Look Out for Floating-Point Errors

If you use radio-controlled movement on an object thousands of times, the resulting floating point errors can slowly accumulate. An object which is supposed to remain upright at all times can slowly tilt to one side. An object which is supposed to stay on the ground can slowly start rising or sinking. The solution is to do periodic corrections. There are two commands that are particularly useful:

obj:setUpright()

If an object is supposed to stay upright, force it to stay upright.

obj:setZ(0)

If an object is supposed to stay at ground level, force it to stay there.