O'Reilly logo

Programming Game AI by Example by Mat Buckland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Goalkeepers
A goalkeepers job is to keep the ball from traveling over the goal line. To
do this, a goalkeeper utilizes a different set of skills than a field player and
is therefore implemented as a separate class,
GoalKeeper. A goalkeeper will
move backward and forward along the goal mouth until the ball comes
within a specific range, at which point it will move outward toward the ball
in an attempt to intercept it. If a goalkeeper attains possession of the ball, it
puts the ball back in play by kicking it to an appropriate team member.
A Simple Soccer goalkeeper is assigned to the region that overlaps its
team’s goal. Therefore the red goalkeeper is assigned to region 16 and the
blue goalkeeper to region 1.
Goalkeeper Motion
Along with having a completely different set of states than a field player,
the
GoalKeeper class must employ a slightly different setup for its motion.
If you observe a goalkeeper playing soccer you will notice that he is almost
always looking directly at the ball and that many of his movements are
from side to side, rather than along his facing direction like a field player.
Because an entity using steering behaviors has a velocity aligned heading,
a goalkeeper utilizes another vector,
m_vLookAt, to indicate facing direction,
and it’s this vector that is passed to the
Render function in order to trans-
form the goalkeepers vertices. The end result is an entity that appears to be
always facing the ball and can move laterally from side to side as well as
along its heading axis. See Figure 4.13.
170 | Chapter 4
Designing the AI
Figure 4.13. Goalkeeper movement
Goalkeeper States
A goalkeeper utilizes five states. These are:
n
GlobalKeeperState
n
TendGoal
n
ReturnHome
n
PutBallBackInPlay
n
InterceptBall
Let’s take a look at each one of these in detail to see what makes a goal
-
keeper tick.
GlobalKeeperState
Like the
FieldPlayer global state, the GoalKeeper global state is used as the
router for all the messages it can receive. A goalkeeper only listens for two
messages:
Msg_GoHome and Msg_ReceiveBall.
I think the code can speak for itself here:
bool GlobalKeeperState::OnMessage(GoalKeeper* keeper, const Telegram& telegram)
{
switch(telegram.Msg)
{
case Msg_GoHome:
{
keeper->SetDefaultHomeRegion();
keeper->ChangeState(keeper, ReturnHome::Instance());
}
break;
case Msg_ReceiveBall:
{
keeper->ChangeState(keeper, InterceptBall::Instance());
}
break;
}//end switch
return false;
}
TendGoal
When in the TendGoal state, a goalkeeper will move laterally across the
front of the goal mouth, attempting to keep its body between the ball and a
moving position located to its rear somewhere along the goal line. Here is
the state’s
Enter method:
void TendGoal::Enter(GoalKeeper* keeper)
{
//turn interpose on
keeper->Steering()->InterposeOn(Prm.GoalKeeperTendingDistance);
Sports Simulation — Simple Soccer
| 171
Designing the AI
//interpose will position the agent between the ball position and a target
//position situated along the goal mouth. This call sets the target
keeper->Steering()->SetTarget(keeper->GetRearInterposeTarget());
}
First, the interpose steering behavior is activated. Interpose will return a
steering force that attempts to position the goalkeeper between the ball and
a position situated along the goal mouth. This position is determined by the
GoalKeeper::GetRearInterposeTarget method, which assigns a position to
the target proportionally as far up the length of the goal mouth as the ball is
positioned up the width of the pitch. (I hope that sentence made sense
because I agonized over it for ten minutes and it’s the best I could do!)
Hopefully Figure 4.14 will help your understanding. From the goalkeepers
perspective, the farther the ball is to the left, the farther to the left along the
goal line is the interpose rear target. As the ball moves to the goalkeeper’s
right, the interpose rear target moves to the right of the goal mouth with it.
The black double-headed arrow indicates the distance the goalkeeper
attempts to keep between itself and the back of the net. This value is set in
params.ini as
GoalKeeperTendingDistance.
Let’s move on to the
Execute method.
void TendGoal::Execute(GoalKeeper* keeper)
{
//the rear interpose target will change as the ball’s position changes
//so it must be updated each update step
keeper->Steering()->SetTarget(keeper->GetRearInterposeTarget());
172 | Chapter 4
Designing the AI
Figure 4.14. Tending the goal

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required