Download presentation
Presentation is loading. Please wait.
2
Sony Computer Entertainment
Steering Behaviours Robin Green Sony Computer Entertainment
3
Definitions Vehicle Behaviour
The abstract representation of a moving object. Behaviour Code that generates a steering force each iteration.
4
The Vehicle Object
5
The Vehicle Object A vehicle is represented in this tutorial by a circle in 2D space or sphere in 3D. Diagrams will use triangles or circles (with correct extent) with velocity vector.
6
The Vehicle Object A vehicle must have: Optional extras: position
velocity radius set of behaviours maximum force maximum velocity Optional extras: mass orientation local reference frame target references formation position
7
A Basic Vehicle class Vehicle { public: void update(); public: BehaviourList list; float mass; Vector2d pos; Vector2d vel; float max_force; float max_speed; };
8
Integration Euler Integration Not accurate, not elegant but fast.
9
Clamping Forces Clamp the vector length of forces to max_force and max_speed.
10
Updating The Vehicle The ratio of max_force to max_speed will control
1. Time it takes to get to max_speed 2. Turning circle radius (rate of change of direction) 3. Stopping speed (rate of change of speed) 4. Accuracy of hitting targets
11
Updating The Vehicle void Vehicle::update() { Vector2d force(0,0); BehaviourList::iterator i; for(int i=list.begin(); i!=list.end(); ++i) { force += i->calculate(); } Vector2d steering = truncate(force,max_force); Vector2d acc = steering / mass; vel += truncate(vel + acc, max_speed); pos += vel; }
12
Behaviours
13
Behaviours Seek & Flee Pursue & Evade Arrival Offset Pursuit
Formations Obstacle Avoidance Separation Follow Path Avoid Walls Wander Follow Flow Field Interpose & Hide Collision Avoidance Flocking
14
Designing Behaviours Generate a desired velocity
Calculate from the current state where the vehicle should be going. Calculate the steering force Calculate the difference between current and desired velocities: steering = desired - vel;
15
Seek & Flee Seek Flee vel pos target desired = normalise(target-pos) * max_speed; steering = desired - vel; steering steering desired = normalise(pos-target) * max_speed; steering = desired - vel;
16
Pursue & Evade Predict target position Seek that position
Maybe using distance to target and time to turn. Seek that position predicted predict = target.pos time * target.vel; desired steering steering desired = normalise(predict - pos) * max_speed; steering = desired - vel;
17
Arrival How fast do we need to go to get to the target in decel_time units of time? steering dist = length(target-pos); speed = min(distance/decel_time, max_speed); desired = normalise(target-pos) * speed; steering = desired - vel; desired max_speed <max_speed
18
Offset Pursuit Move through a point adjacent to a target
Find target in local space. Convert target to world space. Seek target position.
19
Formation Seek a position defined by a Leader’s local-space formation.
Find target in Leader’s local space. Convert target to world space. Seek target position.
20
Wander Causes vehicles to follow a smoothed random path.
In local space, construct a circle in front of the vehicle. Pick a target point on the circle. Add a random vector to the target position, then reproject the target onto the circle. Project the target into world space. Seek the target point.
21
Separation Force vehicles to use space more realistically.
Search world for neighbours within a search radius. For each neighbour, calculate and accumulate a 1.0/distance2 repelling force. Truncate resulting vector to max_force
22
Avoid Obstacle Keep a box in front of the vehicle free of obstacles.
Search world for collision candidates. Project their positions into local space. In local space, choose closest obstacle to steer away from. Generate lateral steering force. Project steering force back into world space.
23
Avoid Obstacle Tweaks In addition:
Scale length of box w.r.t. velocity (anticipating obstacles). Scale repelling force w.r.t. distance from vehicle (steering more to avoid close obstacles). Add small negative amount of velocity proportional to distance from vehicle (vehicle brakes to steer).
24
Follow Path Three strategies for path following: 1. Dog On A String
2. Reprojections 3. Grappling Hooks.
25
Parts Of A Path A path is a polyline of “waypoints”.
Each line between waypoints is a “path segment”. A collection of path segments is a “subpath” subpath destination waypoint current
26
Dog On A String Linearly interpolate a target point down the polyline.
Measure distance from vehicle to target to calculate the target increment. Seek the target point.
27
Reprojection Calculate the vehicle’s position in the near future.
Project that future position onto the path. Use “closest point on line to point” calculation. Seek that projected position. In effect keeping the vehicle within a wide stroke path.
28
Grappling Hook Starting with the first waypoint, keep a note of the “current waypoint”. Seek the current waypoint. If distance to current < threshold, increment current. NOTE: Only works on line-of-sight paths.
29
Avoid Walls Predict the vehicle’s future position.
If future pos intersects a wall Reproject the future position onto the wall surface along the surface normal. Extend this reprojection vector. Seek the target point. Else return no steering vector.
30
Follow Flow Field Predict your vehicle’s position forward in time.
Look up the desired velocity from the flow field map. You may like to bilinear filter the value from surrounding samples for smoothness. steering = flow_field[x,y] - vel;
31
Interpose & Hide Interpose Hide
Seek a target halfway along the vector between two objects. Hide Pick a point inside the occluding object. Seek a target extended beyond that point.
32
Unaligned Collision Avoidance
One of the more complicated behaviours. Picture two groups of vehicles passing through each other at a crossroads, each vehicle avoiding collisions. Three parts to this behaviour: 1. Neighbour search. 2. Find Closest Approach. 3. Take Evasive Action
33
Unaligned Collision Avoidance
1. Neighbour Search. Important to set an upper bound on the search area. Vehicles probably won’t be travelling in a straight line for too long. Search returns a set of “collision candidates” for further tests Ideal search area (assuming constant velocity) Approximated search area.
34
Unaligned Collision Avoidance
2. Find closest approach Find the one candidate that will collide soonest assuming constant velocity. p v p v p = B.pos - A.pos v = B.vel - A.vel a = v.p b = v.v time = - a / b dist_sq = p.p + 2*a*time + b*time*time p |v| = 0 p v
35
Unaligned Collision Avoidance
3. Take Evasive Action Vehicles can speed-up/brake and they can turn to avoid. How to choose? Avoiding Strategy: If one vehicle has extra velocity, speed up. If vehicles both are at max_speed, choose one to brake and turn. If distance > threshold, both vehicles turn. etc... Steering to avoid collision
36
Flocking 1. Separation 2. Cohesion 3. Alignment
The “Boids” flocking algorithm is comprised of three concurrent behaviours: 1. Separation 2. Cohesion 3. Alignment
37
Separation Covered earlier.
The 1/r2 repelling force isn’t intrinsic and can be replaced with a different scaled force for interesting effects.
38
Cohesion Vehicles in the same area tent towards the mean point.
This forces vehicles on the outside to bind together into flocks rather than fly off independently. For each vehicle, search for neighbours, calc. the mean position and seek that point.
39
Alignment Vehicles in the same area tend to face the same direction.
This forces flocks to swirl as direction changes propogate through the flock. For each vehicle, search for neighbours, calc. the mean facing direction, add in correcting force.
40
Combining Behaviours
41
Types of Behaviour Constant Forces Occasional Forces
Behaviours that produce a force every iteration e.g. Follow Path, Follow Flow Field, Wander Occasional Forces Behaviours that produce forces occasionally e.g. Separation, Avoid Walls, Avoid Obstacle
42
Ordering Behaviours Often, behaviours should be ordered in importance.
The ordering used in DK2 was: 1. Vehicles MUST NOT intersect walls. 2. Vehicles SHOULD NOT intersect obstacles. 3. Vehicles should TRY NOT to intersect each other. How to code this as an expression?
43
Weighted Average Take the force vectors from N behaviours, scale them and sum the result. void Vehicle::next_step() { Vector2d steering(0,0); BehaviourList::iterator i; for(i = behaviour_list.begin(); i != behaviour_list.end(); ++i) { steering += i->calculate_force(*this) * i->behaviour_scale; } … }
44
Weighted Average Problems
It’s expensive. Calculate every behaviour for every vehicle each iteration. When to truncate to max_speed? Truncation needed to allow important forces to swamp unimportant ones. Scaling forces leads to undesirable effects. Scaling path motion forces forces means vehicles movea long paths more slowly than you thought. How big should the scaling be? Do scale factors sum to 1.0? How big does a force need to be to swamp all other forces? Trial and Error.
45
Prioritised Dithering
Generate a random value X. If X < some threshold, execute the first behaviour. Else execute one of the remaining behaviours until a non-zero force is returned.
46
Prioritised Dithering
void Vehicle::calc_steering() { Vector2d steering(0,0); if(random() < 0.65f) steering = containment(this); if(steering.is_zero()) steering = avoid_obstacle(this); if(steering.is_zero()) steering = follow_path(this); … }
47
Prioritised Dithering Problems
Continuous Behaviours MUST be randomised. If they’re not randomised, they will block lower behaviours by always returning a value. How to design the ordering is unclear. Which behaviours need to be executed first? What thresholds are needed to ensure they get priority? Trial and error.
48
Gotchas
49
Extent Problems Path Finding doesn’t take extent into account
Paths intersect corners and along the edges of walls. Behaviours had to be robust enough to handle these cases.
50
Extent Problems Density of objects on the map was often unrealistic.
Large creatures couldn’t fit between large obstacles but still had to move realistically. Careful attention paid to cancelling out and truncating forces correctly.
51
Extent Problems Large obstacles were missed in neighbourhood searches.
Vehicles seemed to suddenly “remember” that they were intersecting a large obstacle. Return neighbours based on the cells they intersect, not just the cell they are created in.
52
Changing World Problems
Creating obstacles dynamically can trap small vehicles. Corner columns in rooms would trap imps who were re-enforcing walls. Vehicles had an “escape stuck mode” to enforce several iterations of ignoring obstacles.
53
AI Controls for Path Following
The AI has additional information on why a vehicle is going to a target. Vehicle needs to run an animation when it arrives Vehicle is going to be redirected when it arrives (pass through a target point) Vehicle is going to interact with an obstacle. Additional information is needed to help path following meet these goals.
54
AI Controls for Path Following
Extra flags were used: Arrive With Velocity - use seek for final waypoint. Arrive And Stop - use arrive for final waypoint. Ignore Final Obstacles - don’t use repel_obstacle for final. Repel Final Obstacles - use repel_obstacle for final seg. Ignore Final Creatures - don’t use separation for final seg. Repel Final Creatures - use separation for final path seg. Accurate Positioning - linearly interpolate along final segment. Loose Positioning - use arrive + threshold for final seg.
55
The End
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.