lecture 4 movement in 3d path findingmarco/teaching/ay2014-2015/... · dm842 computer game...

41
DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding Marco Chiarandini Department of Mathematics & Computer Science University of Southern Denmark

Upload: others

Post on 20-Mar-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

DM842

Computer Game Programming: AI

Lecture 4Movement in 3D

Path Finding

Marco Chiarandini

Department of Mathematics & Computer ScienceUniversity of Southern Denmark

Page 2: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Outline

1. Movement in 3D

2. Pathfinding

2

Page 3: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Movement in 3D

So far we had only orientation and rotation in the up vector.

roll > pitch > yaw we need to bring the third dimension in orientation and rotation.

3

Page 4: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Euler angles

Orientation and rotation in 3D have 3 degrees of freedom 3D vector.

Euler angles represent the spatialorientation of any coordinate system(X ,Y ,Z ) as a composition ofrotations from a coordinate system ofreference (x , y , z).

α between x-axis and line ofnodes.

β between z-axis and Z -axis.

γ between the line of nodes andthe X -axis.

4

Page 5: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Rotation matrix

Define unit vectors called basis.

The rotation is then fully described by specifying the coordinates (scalarcomponents) of this basis in its current (rotated) position, in terms of thereference (non-rotated) coordinate axes.

The three unit vectors u, v and w which form the rotated basis each consistof 3 coordinates, yielding a total of 9 parameters. These parameters can bewritten as elements of a 3× 3 matrix A, called rotation matrix.

A =

ux vx wxuy vy wyuz vz wz

combining rotations:R = ZαXβZγ

conditions for u, v, w to be a 3D orthonormalbasis:

|u| = |v| = 1u · v = 0u× v = w

6 conditions (cross product contains 3) rotation matrix has 3 degrees of freedom

5

Page 6: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Euler axis and angle

Any rotation can be expressed as asingle rotation about some axis(Euler’s rotation theorem). The axiscan be represented as a 3D unit vectore = [ex ey ez ]

T, and the angle by a scalarθ.

r = θe

Combining two successive rotations with this representation is notstraightforward (in fact does not satisfy the law of vector addition)

6

Page 7: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Quaternions

Quaternion: normalized 4D vector: q = [q1 q2 q3 q4]T

related to axis and angle:

q1 = cos (θ/2)q2 = ex sin (θ/2)q3 = ey sin (θ/2)q4 = ez sin (θ/2)

it follows:

q21 + q2

2 + q23 + q2

4 = 1

a+ bi + cj + dk with {a, b, c , d} ∈ Rand where {1,i, j, k} are the basis(hypercomplex numbers).The following must hold for the basis

i2 = j2 = k2 = ijk = −1

which determines all the possibleproducts of i , j , and k:

ij = k, ji = −k ,jk = i , kj = −i ,ki = j , ik = −j ,

A good 3D math library of the graphics engine will have the relevant code tocarry out combinations rotations, ie, products of quaternions.

7

Page 8: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Expressing rotations in 3D as unit quaternions instead of matrices has someadvantages:

Extracting the angle and axis of rotation is simpler.

Expression of the rotation matrix in terms of quaternion parametersinvolves no trigonometric functions

Simple to combine two individual rotations represented as quaternionsusing a quaternion product

More compact than the matrix representation and less susceptible toround-off errors

Quaternion elements vary continuously over the unit sphere in R4, asorientation changes, avoiding discontinuous jumps

Interpolation is more straightforward. See for example slerp.They must sometimes be re-normalized due to rounding errors, but lowcomputational cost.

8

Page 9: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Resume

Kinematic MovementSeek

Wandering

Steering MovementSeek and Flee

Arrive

Align

Velocity Matching

Delegated SteeringPursue and Evade

Face

Looking Where You Are Going

Wander

Path Following

Separation

Collision Avoidance

Obstacle and Wall Avoidance

9

Page 10: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Steering Behaviours in 3D

Behaviours that do not change angles do not change: seek, flee, arrive,pursue, evade, velocity matching, path following, separation, collisionavoidance, and obstacle avoidance

Behaviours that change: align, face, look where you’re going, andwander

10

Page 11: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

AlignInput a target orientationOutput rotation match character’s current orientation to target’s.

q quaternion that transforms current orientation s into t is given by:

q = s−1t

s−1 = s∗ conjugate because unit quaternion (corresponds to rotate withopposite angle, θ−1 = −θ)

s =

1ijk

−1

=

1−i−j−k

q =

q1 = cos (−θ/2)q2 = ex sin (−θ/2)q3 = ey sin (−θ/2)q4 = ez sin (−θ/2)

To convert q back into an axis and angle:

θ = 2 arccos q1 e =1

2 sin(θ/2)

q2q3q4

Rotation speed: equivalent to 2D start at zero and reach θ and combinethis with the axis e.

11

Page 12: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Face and Look WYAGInput a vector (from the current character position to a target, or thevelocity vector).Output a rotation to align the vectorThat is: position the z-axis of the character in the input direction

In 2D we used θ = arctan(vx/vz) knowing the two vectors. In 3D infinitepossibilities

12

Page 13: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Align to a vector

Common assumption is to bias the target toward an orientation that is asnear to the base orientation as possible.

Start with a “base” b orientation and find rotation r through the minimumangle possible so that the local z-direction of b (zb) points along the targetvector t.

r = zb × t =(|zb||t| sin θ)er = sin θer

= [z2t3 − z3t2, z3t1 − z1t3, z1t2 − z2t1]

Since |er | = 1 then θ = arcsin |r|.

Target orientation t: turn axis and angle in a quaternion r, together withbasis quaternion b (commonly [1 0 0 0]) and compute:

t = b−1r if sin θ = 0: t =

{+b zb = zt

−b otherwise

13

Page 14: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Face in 3D� �class Face3D (Align3D):

baseOrientationtarget# ... Other data is derived from the superclass ...def calculateOrientation(vector):

# Get the base vector by transforming the z−axis by base# orientation (this only needs to be done once for each base# orientation, so could be cached between calls).baseZVector = new Vector(0,0,1) * baseOrientation # rotate vector by quaternionif baseZVector == vector: # handle case sin theta = 0

return baseOrientationif baseZVector == -vector:

return -baseOrientation

# Otherwise find the minimum rotation from the base to the targetchange = crossproduct(baseZVector, vector)angle = arcsin(change.length())axis = changeaxis.normalize()return new Quaternion(cos(angle/2), sin(angle/2)*axis.x, sin(angle/2)*axis.y, sin

(angle/2)*axis.z)

def getSteering():direction = target.position - character.position # character.velocity.normalize()if direction.length() == 0: return targetAlign3D.target = explicitTargetAlign3D.target.orientation = calculateOrientation(direction)return Align3D.getSteering()� �

14

Page 15: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Transformation of a vector v by a quaternion:

v = qvq∗ where v =

0vxvy

vz is the quaterion derived from vector

q∗ is the conjugate quaterion saw earlier

Products between quaternions:

pq =

p1q1 − piqi − pjqj − pkqkp1qi + piq1 + pjqk − pkqjp1qj + pjq1 − piqk + pkqip1qk + pkq1 + piqj − pjqi

Note: it is not commutative

15

Page 16: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Wandering

In 2D

keeps target in front of characterand turning angles low

In 3D:

3D sphere on which the target isconstrained,

offset at a distance in front of thecharacter.

to represent location of target on thesphere, more than one angle.quaternion makes it difficult tochange by a small random amount

3D vector of unit length. Update itsposition adding random amount< 1√

3to each component and

normalize it again.

16

Page 17: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

To simplify the math:

wander offset (from char to center of sphere) is a vector with only apositive z coordinate, with 0 for x and y values.

maximum acceleration is also a 3D vector with non-zero z value

Use Face to rotate and max acceleration toward target

Rotation in x–z plane more important than up and down (eg for flyingobjects) two radii

17

Page 18: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

� �class Wander3D (Face3D):wanderOffset # 3D vectorwanderRadiusXZwanderRadiusYwanderRate # < 1/sqrt(3) = 0.577 to avoid ending up with a zero vectorwanderVector # current wander offset orientationmaxAcceleration # 3D vector# ... Other data is derived from the superclass ...def getSteering():# Update the wander directionwanderVector.x += randomBinomial() * wanderRatewanderVector.y += randomBinomial() * wanderRatewanderVector.z += randomBinomial() * wanderRatewanderVector.normalize()# Calculate the transformed target direction and scale ittarget = wanderVector * character.orientationtarget.x *= wanderRadiusXZtarget.y *= wanderRadiusYtarget.z *= wanderRadiusXZ# Offset by the center of the wander circletarget += character.position + wanderOffset * character.orientationsteering = Face3D.getSteering(target)steering.linear = maxAcceleration * character.orientationreturn steering� �

18

Page 19: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Faking Rotation AxesIn aircraft, typically, rolling and pitching occur only with a turn

bring 3D only into actuators, calculate the best way to trade off pitch,roll, and yaw based on the physical characteristics. Maybe costly.

add a steering behavior that forces roll whenever there is a rotation

blending approach with the following orientation values in steering:1. keep orientation θ around the up vector as the kinematic orientation.

2. find the pitch φ by looking at the component of the vehicle’svelocity in the up direction.The output orientation has an angle above the horizon given by:

φ = arcsinv · u|v|

u is a unit vector in the up direction

3. find the roll ψ by looking at the vehicle’s rotation speed around theup direction.

ψ = arctanrk

r is the rotation, k is a constant19

Page 20: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

� �def getFakeOrientation(kinematic, speedThreshold, rollScale):speed = kinematic.velocity.length()if speed < speedThreshold:if speed == 0:return kinematic.orientation

else:fakeBlend = speed / speedThresholdkinematicBlend = 1.0 - kinematicBlend

else:fakeBlend = 1.0kinematicBlend = 0.0

yaw = kinematic.orientation # y−axis orientationpitch = asin(kinematic.velocity.y / speed) # tiltroll = atan2(kinematic.rotation, rollScale) # rollresult = orientationInDirection(roll, Vector(0,0,1))result *= orientationInDirection(pitch, Vector(1,0,0))result *= orientationInDirection(yaw, Vector(0,1,0))return result

# quaternion for rotation by a given angle around a fixed axis.def orientationInDirection(angle, axis):result = new Quaternion()result.r = cos(angle*0.5)sinAngle = sin(angle*0.5)result.i = axis.x * sinAngleresult.j = axis.y * sinAngleresult.k = axis.z * sinAnglereturn result� �

Page 21: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Outline

1. Movement in 3D

2. Pathfinding

21

Page 22: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

MotivationFor some characters, the route can be prefixed but more complex charactersdon’t know in advance where they’ll need to move.

a unit in a real-time strategy game may be ordered to any point on themap by the player at any time

a patrolling guard in a stealth game may need to move to its nearestalarm point to call for reinforcements,

a platform game may require opponents to chase the player across achasm using available platforms.

We’d like the route to be sensible and as short or rapid as possible

pathfinding (aka path planning) finds theway to a goal decided in decision making

22

Page 23: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Graph representation

Game level data simplified into directed non-negative weighted graph

node: region of the game level, suchas a room, a section of corridor, aplatform, or a small region ofoutdoor space

edge/arc: connections, they can bemultiple

weight: time or distance betweenrepresentative points or acombination thereof

23

Page 24: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Best first search

State Space SearchWe assume:

A start state

A successor function

A goal state or a goal test function

Choose a metric of bestExpand states in order from best to worst

Requires:Sorted open list/priority queueclosed listunvisited nodes

24

Page 25: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Best first search

Definitions

Node is expanded/processed when taken off queue

Node is generated/visited when put on queue

g -cost is the cost from the start to the current node

c(a, b) is the edge cost between a and b

Algorithm Measures

CompleteIs it guaranteed to find a solution if one exists?

OptimalIs it guaranteed to find the optimal solution?

Time

Space

25

Page 26: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Best-First Algorithms

Best-First Pseudo-Code� �Put start on OPENWhile(OPEN is not empty)Pop best node n from OPEN # expand nif (n == goal) return path(n, goal)for each child of n: # generate

childrenput/update value on OPEN/CLOSED

put n in CLOSEDreturn NO PATH� �

Best-First child update� �If child on OPEN, and new cost is lessUpdate cost and parent pointer

If child on CLOSED, and new cost is lessUpdate cost and parent pointer, move

node to OPENOtherwiseAdd to OPEN list� �

26

Page 27: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Search Algorithms

Dijkstra’s algorithm ≡ Uniform-Cost Search (UCS) Best-first with g -cost

Complete? Finite graphs yes, Infinite yes if ∃ finite cost path, eg, weights> ε

Optimal? yes

Idea: reduce fill nodes: Heuristic: estimate of the cost from a given state tothe goal

Pure Heuristic Search / Greedy Best-first Search (GBFS) Best-first with h-costComplete? Only on finite graphOptimal? No

A∗

best-first with f -cost, f = g + hOptimal? depends on heuristic

27

Page 28: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

TerminationWhen the node in the open list with the smallest cost-so-far has a cost-so-farvalue greater than the cost of the path we found to the goal, ie, at expansion.(like in Dijkstra)

Note: with any heuristic, when the goal node is the smallestestimated-total-cost node on the open list we are not done since a node thathas the smallest estimated-total-cost value may later after being processedneed its values revised.

In other terms: a node may need revision even if it is in the closed list ( 6=Dijkstra) because. We may have been excessively optimistic in its evaluation(or too pessimistic with the others).

(Some implementations may stop already when the goal is first visited, orexpanded, but then not optimal)

However if the heuristic has some properties then we can stop earlier:

28

Page 29: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

TheoremIf the heuristic is:

admissible, i.e., h(n) ≤ h∗(n) where h∗(n) is the true cost from n(h(n) ≥ 0, so h(G ) = 0 for any goal G )

consistent

h(n) ≤ c(n, n′) + h(n′) n′ sucessor of n

(triangular inequality holds)then when A∗ selects a node for expansion (smallest estimated-total-cost),the optimal path to that node has been found.

E.g., hSLD(n) never overestimates the actual road distance

Note:

consistent ⇒ admissible

if the graph is a tree, then admissible is enough.29

Page 30: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Consistency

A heuristic is consistent if

n

c(n,a,n’)

h(n’)

h(n)

G

n’

h(n) ≤ c(n, a, n′) + h(n′)

If h is consistent, we have

f (n′) = g(n′) + h(n′)= g(n) + c(n, a, n′) + h(n′)≥ g(n) + h(n)= f (n)

I.e., f (n) is nondecreasing along any path.

30

Page 31: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Admissible heuristicsE.g., for the 8-puzzle:

h1(n) = number of misplaced tilesh2(n) = total Manhattan distance(i.e., no. of squares from desired location of each tile)

2

Start State Goal State

51 3

4 6

7 8

5

1

2

3

4

6

7

8

5

h1(S) = 6h2(S) = 4+0+3+3+1+0+2+1 = 14

31

Page 32: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Optimality of A∗ (standard proof)

Suppose some suboptimal goal G2 has been generated and is in the queue.Let n be an unexpanded node on a shortest path to an optimal goal G1.

G

n

G2

Start

f (G2) = g(G2) since h(G2) = 0> g(G1) since G2 is suboptimal≥ f (n) since h is admissible

Since f (G2) > f (n), A∗ will never select G2 for expansion

32

Page 33: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Optimality of A∗

Lemma: A∗ expands nodes in order of increasing f value∗

Gradually adds “f -contours” of nodes (cf. breadth-first adds layers)Contour i has all nodes with f = fi , where fi < fi+1

O

Z

A

T

L

M

DC

R

F

P

G

BU

H

E

V

I

N

380

400

420

S

33

Page 34: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

A∗ vs. Breadth First Search

34

Page 35: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Properties of A∗

Complete? Yes, unless there are infinitely many nodes with f ≤ f (G )Optimal? Yes—cannot expand fi+1 until fi is finished

A∗ expands all nodes with f (n) < C∗

A∗ expands some nodes with f (n) = C∗

A∗ expands no nodes with f (n) > C∗

Time O(lm), Exponential in [relative error in h × length of sol.]l number of nodes whose total estimated-path-cost is less than that of thegoal.Space O(lm) Keeps all nodes in memory

35

Page 36: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Data Structures

Same as Dijkstra:

list used to accumulate the final path: not crucial, basic linked list

graph : not critical: adjacency list, best if arcs are stored in contiguousmemory, in order to reduce the chance of cache misses when scanning

open and closed lists: critical!1. push

2. remove

3. extract min

4. find an entry

36

Page 37: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Priority queueskeep list sorted by finding right insertion point when adding.If we use an array rather than a linked list, we can use a binary search

Priority heaps

array-based data structure which represents atree of elements.

each node has up to two children, both withhigher values.

balanced and filled from left to right

node i has children in positions 2i and 2i + 1

extract min in O(1)

adding O(log n)

find O(log n)

remove O(log n)

37

Page 38: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Bucketed Priority Queues

partially sorted data structure

buckets are small lists thatcontain unsorted items within aspecified range of values.

buckets are sorted but theircontents not

exctract min: go to the firstnon-empty bucket and search itscontents

find, add and remove depend onnumebr of buckets and can betuned.

extensions: multibuckets

38

Page 39: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Implementation DetailsData structures:

author: depends on the size of the graph with million of nodes bucketpriority list may outperform priority bufferBut see http://stegua.github.com/blog/2012/09/19/dijkstra/

Heuristics:

implemented as functions or class.

receive a goal so no code duplication

pathfindAStar(graph, start, end, new Heuristic(end))

efficiency is critical for the time of pathfindProblem background, Pattern Databases, precomputed memory-basedheuristic

Other:

overall must be very fast, eg, 100ms split in 1ms per frame

10MB memory39

Page 40: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Break ties towards states with higher g-cost

If a successor has f-cost as good as the front of OPENAvoid the sorting operations

Make sure heuristic matches problem representationWith 8-connected grids don’t use straight-line heuristic

weighted A∗: f (n) = (1− w)g(n) + wh(n)

40

Page 41: Lecture 4 Movement in 3D Path Findingmarco/Teaching/AY2014-2015/... · DM842 Computer Game Programming: AI Lecture 4 Movement in 3D Path Finding MarcoChiarandini Department of Mathematics

Node Array A∗

Improvement of A∗ when nodes are numbered with sequential integers.

Trade memory for speed

Allocate array of pointers to records for all nodes of the graph. (manynodes will be not used)

Thus Find in O(1)

A field in the record indicates: unvisited, open, or closed

Closed list can be removed

Open list still needed

41