Top Banner
DM810 Computer Game Programming II: AI Lecture 2 Movement Marco Chiarandini Department of Mathematics & Computer Science University of Southern Denmark
27

DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ......

Mar 09, 2018

Download

Documents

vuongnhi
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

DM810

Computer Game Programming II: AI

Lecture 2Movement

Marco Chiarandini

Department of Mathematics & Computer ScienceUniversity of Southern Denmark

Page 2: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsOutline

1. Representations

2. Kinematic MovementSeekingWandering

3. Steering Behaviors

2

Page 3: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsOutline

1. Representations

2. Kinematic MovementSeekingWandering

3. Steering Behaviors

3

Page 4: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsMovement

Movement of characters around the level (not about movement of faces)

Input: geometric data about the state of the world + current position ofcharacter + other physical properties

Output: geometric data representing movement (velocity, accelerations)

For most games, characters have only two states: stationary + running

Running:

Kinematic movement: constant velocity, no acceleration nor slow down.

Steering behavior: dynamic movement with accelerations. Takes intoaccount current velocity of the character and outputs acceleration (eg,Craig Reynolds, flocking)

Examples: Kinematic algorithm from A to B returns direction.Dynamic/steering algorithm from A to B returns acceleration and deceleration

4

Page 5: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsStatic Representations

Characters represented as points, center of mass (collision detection, obstacleavoidance need also size but mostly handled outside of movementalgorithms).

In 2D:x, z orthonormal basis of 2D space2D movement takes place in x, z (x, z) coordinates

Orientation value θ:counterclockwise angle, in radiantsfrom positive z-axis

struct Static:position # a 2D vectororientation # single floating point value

then rendered in 3D (θ determines the rotation matrix)5

Page 6: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsStatic Representations

In 3D movement is more complicated: orientation implies 3 parameters

May be needed in flight simulators

But often one dim is gravity and rotation about the upright direction isenough, the rest can be handled by animations)

Hybrid model:

In 2 12D

full 3D position (includes possibility for jumps)

orientation as a single value

huge simplification in math in change of a small loss in flexibility

6

Page 7: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsOrientation in Vector Form

from angle θ to unit length vector in the direction that the character is facing

θ =

[sinθcosθ

]

7

Page 8: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsKinematic Representations

Kinematic algorithms:position + orientation + velocity

2D 2 12D

linear velocity v vx, vz components vx, vy, vz componentsangular velocity θ′ π/s π/s

struct Kinematic:position # 2 or 3D vectororientation # single floating point valuevelocity # 2 or 3D vectorrotation # single floating point value

Steering algorithms:return linear acceleration a and angular acceleration θ′′

struct SteeringOutput:linear # 2D or 3D vectorangular # single floating point value

8

Page 9: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsIndependent facing

Characters mostly face the direction of movement. Hence steering algs oftenignore rotation. To avoid abrupt changes orientation is moved proportionallytowards moving direction:

9

Page 10: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsKinematic Representations

Updates (classical mechanics)

v(t) = r′(t) a(t) = r′′(t)

r = vt+ 12at

2 v = atθ = θ′t+ 1

2θ′′t2 θ′ = θ′′t

struct Kinematic:positionorientationvelocityrotationdef update(steering, time):

position += velocity * time + 0.5 *steering.linear * time * time

orientation += rotation * time + 0.5 *steering.angular * time * time

velocity += steering.linear * timeorientation += steering.angular * time

struct Kinematic:positionorientationvelocityrotationdef update(steering, time):

position += velocity * timeorientation += rotation * timevelocity += steering.linear * timeorientation += steering.angular * time

Velocities expressed as m/s thus support for variable frame rate.Eg.: If v = 1m/s and the frame duration is 20ms è x = 20mm

10

Page 11: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsNetwon’s Physics

Accelerations are determined by forces and inertia (F = ma)

To model object inertia:

object’s mass for the linear inertia

moment of inertia (or inertia tensor in 3D) for angular acceleration.

We could extend char data and movement algorithms with these, but mostlyneeded for physics games, eg, driving game.

Actuation is a post-processing step that takes care of computing forces aftersteering has been decided to produce the desired change in velocity (posesfeasibility problems)

11

Page 12: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsOutline

1. Representations

2. Kinematic MovementSeekingWandering

3. Steering Behaviors

12

Page 13: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsKinematic Movement Algorithms

Input: static dataOutput: velocity (often: on/off full speed or being stationary + target

direction)

From v we calculate orientation using trigonometry:

tan θ =sin θ

cos θθ = arctan(−vx/vz)

(sign because counterclockwise from z-axis)def getNewOrientation(currentOrientation, velocity):

if velocity.length() > 0:return atan2(-static.x, static.z)

else: return currentOrientation

13

Page 14: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsSeeking

Input: character’s and target’s static dataOutput: velocity along direction to target

struct Static:positionorientation

struct KinematicSteeringOutput:velocityrotation

class KinematicSeek:character # static data char.target # static data targetmaxSpeed

def getSteering():steering = new KinematicSteeringOutput()steering.velocity = target.position - character.position # directionsteering.velocity.normalize()steering.velocity *= maxSpeedcharacter.orientation = getNewOrientation(character.orientation, steering.

velocity)steering.rotation = 0return steering

Performance in time and memory? O(1)15

Page 15: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering Behaviors

getNewOrientation can be taken out

flee mode:steering.velocity = character.position - target.position

problem: arrival must be stationary not wiggling back and forth

use large radius of satisfaction to target

use a range of movement speeds, and slow the character down as itreaches its target

16

Page 16: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering Behaviors

class KinematicArrive:charactertargetmaxSpeedradius # satisfaction radiustimeToTarget = 0.25 # time to target constantdef getSteering():

steering = new KinematicSteeringOutput()steering.velocity = target.position - character.position # directionif steering.velocity.length() < radius:return Nonesteering.velocity /= timeToTarget # set vel. wrt time to targetif steering.velocity.length() > maxSpeed:steering.velocity.normalize()steering.velocity *= maxSpeedcharacter.orientation = getNewOrientation(character.orientation, steering.

velocity)steering.rotation = 0return steering

17

Page 17: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsWander

A kinematic wander behavior moves in the direction of the character’s currentorientation with maximum speed.Orientation is changed by steering.

class KinematicWander:charactermaxSpeedmaxRotation # speeddef getSteering():

steering = new KinematicSteeringOutput()steering.velocity = maxSpeed * character.orientation.

asVector()steering.rotation = random(-1,1) * maxRotationreturn steering

Demo

19

Page 18: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsOutline

1. Representations

2. Kinematic MovementSeekingWandering

3. Steering Behaviors

20

Page 19: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsSteering, Intro

movement algorithms that include accelerations

present in driving games but always more in all games.

range of different behaviors obtained by combination of fundamentalbehaviors: eg. seek and flee, arrive, and align.

each behavior does a single thing, more complex behaviors obtained byhigher level code

often organized in pairs, behavior and its opposite (eg, seek and flee)

Input: kinematic of the moving character + target information(moving char in chasing, representation of the geometry of the world inobstacle avoidance, path in path following behavior; group of targets inflocking – move toward the average position of the flock.)

Output: steering, ie, accelerations

21

Page 20: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsVariable Matching

Match one or more of the elements of the character’s kinematic to asingle target kinematic (additional properties that control how thematching is performed)

To avoid incongruencies: individual matching algorithms for eachelement and then right combination later. (algorithms for combinationsresolve conflicts)

22

Page 21: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsSeek and Flee

Seek tries to match the position of the character with the position of thetarget. Accelerate as much as possible in the direction of the target.

struct Kinematic:positionorientationvelocityrotationdef update(steering, maxSpeed, time):position += velocity * timeorientation += rotation * timevelocity += steering.linear * timeorientation += steering.angular * timeif velocity.length() > maxSpeed:velocity.normalize()velocity *= maxSpeed

struct SteeringOutputlinear # acclerationangular # acceleration

class Seek:character # kinematic datatarget # kinematic datamaxAcceleration

def getSteering():steering = new SteeringOutput()steering.linear = target.position -

character.position #change here forflee

steering.linear.normalize()steering.linear *= maxAccelerationsteering.angular = 0return steering

Demo

if velocity exceeds the maximum speed it is trimmed back in apost-processing step of the update function.Note, orientation removed: like before or by matching or proportional

23

Page 22: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsArrive

Seek always moves to target with max acceleration. If target is standing itwill orbit around it. Hence we need to slow down and arrive with zero speed.

Two radii:

arrival radius, as before, lets the character get near enough to the targetwithout letting small errors keep it in motion.

slowing-down radius, much larger. max speed at radius and theninterpolated by distance to target

Direction as beforeAcceleration dependent on the desired velocity to reach in a fixed time (0.1 s)

24

Page 23: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsArrive

class Arrive:character # kinematic datatargetmaxAccelerationmaxSpeedtargetRadiusslowRadiustimeToTarget = 0.1 # time to arrive at targetdef getSteering(target):

steering = new SteeringOutput()direction = target.position - character.positiondistance = direction.length()if distance < targetRadius

return Noneif distance > slowRadius:

targetSpeed = maxSpeedelse:

targetSpeed = maxSpeed * distance / slowRadiustargetVelocity = directiontargetVelocity.normalize()targetVelocity *= targetSpeedsteering.linear = targetVelocity - character.velocitysteering.linear /= timeToTargetif steering.linear.length() > maxAcceleration:

steering.linear.normalize()steering.linear *= maxAcceleration

steering.angular = 0return steering

25

Page 24: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsAlign

match the orientation of the character with that of the target (just turn, nolinear acceleration). Angular version of Arrive.Issue:avoid rotating in the wrong direction because of the angular wrap

convert the result into the range (−π, π) radians by adding or subtractingm · 2π

26

Page 25: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsAlign

class Align:charactertargetmaxAngularAccelerationmaxRotationtargetRadiusslowRadiustimeToTarget = 0.1def getSteering(target):

steering = new SteeringOutput()rotation = target.orientation - character.orientationrotation = mapToRange(rotation)rotationSize = abs(rotationDirection)if rotationSize < targetRadius

return Noneif rotationSize > slowRadius:

targetRotation = maxRotationelse:

targetRotation = maxRotation * rotationSize / slowRadiustargetRotation *= rotation / rotationSizesteering.angular = targetRotation - character.rotationsteering.angular /= timeToTargetangularAcceleration = abs(steering.angular)if angularAcceleration > maxAngularAcceleration:

steering.angular /= angularAccelerationsteering.angular *= maxAngularAcceleration

steering.linear = 0return steering

27

Page 26: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsVelocity Matching

So far we matched positions

Matching velocity becomes relevant when combined with otherbehaviors, eg. flocking steering behavior

Simplified version of arrive

class VelocityMatch:charactertargetmaxAccelerationtimeToTarget = 0.1def getSteering(target):

steering = new SteeringOutput()steering.linear = target.velocity - character.velocitysteering.linear /= timeToTargetif steering.linear.length() > maxAcceleration:

steering.linear.normalize()steering.linear *= maxAcceleration

steering.angular = 0return steering

28

Page 27: DM810 Computer Game Programming II: AI - SDUmarco/DM810/Slides/dm810-lec2.pdf ·  · 2013-01-14DM810 Computer Game Programming II: AI Lecture 2 Movement ... Dynamic/steeringalgorithmfromAtoBreturnsaccelerationanddeceleration

RepresentationsKinematic MovementSteering BehaviorsDelegated Behaviors

we saw the building blocks: seek and flee, arrive, align, and velocitymatching

calculate a target, either position or orientation, and delegate thesteering

author uses polymorphic style of programming (inheritance, subclasses)to avoid duplicating code

29