Docsity

CS410 Winter 2007 Lecture Notes: AI and Game Design - Chasing and Evading, Study notes of Computer Science

Partial preview of the text

Download CS410 Winter 2007 Lecture Notes: AI and Game Design - Chasing and Evading and more Study notes Computer Science in PDF only on Docsity!

AI and Game Design

CS 410/

Lecture 2 : Chasing and Evading

Professor Bryant W. York^ January 23, 2007

Chasing and Evading

  • Chasing and evading in continuousenvironments • Line of sight chasing • Line of sight chasing in tiled environments • Line of sight chasing in continuousenvironments • Intercepting

Basic Evading

If (preyX > predatorX) preyX++; else if (preyX < predatorX) preyX--; If (preyY > predatorY) preyY++; else if (preyY < predatorY) preyY--; •^ Coordinates checked and updated eachcycle through the game loop

Environments

  • Tile Based Environments^ – Player’s position is fixed to a discrete tile^ – Movement is tile by tile^ – Limited number of directions • Continuous Environments^ – Position is represented by floating point

coordinates – Player can move in any direction

Line of Sight Chasing

  • The predator always takes a straight linepath towards the prey • The predator always moves towards theprey’s current position • If the prey is not moving – the pathfollowed is a straight line • If the prey is moving the path will becurved • Result: movement appears more natural

Aesthetics York CS410 Winter 2007

-^ In a continuous environment position isrepresented by floating point numbers which getmapped to the nearest screen pixel at rendertime. •^ Changing position in a continuous environment,you do not have to limit yourself to adjacentscreen pixels. •^ A small number of pixels can be skippedbetween each screen redraw without sacrificingmotion fluidity.

Bresenham’s Algorithm

  • Predator’s position is start point • Prey’s position is ending point • This function must be called each time theprey moves • Once the prey moves, the precalculatedpath becomes obsolete

Build Path to Target

void ai_Entity::BuildPathToTarget(void) {^ int nextCol = col;^ int netRow = row;^ Int deltaRow = endRow – row;^ int deltaCol = endCol – col;^ int stepCol, stepRow;^ int currentStep, fraction;

Path Direction Calculation

If (deltaRow < 0) stepRow = -1; else stepRow = 1; If (deltaCol < 0) stepCol = -1; else stepCol = 1; deltaRow = abs(deltaRow2); deltaCol = abs(deltaCol2); pathRow[currentStep] = nextRow; pathCol[currentStep] = nextCol; currentStep++ •^ Pick the direction with the longest axis

Bresenham’s Algorithm

-^ Example 2-7. Bresenham algorithm •^ if (deltaCol > deltaRow) •^ { •^

fraction – deltaRow *2-deltaCol;

-^

while (nextCol != endCol)

-^

{

-^

if (fraction >=0)

-^

{

-^

nextRow =nextRow +stepRow;

-^

fraction =fraction –deltaCol;

-^

}

-^

nextCol=nextCol+stepCol;

-^

fraction=fraction +deltaRow;

-^

pathRow[curr

entStep]=nextRow;

-^

pathCol[currentStep]=nextCol;

-^

currentStep++;

-^

}

-^ } •^ else •^ { •^

fraction =deltaCol *2-deltaRow;

-^

while (nextRow !=endRow)

-^

{

-^

if (fraction >=0)

-^

{

-^

nextCol=nextCol+stepCol;

-^

fraction=fraction –deltaRow;

-^

}

-^

nextRow =nextRow +stepRow;

-^

fraction=fraction +deltaCol;

-^

pathRow[curr

entStep]=nextRow;

-^

pathCol[currentStep]=nextCol;

-^

currentStep++;

-^

}

-^ }

Motion York CS410 Winter 2007

-^ Vehicles are identical •^ Pushed by applied thrust and can turn via activation ofturning forces •^ Steering forces are “bow thrusters” •^ A steering force is applied that will push the nose of thevehicle to the left/right •^ The LOS algorithm will control activation of the steeringforces so that predator is always kept heading towardsthe prey •^ The max speed and turning angle are limited due tolinear and angular drag forces that are calculated andapplied to each vehicle •^ Turning radius is a function of linear speed – the higherthe linear speed, the larger the turning radius – leads tosmoother, more natural paths.

LOS Chase Function

  • Called every time step through thesimulation – i.e. every cycle through thephysics engine loop • The predator constantly calculates theprey’s position relative to itself and thenadjusts its steering to keep itself pointed atthe prey.

York CS410 Winter 2007

LOS Chase Function

-^ u^ and

v^ are

Vector

types

-^ Vector

has operations defined:

  • Add, subtract, dot product, cross product – Others • left^ and

right

are boolean variables to indicate which steering force to turn on^ – Both initially

false

-^ Calculate the relative position vector betweenthe predator and prey in global, earth-fixedcoordinates. •^ VRotate2D converts the vector to the predator’slocal, body-fixed coordinates. •^ Result is stored in

u^ and then

u^ is normalized

York CS410 Winter 2007

LOS Chase Function

-^ u^ is a unit vector pointing from thepredator directly towards the prey • Next few lines determine if the prey is tothe port side, the starboard side, or directlyin front of the predator • The vehicle always heads along the

local,

positive, y-axis

-^ SetThrusters

function