basic movement ai
DESCRIPTION
Basic Movement AI. Chasing, Evading, Intercepting, Pattern Movements. Movement AI. Movement – Ubiquitous behavior found in many games Chasing and Evading – the most basic movement AI that can be implemented on NPCs. Chasing & Evading. 2 parts Decision to initiate a chase or evade - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/1.jpg)
Basic Movement AIChasing, Evading,Intercepting, Pattern Movements
![Page 2: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/2.jpg)
Movement AI
•Movement – Ubiquitous behavior found in many games
•Chasing and Evading – the most basic movement AI that can be implemented on NPCs
![Page 3: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/3.jpg)
Chasing & Evading
•2 parts▫Decision to initiate a chase or evade▫Effecting the chase or evade
•Sometimes, a 3rd part, to avoid obstacles while chasing and evading is required, but that can be considered separately
•1st part – decision making•Focus on 2nd part – concerns the action
itself, to chase and evade
![Page 4: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/4.jpg)
Basic method
•Simplest method: Update NPC coordinates through each game loop to ▫Decrease the distance between NPC and
player (chase)▫Increase the distance between NPC and
player (evade)•Pays no attention to NPC or player
headings (direction of travel) or speeds
![Page 5: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/5.jpg)
More complex methods
•Positions and velocities (direction of travel) of both NPC and player are considered so that NPC can be moved to intercept the player instead of relentless chasing
•In short: Predicting where the player will likely to be at in the future, and moving towards that as target
![Page 6: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/6.jpg)
Basic Chasing
•Pseudocode for Chasingif (npcX > playerX)npcX--;
else if (npcX < playerX) npcX++;
if (npcY > playerY) npcY--;
else if (npcY < playerY) npcY++;
![Page 7: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/7.jpg)
Basic Evading
•Moving farther, instead of moving closer•Pseudocode for Evadingif (npcX > playerX)npcX++;
else if (npcX < playerX) npcX--;
if (npcY > playerY) npcY++;
else if (npcY < playerY) npcY--;
![Page 8: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/8.jpg)
Tile-based environment – How?
•8-way movement
![Page 9: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/9.jpg)
Tile-based Chase movement• Updating difference
▫Continuous environment: x, y coordinates in Cartesian coordinate system (float)
▫Tile-based environment: x, y in tile coordinate system (int.) are columns and rows
if (npcCol > playerCol)npcCol--;
else if (npcCol < playerCol) npcCol++;
if (npcRow > playerRow) npcRow--;
else if (npcRow < playerRow) npcRow++;
![Page 10: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/10.jpg)
Tile-based Chase – Any problem?
![Page 11: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/11.jpg)
Line-of-sight Chasing
•Make NPC take a direct straight-line path towards player
•Stationary player – straight path•Moving player – path is not necessarily
straightNote may not look too bad if game loop constantly updates position, path should appear curved and natural
•Movements in TBE will appear less smoother than CE due to coarse movement units
![Page 12: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/12.jpg)
L-o-S Chasing in TBE
• Aesthetic disadvantage of using simple basic chase – although no. of steps are the same
• Limitation of 8-way movement, no other possible angles
• Will look really BAD if many NPCs are chasing this way
![Page 13: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/13.jpg)
L-o-S Chasing in TBE•Solution: To calculate an intermediate path
(closest to a straight line) for the NPC to chase•Use a standard line-drawing algorithm –
Bresenham’s algorithm▫Nice thing: Does not draw two adjacent pixels
along a line’s shortest axis•Path calculation function
▫Calculate and store a series of tiles to be moved to▫Needs to be called every time player changes
position
![Page 14: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/14.jpg)
L-o-S Path in TBE
![Page 15: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/15.jpg)
“Looking At” Rotation• Without “Looking At”
Rotation
• Only x and/or y are updated at each time (8-direction)
• With “Looking At” Rotation
• Additional rotation steers NPC to look at target and chase in that direction
(x1, y1)
(x2, y2)
(x1, y1)
(x2, y2)
θ
![Page 16: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/16.jpg)
Thrust and Steering forces
•Thrust force: pushes object to the front (velocity)
•Steering force: pushes the nose of object to the right or left to rotate (angular velocity)
![Page 17: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/17.jpg)
L-o-S Chasing in CE
•No need to determine path like in TBE, movement updates are in floating-point (rounding to nearest pixel integer gives good approximation)
•Similarly, rotation angle (“looking at” direction) should be re-calculated every time player changes position
![Page 18: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/18.jpg)
L-o-S Chasing in CE
• Curved path taken by predator when chasing prey
• What happens if the speed of the predator is set too fast?
![Page 19: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/19.jpg)
Preventing overshooting
•Implement speed control logic to allow predator to slow down as it gets closer to prey▫ Calculate distance between both, if distance <
some predefined distance, reduce thrust velocity to slow down
![Page 20: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/20.jpg)
L-o-S Evading•Since we now understand L-o-S Chasing, L-o-S
Evading is just doing everything the reverse
•TBE▫Update movement in opposite direction (good
enough!)•CE
▫“Looking at” rotation should steer to opposite face
▫Update movement in opposite direction
![Page 21: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/21.jpg)
Further improvements?
•Acceleration (and deceleration) for thrust force and steering force?
•Any way to improve “intelligence” of chasing movement?
![Page 22: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/22.jpg)
Intercepting• Line-of-Sight Chase: NPC will always head
directly towards player • In the case of a moving target, what happens?
NPC
Player
Line-of-sight Chase
![Page 23: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/23.jpg)
Intercepting• Movement can be more “intelligent”, if it knows
how to intercept the player somewhere along the player’s “trajectory”.
• How do we work this out? What information do we need?
NPC
Player
Line-of-sight Chase
![Page 24: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/24.jpg)
Intercepting – In Principle
•Predict some “future” position of the player and move towards that position, so that it reaches the same time as player…
NPC
Player
Line-of-sight Chase
Intercepting Chase
A “future” position of player
![Page 25: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/25.jpg)
Intercepting – A “Future” Position•“Future” can be safely predicted linearly•How to compute this position?
NPC
Player
Line-of-sight Chase
Intercepting Chase
A “future” position of player
![Page 26: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/26.jpg)
Intercepting
•Important to consider relative velocities (direction and magnitude) and distance instead of just their current positions
•Let’s work out the math to determine what is the predicted future position…
![Page 27: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/27.jpg)
Intercepting - Code
•Recall: You would have implemented the Enemy class already, and have it to chase the player. Target Player position
•For Intercept, Target Player’s predicted future position▫Easy to modify and adapt code from Chase to
Intercept•Function to be called through the game
loop to constantly update interception point
![Page 28: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/28.jpg)
Intercepting
![Page 29: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/29.jpg)
Intercepting
•There are scenarios where intercepting may not be possible or unrealistic▫NPC moving at much slower velocity▫NPC ends up chasing from behind a player
moving in straight line ▫NPC gets ahead of player and moving at a
faster speed
![Page 30: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/30.jpg)
Pattern Movement
•Simple way of giving the illusion of intelligent behavior
•“Choreographed” movements of enemy NPCs making organized maneuvers, loops
•Can be used effectively for ▫Enemy NPCs (patrol, attack)▫Friendly NPCs (to exhibit or act
intelligently to cooperate with player)▫Secondary NPCs (doing all kinds of random
patterned actions)
![Page 31: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/31.jpg)
Standard Pattern Movement
•Uses lists or arrays of encoded instructions to tell NPC how and where to move each step▫Easy to loop through again
•Sample control data
ControlData { double turnRight; double turnLeft; double stepForward; double stepBackward;
};
![Page 32: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/32.jpg)
Standard Pattern Movement
•Can include other kinds of actions:▫Fire weapon, release chaff, drop bomb, do
nothing, speed up, slow down, etc.•Pattern initialization
▫Hardcoded in game▫Loaded from a data file (text, XML)
•Process pattern▫Maintain and increment an index to the
pattern array through the game loop
![Page 33: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/33.jpg)
Standard Pattern Movementvoid GameLoop(void) { . . .
Object.orientation += Pattern[CurrentIndex].turnRight;
Object.orientation -= Pattern[CurrentIndex].turnLeft;
Object.x += Pattern[CurrentIndex].stepForward; Object.x -= Pattern[CurrentIndex].stepBackward;CurrentIndex++;
. . . }
![Page 34: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/34.jpg)
Pattern Movement in TBE
•Bresenham’s line drawing algorithm used again to pre-calculate path steps
•More complex paths can be created by joining up line segments▫Each new segment will begin once a
previous one ends▫Line-of-sight function End of segment
now assigned as the target
![Page 35: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/35.jpg)
Pattern Movement in TBE
• Instead of resetting L-o-S path array, new line segment is appended to previous path (Why do we do this?)
![Page 36: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/36.jpg)
Pattern Movement in TBE
•High-level code for pattern initializationentityList[1].InitializePathArrays(); entityList[1].BuildPathSegment(10, 3, 18, 3); entityList[1].BuildPathSegment(18, 3, 18, 12); entityList[1].BuildPathSegment(18, 12, 10, 12); entityList[1].BuildPathSegment(10, 12, 10, 3); entityList[1].NormalizePattern(); entityList[1].patternRowOffset = 5; entityList[1].patternColOffset = 2;
![Page 37: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/37.jpg)
Patrolling: Cyclical & Ping PongA B
C
A B
CD
ABCBABCBA ABCDABCD
![Page 38: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/38.jpg)
Pattern Movement in TBE
•Ping Pong PatrollingentityList[1].InitializePathArrays(); entityList[1].BuildPathSegment(10, 3, 18, 3); entityList[1].BuildPathSegment(18, 3, 10, 3); entityList[1].NormalizePattern(); entityList[1].patternRowOffset = 5; entityList[1].patternColOffset = 2;
![Page 39: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/39.jpg)
Pattern Movement in TBE
•To prevent repetition and predictability Add a random factor for choosing alternative patrolling path
How to implement?
![Page 40: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/40.jpg)
Pattern Movement in PSE
•PSE – Physically Simulated Environments (or Continuous Environments with Physics)▫You may specify positions for NPC to
traverse, but that defeats the purpose of using CE. Bad realism!
•Control structure needs to be designed to accommodate forces (thrust, steering)
![Page 41: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/41.jpg)
Pattern Movement in PSEstruct ControlData {
bool PThrusterActive; bool SThrusterActive; double dHeadingLimit; double dPositionLimit; bool LimitHeadingChange; bool LimitPositionChange;
};
• Changes in heading and position are relative in patrolling
• Based on this control data, how do you initialize a square pattern patrol?
![Page 42: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/42.jpg)
Pattern Movement in PSE• More implementation details can be found in
textbook
![Page 43: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/43.jpg)
Wander• Useful movement for creating “random” walks
through the environment• Naïve approach:
▫ Calculate a random steering force each time step▫ But, produces jittery behavior with no long
persistent turns• Craig Reynolds’ solution:
▫ Project a circle in front of the NPC▫ Steer towards a target that is constrained to move
along the perimeter (of the circle)▫ Each time step, a random displacement is added to
this target move back and forth the perimeter
![Page 44: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/44.jpg)
Wander
• Jitter-free wander movement• Of course, you can always implement the naïve
version but increase the time delay required to trigger the next steering change still turn abruptly, but wandering improves
![Page 45: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/45.jpg)
Wander• From previous target
position, add a small random displacement to target
• Project target back onto wander circle (normalize to unit circle and multiply radius)
• The new target position is moved in front of the NPC by the wander distance set
![Page 46: Basic Movement AI](https://reader036.vdocument.in/reader036/viewer/2022062518/5681451a550346895db1dcf2/html5/thumbnails/46.jpg)
Other Useful Movements
•Velocity Matching (for friendly NPCs, flocking)
•Obstacle Avoidance
(can be found in Millington’s book)