370 likes | 384 Views
CSCI 4310. Lecture 5: Steering Behaviors in Raven. Book. Buckland Chapter 3, 7 Generating Automated Agent Behavior. High Level. Some ‘meta’ intelligence decides it is time to flee Ex: Rule: if badguy.size > me.size How do we implement fleeing?. Background. Vectors. Vector v(6,2).
E N D
CSCI 4310 Lecture 5: Steering Behaviors in Raven
Book • Buckland • Chapter 3, 7 • Generating Automated Agent Behavior
High Level • Some ‘meta’ intelligence decides it is time to flee • Ex: Rule: if badguy.size > me.size • How do we implement fleeing?
Background • Vectors Vector v(6,2) v(6,2) Magnitude = √(6² + 2²) = |v| = 6.32 Also called norm
Background • Normalize Vectors New Magnitude = 1 Direction unchanged xN = x / |v| xN = 6 / 6.32 yN = y / |v| yN = 2 / 6.32
Background Component Vectors v y Θ x a cos(Θ) = ay / |v| Θ = arccos (ay / |v|) ay = |v| cos (Θ) sin(Θ) = ax / |v| Θ = arcsin (ax / |v|) ax = |v| sin (Θ)
Background Dot product of 2 vectors u, v u · v = uxvx + uyvy Dot product v y Θ x a Dot product of 2 vectors u, v u · v = |u||v| cos (Θ) normalize u, v u · v = cos (Θ) Θ = arccos (u · v) Angle between vectors
Raven Vector2D struct has methods for Normalizing, Dot product, and adding, multiplying, etc. vectors
Steering: Seek • Direct our player toward a target position • Return a force (Vector) to a target position • Airplane navigation
Steering: Seek current target desired Steering force (seek) Desired = targetPos – vehiclePos Assume vehicle at (0,0) Desired = (5,6) Current + = Desired
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
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
Steering: Flee current target Steering force (flee) desired Desired = vehiclePos – targetPos Assume vehicle at (0,0) Desired = (-5,-6) Current + = Desired
Steering: Flee • Vector2D SteeringBehaviors:: Flee(Vector2D TargetPos) • Returned Vector2D represents the steering force • Flee demo
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
Steering: Arrive • Just adjust speed multiplier (magnitude) of DesiredVelocity vector to slow down • Base on distance to target • Arrive demo
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
Steering: Pursuit • Vector2D SteeringBehaviors:: Pursuit(const Vehcile* evader) • Seek (evader->Pos() + evader->Velocity * LookAheadTime) LookAheadTime = ToEvader.Length() / me->MaxSpeed() + evader->Speed()
Steering: Pursuit current evader current Steering force (flee) desired Current + = Desired Pursuit Demo
Steering: Evade • Vector2D SteeringBehaviors:: Evade(const Vehcile* pursuer) • Opposite of pursuit: • Evader flees from the estimated future position.
Steering: Evade • Flee (persuer->Pos() + persuer->Velocity * LookAheadTime) LookAheadTime = ToPersuer.Length() / me->MaxSpeed() + persuer->Speed()
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!
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
Steering: Wander Wander Demo wander radius wander distance
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
Steering: Obstacle Avoidance • Collision detection is a detailed endeavor • Only recently distinguished hits by location in FPS
Steering: Obstacle Avoidance Dot product of agent (purple) vector and vector to obstacle will be positive if obs is in front – this obstacle can be ignored. obs Detection box length proportionate to speed
obs Steering: Obstacle Avoidance Extend obstacle boundary by ½ width of detection box. If new boundary crosses agent vector, collision occurs at intersection of new boundary and detection box. obs
obs Steering: Obstacle Avoidance obs
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
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
Steering: Hide • Position yourself such that an obstacle is always between you and an agent • Constant (c) for distance from obstacle ‘Arrive’ to this position c
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
Steering: Path Following • Useful for creating bot behaviors 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
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
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
Steering: Flocking • Combination of • Separation • Alignment • Cohesion • Flocking Demo