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

The Goals
A goal on a real-life soccer pitch is defined by a left goal post and a right
goal post. A goal is scored if any part of the ball crosses the goal line —
the line connecting the goal posts. A rectangular area in front of each goal
is drawn in the relevant team’s color to make distinguishing each team’s
side easy. The goal line is the line that describes the rear of this box.
Here is the class declaration:
class Goal
{
private:
Vector2D m_vLeftPost;
Vector2D m_vRightPost;
//a vector representing the facing direction of the goal
Vector2D m_vFacing;
//the position of the center of the goal line
Vector2D m_vCenter;
//each time Scored() detects a goal this is incremented
int m_iNumGoalsScored;
public:
Goal(Vector2D left, Vector2D right):m_vLeftPost(left),
m_vRightPost(right),
m_vCenter((left+right)/2.0),
m_iNumGoalsScored(0)
{
m_vFacing = Vec2DNormalize(right-left).Perp();
}
//Given the current ball position and the previous ball position,
//this method returns true if the ball has crossed the goal line
//and increments m_iNumGoalsScored
inline bool Scored(const SoccerBall*const ball);
/* ACCESSOR METHODS OMITTED */
};
Each time step, the Scored method of each team’s goal is called from
within
SoccerPitch::Update. If a goal is detected, then the players and ball
are reset to their start positions ready for kick-off.
The Soccer Ball
A soccer ball is a little more interesting. The data and methods to encapsu
-
late a soccer ball are encoded in the
SoccerBall class. A soccer ball moves,
so its class inherits from the
MovingEntity class we used in Chapter 3. In
addition to the functionality provided by
MovingEntity, SoccerBall also has
138 | Chapter 4
The Simple Soccer Environment and Rules
data members for recording the ball’s last updated position and methods
for kicking the ball, testing for collisions, and calculating the future posi
-
tion of the ball.
When a real soccer ball is kicked it gently decelerates to rest because of
the friction from the ground and the air resistance acting upon it. Simple
Soccer balls don’t live in the real world, but we can model a similar effect
by applying a constant deceleration (a negative acceleration) to the ball’s
motion. The amount of deceleration is set in Params.ini as the value
Friction.
Here is the complete declaration of the
SoccerBall class followed by
descriptions of a couple of its important methods.
class SoccerBall : public MovingEntity
{
private:
//keeps a record of the ball's position at the last update
Vector2D m_vOldPos;
//a pointer to the player(or goalkeeper) who possesses the ball
PlayerBase* m_pOwner;
//a local reference to the walls that make up the pitch boundary
//(used in the collision detection)
const std::vector<Wall2D>& m_PitchBoundary;
//tests to see if the ball has collided with a wall and reflects
//the ball's velocity accordingly
void TestCollisionWithWalls(const std::vector<Wall2D>& walls);
The soccer ball only checks for collisions with the pitch boundary; it
doesn’t test for collisions against the players, as the ball must be able to
move freely around and through their “feet.”
public:
SoccerBall(Vector2D pos,
double BallSize,
double mass,
std::vector<Wall2D>& PitchBoundary):
//set up the base class
MovingEntity(pos,
BallSize,
Vector2D(0,0),
-1.0, //max speed - unused
Vector2D(0,1),
mass,
Vector2D(1.0,1.0), //scale - unused
0, //turn rate - unused
0), //max force - unused
m_PitchBoundary(PitchBoundary),
m_pOwner(NULL)
Sports Simulation — Simple Soccer
| 139
The Simple Soccer Environment and Rules

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