Steering Behaviors: Wander

Posted by: rocketman on June 16, 2010

The next steering behavior is wander. It is the next steering behavior in the series. This will use the same class. If you haven't read the previous posts, you should, especially the Seeking Behavior. These behaviors are based on Craig Reynolds’ article, Steering Behaviors For Autonomous Characters.

Wandering produces a random path. You could add a random number to the velocity each frame, but that would produce a jittery motion. This method creates a smooth and natural feeling motion. To wander, you change the velocity in small amounts each frame.

A good way to do this is to place a circle in front of the vehicle and keep the steering force on that circle. To produce the steering force, you add a random amount to the previous random number. Then constrain it to the circle. The radius of the circle is the length (or strength) of the force.

The wanderAngle property is used to produce sustained turns. If you just added a random number, you would be moving all over the place. With wanderAngle, once you start turning right, the vehicle will continue to turn right until the left force overpowers it. It looks much more natural.

All of the properties can be changed to produce the desired feel for the vehicle.

In code, it looks like this:

private var circleRadius:int = 6; //the radius of the circle
private var wanderAngle:Number = 0; //the change to the current direction. Produces sustained turned, keeps it from being jerky. Makes it smooth
private var wanderChange:Number = 1; //the amount to change the angle each frame.
//Those numbers can be changed to get other movement patterns. Those are the defaults used in the demo
public function wander():void {
var circleMidde:Vector2D = velocity.cloneVector().normalize().multiply(circleRadius); //circle middle is the the velocity pushed out to the radius.
var wanderForce:Vector2D = new Vector2D();
wanderForce.length = 3;//force length, can be changed to get a different motion
wanderForce.angle = wanderAngle;//set the angle to move
wanderAngle += Math.random() * wanderChange - wanderChange * .5;//change the angle randomly to make it wander
var force:Vector2D = circleMidde.add(wanderForce);//apply the force
velocity.add(force);//then update

Click here to see wandering in action!

Steering Behavior2DMathVector

Current rating: 4.3


  • chris quinn 3 years, 1 month ago

    Many thanks for this series of articles on Steering. Recently ordered 'the Nature of Code' - Daniel Shiffman and coming from as As3 perspective your articles are helping enormously - which I am aiming to incorporate into an as3-starling game.

    Link / Reply

New Comment

required (not published)
  • Back in July, we looked at how to use Ray Casting for collision detection. We also learned how to use the Separation of Axis Theorem. I recently had a request for a way to use the Shapes we created here with the Ray Casting method. First, lets do a quick review of the shape classes. read more
    5 years, 5 months ago
  • Today we will finally be detecting a collision with SAT. We know how SAT works, we've built classes to work with SAT, now we can use all of this to detect a real, live collision! read more
    5 years, 8 months ago
  • A few months ago, I posted on the separation of axis theorem. You can learn all about SAT and how it works here. What that post failed to do was use the SAT. We will explore using SAT for collision detection in this post. read more
    5 years, 8 months ago
  • Some of you have asked for the entire steering behavior source as well as the A* source. Here is the zip with all the classes. In there is the vehicle... read more
    5 years, 8 months ago
  • In the last three posts, we explored the how A* works, then we put A-star into code, then we looked at different heuristics for A*. Now we will combine A*... read more
    5 years, 8 months ago
RSS / Atom