Day 1: Movement

So we have a robot which rolls forward slowly. We are not going to last 2 minutes in the Arena, let alone win any prizes, if we don’t do something a bit more fancy than that!

Today we will cover movement. By the end of this workshop you should be able to start, stop, turn, and ram your opponents.

 

Getting Started

Each of the robots has its own method of moving. The Cannon and Rocket robots (1&2) use tank like treads, the Dual MG robot (3) has wheels, the Laser robot (4) hovers on two anti-gravity repulsers. Each of these methods give the robot very distinctive movement styles. Treaded robots are slow but easy to control, wheeled robots are fast but reckless, hover robots fall somewhere in-between.

Lucky for us, all these robots use the same basic interface to move. By simply applying forces to the left and right drives of the robot and turning on and off the brake, we can make our robots move with all the grace of butterfly or crash ourselves into random walls. ;)

 

Left and Right Forces

The two values you will be using most often for movement are "force_left" and "force_right". These values are used to set the percent of forward or backward thrust that will be applied to either side of the robot. The input can range from –100 (full backward force) to +100 (full forward force). Anything that falls outside this range will be truncated to the nearest value in this range (i.e. force_left = 500 will become force_left = 100).

 

Moving Forward and Backwards

In our first robot script we set both of the robot’s forces equal to 50%. This made our robot move forward at half its maximum speed. Increase this value to 100 and you will go faster. A negative value and you will end up back in the tunnel.

function AI_ROBOT()
{
   // full speed ahead!!!
   force_left = 100;
   force_right = 100;
}

 

function AI_ROBOT()
{
   // back-up slowly
   force_left = 25;
   force_right = 25;
}

As long as the left and right values remain equal, the robot will move in a straight line.

 

Brake

Racing at breakneck speeds can get you into trouble quickly. Sometimes you want to stop in a big hurry, which is why each robot is equipped with brakes. By setting the apply_brake flag to on, your robot will disengage its driven mechanism and come to a complete stop as quickly as possible.

For example, if we want to stop to within 700 quants of our opponent we could code the following:

function AI_ROBOT()
{
   // full speed ahead!!!
   force_left = 100;
   force_right = 100;
   
   // close to target?
   if(distance_to_target < 700)
   {
      apply_brake = ON; // stop
   }
   else
   {
      apply_brake = OFF; // continue
   }
}

NOTE! Variables such as force_left, gun1_fire, and apply_brake with retain the value they are given between calls to the AI_ROBOT function. Therefor it is important that we make sure that we set these values every frame (unless we want to use the previous value).

In the above example, you might be tempted to leave out the ‘else’ part of this statement. If you do this, everything will work correctly until the robot closes within 700 quants of the target. From then on, the brake value will be set on even after the target has moved outside of 700 quants.

If your robot is not moving in your game, check to make sure you are clearing the apply_brake value.

 

Turning

Moving in a straight line is all very good, but it is not going to help us much when we try to dodge incoming fire. If you have fooled around with the left and right force for a while you probably already guessed on how you can turn. If you apply different force values to either side of the robot the robot will turn. How much the robot turns depends on how different the forces are.

For example, the simplest example is turning in place, which is rotating the robot without moving forward or backwards. To do this simply apply the exact opposite force to the right side as you do to the left:

function AI_ROBOT()
{
   // rotate in place counter-clockwise
   force_left = 50;
   force_right = -50;
}

Reverse the forces and you turn the other direction:

function AI_ROBOT()
{
   // rotate in place clockwise
   force_left = -50;
   force_right = 50;
}

You can increase the rate of rotation by increasing the negative and positive values. For example, for maximum clockwise spin you would set force_left to -100 and force_right to 100).

Spinning in place is great method for tracking your opponent and aiming your weapons but sometimes you want to move forward and turn at the same time.

If the force values are unequal, but not 100% opposite, then the robot will move forward or backwards while turning to the left or right. For example:

 

function AI_ROBOT()
{
   // forward and to the left
   force_left = 50;
   force_right = 100;
}

This may be confusing at first, but all you need to remember is, when moving forwards, the robot will turn towards the side with less force and the opposite it true while moving backwards. To help clear this up lets do a practical example, let’s ram our opponent!!!

function AI_ROBOT()
{
   // turn towards target
   if(angle_to_target > 5) // target to the left…
   {
      // turn left
      force_left = -5;
      force_right = 100;
      apply_brake = OFF;
   }
   else
   {
      if(angle_to_target < -5) // target to the right…
      {
         // turn right
         force_left = 100;
         force_right = -5;
         apply_brake = OFF;
      }
      else // target dead ahead
      {
         // move forward, full speed
         force_right = 100;
         force_left = 100;
         apply_brake = OFF;
      }
   }
}

 

This simple function will make your robot charge it’s opponent. If your opponent is slower, or isn’t watching you closely, a collision will occur.

 

Conclusion

We’ve covered the basics of movement. Our robot should now be able to zoom around the arena floor and hunt down our opponents with ruthless efficiency. Tomorrow we cover weapons.