37
CSCI 4310 Lecture 5: Steering Behaviors in Raven

CSCI 4310 Lecture 5: Steering Behaviors in Raven

Embed Size (px)

DESCRIPTION

High Level Some ‘meta’ intelligence decides it is time to flee Ex: Rule: if badguy.size > me.size How do we implement fleeing?

Citation preview

Page 1: CSCI 4310 Lecture 5: Steering Behaviors in Raven

CSCI 4310Lecture 5: Steering Behaviors in Raven

Page 2: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Book Buckland Chapter 3, 7

Generating Automated Agent Behavior

Page 3: CSCI 4310 Lecture 5: Steering Behaviors in Raven

High Level Some ‘meta’ intelligence decides

it is time to flee Ex: Rule: if badguy.size >

me.size

How do we implement fleeing?

Page 4: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Background Vectors

v(6,2)

Magnitude= √(6² + 2²)= |v|= 6.32

Also called norm

Vector v(6,2)

Page 5: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Background Normalize Vectors

xN = x / |v|

xN = 6 / 6.32

yN = y / |v|

yN = 2 / 6.32

New Magnitude = 1Direction unchanged

Page 6: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Background

cos(Θ) = ay / |v|Θ = arccos (ay / |v|)ay = |v| cos (Θ)

sin(Θ) = ax / |v|Θ = arcsin (ax / |v|)ax = |v| sin (Θ)

Component Vectors

x

y Θ v

a

Page 7: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Background

Dot product of 2 vectorsu, v

u · v = |u||v| cos (Θ)normalize u, vu · v = cos (Θ)Θ = arccos (u · v)

Dot product

x

y Θ v

a

Dot product of 2 vectorsu, v

u · v = uxvx + uyvy

Angle between vectors

Page 8: CSCI 4310 Lecture 5: Steering Behaviors in Raven

RavenVector2D struct has methods for

Normalizing, Dot product, and adding, multiplying, etc. vectors

Page 9: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Seek Direct our player

toward a target position

Return a force (Vector) to a target position

Airplane navigation

Page 10: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Seek

current

target

Steering force (seek)

Desired = targetPos – vehiclePosAssume vehicle at (0,0)Desired = (5,6)Current + = Desired

desired

Page 11: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Seek Vector2D SteeringBehaviors::Seek(Vector2D TargetPos)

Vehicles contain a reference to a SteeringBehaviors

SteeringBehaviors::Seek returns a Vector2D

Returned Vector2D represents the steering force

Seek demo

Page 12: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Flee Run away from a given position

Return a force (Vector) to a target position

Opposite of seek

Modified in book p. 92 to flee only when a target is within a certain range

Page 13: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Flee

current

target

Steering force (flee)

Desired = vehiclePos – targetPosAssume vehicle at (0,0)Desired = (-5,-6)Current + = Desired

desired

Page 14: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Flee Vector2D SteeringBehaviors::Flee(Vector2D TargetPos)

Returned Vector2D represents the steering force

Flee demo

Page 15: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Arrive Vector2D SteeringBehaviors::Arrive(Vector2D TargetPos, Deceleration decel)

Arrive allows you to seek and come to a slower arrival (based on deceleration parameter)

Deceleration is an enumeration, slow=3, normal=2, fast=1

Page 16: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Arrive Just adjust speed multiplier

(magnitude) of DesiredVelocity vector to slow down

Base on distance to target Arrive demo

Page 17: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Pursuit

Intercepting a moving target Don’t just aim for the target

position Aim for where you *think* the

target will be Look-ahead directly proportional

to distance to evader and inversely proportional to speed

Another modification of seek

Page 18: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Pursuit Vector2D SteeringBehaviors::Pursuit(const Vehcile* evader)

Seek (evader->Pos() + evader->Velocity * LookAheadTime)

LookAheadTime = ToEvader.Length() /me->MaxSpeed() + evader->Speed()

Page 19: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Pursuit

currentevader

Steering force (flee)

Current + = Desired

desired

current

Pursuit Demo

Page 20: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Evade Vector2D SteeringBehaviors::Evade(const Vehcile* pursuer)

Opposite of pursuit:

Evader flees from the estimated future position.

Page 21: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Evade Flee (persuer->Pos() +

persuer->Velocity * LookAheadTime)

LookAheadTime = ToPersuer.Length() /me->MaxSpeed() + persuer->Speed()

Page 22: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Wander Steering force for a ‘random walk’ Just moving randomly is unconvincing

and erratic No one out on a walk stops and

reverses course frequently

Perlin noise – Ken Perlin (Tron) Remember our earlier statement: Randomness can make us look

smarter!

Page 23: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Wander

One solution: Project a circle in front of the agent Steer towards a target that is

constrained to move along the perimeter of this circle

Target adjusts in small increments along the perimeter

Adjust algo based on circle radius, target adjustments, circle distance

Page 24: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Wander

wander radius

wander distance

Wander Demo

Page 25: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Obstacle Avoidance Need concept of a

bounding box (or detection box)

Rather than detecting a collision on a complex boundary

Can get incrementally more detailed in collision detection

Page 26: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Obstacle Avoidance Collision detection is a

detailed endeavor Only recently

distinguished hits by location in FPS

Page 27: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Obstacle Avoidance

Detection boxlength proportionate to speed

obs

Dot product of agent (purple) vector and vector to obstacle will be positive if obs is in front – this obstacle can be ignored.

Page 28: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Obstacle Avoidance

obs obs

Extend obstacle boundary by ½width of detection box. If newboundary crosses agent vector,collision occurs at intersection ofnew boundary and detection box.

Page 29: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Obstacle Avoidance

obs

obs

Page 30: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Obstacle Avoidance Vector2D

SteeringBehaviors::ObstacleAvoidance(const std::vector<BaseGameEntity*> &obstacles)

Once an obstacle that will cause a collision is detected, need a steering force to avoid.

Just like driving: slow down and avoid

Page 31: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Obstacle Avoidance Lateral force in proportion to

obstacle’s position with respect to the agent

Braking force in proportion to distance to obstacle (force acting along opposite vector of agent)

Code omitted

Obstacle Avoidance Demo

Page 32: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Hide

Position yourself such that an obstacle is always between you and an agent

Constant (c) for distance from obstacle

c

‘Arrive’ to this position

Page 33: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Hide Vector2D SteeringBehaviors::Hide(const Vehicle*

target, vector<BaseGameEntity*>& obstacles)

For Each Obstacle Calculate A hiding position for this

obs Calculate Distance to hiding position

Is Hiding Position Available? Yes – Call ‘Arrive’ to Closest No – Evade

Hide Demo

Page 34: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Path Following Useful for creating botbehaviors such as patrolling

Used for multi-stage navigation

Very commonly used in most games Some other function determines a

goal location – more difficult Calls FollowPath to get there

Page 35: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Path Following Vector2D SteeringBehaviors::followPath( )

Just ‘seek’ each waypoint in turn Can ‘arrive’ at final destination

Path object in GameWorld class

Path class omitted

Path Following Demo

Page 36: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Group Behavior

Agents can react to other agents Multi-agent aware

From our earlier discussion of environment and agent attributes

Mimic nature Simple creatures performing complex

activities - ACO Neighborhood – pervasive concept.

What is the sphere of influence? This idea present in many AI concepts

Page 37: CSCI 4310 Lecture 5: Steering Behaviors in Raven

Steering: Flocking Combination of

Separation Alignment Cohesion

Flocking Demo