Steering Behaviors: Seeking

Posted by: rocketman on June 11, 2010

Steering behaviors are used to create intelligent movements on a computer. These posts will be an AS3 version of the principles illustrated in Craig Reynolds' article located at http://www.red3d.com/cwr/steer/gdc99/.

Before we can start using steering behaviors, we need a few classes. First we will be using, yet again, the Vector2D.as class. Steering behaviors rely on vector math, so you should be familiar with it.

If you need more information on vector mathematics, you can look here:

https://users.cs.jmu.edu/bernstdh/web/common/lectures/slides_vector-math-2d.php

http://en.wikipedia.org/wiki/Vector_%28mathematics%29

Next we will need a vehicle class to hold all the vehicle's properties. The properties each vehicle will need are:



      • mass - a scalar



    • position - a Vector2D


 

    • velocity - a Vector2D


 

    • maxForce - a scalar


 

    • maxSpeed - a scalar


 

    • rotation - a scalar


 

    • update - a function to move the vehicle around


 

All it needs is some public properties(or getters and setters). I have made a class you can use here: Vehicle.as.

In order for the vehicle to move accurately, there are a couple of important rules:



      • maxForce is the maximum amount a single behavior can affect the velocity



    • maxSpeed is the fastest the vehicle can travel


 


 

    • The steeringForce(seen later) is truncated by the maxForce


 

    • Acceleration is steeringForce / mass


 

    • Velocity is acceleration + velocity truncated to maxSpeed


 

    • Then the position is updated by adding the velocity.


 

The important part of the code is the update function.

public function update():void {
// keep it witin its max speed
_velocity.truncate(_maxSpeed);
// move it
_position = _position.add(_velocity);
// keep it on screen
if(stage) {
if(position.x > stage.stageWidth) position.x = 0;
if(position.x < 0) position.x = stage.stageWidth;
if(position.y > stage.stageHeight) position.y = 0;
if(position.y < 0) position.y = stage.stageHeight;
}
// set the x and y, using the super call to avoid this class's implementation
super.x = position.x;
super.y = position.y;
// rotation = the velocity's angle converted to degrees
rotation = _velocity.angle * 180 / Math.PI;
}

Some of the rules of movement are not applied here(mainly the ones involving the steering force). That is because they are applied when the steering behavior is called(at least in my method).

Now we are ready to begin seeking!

Seeking Behavior

The Seeking Behavior


Seeking looks just like that picture. The vehicle is moving at its current velocity, the target is where it wants to end up. The desired velocity is the fastest way there. The seeking behavior results in a curve(most of the time).

It works like this:



      • desiredVelocity = position-target truncated normalized then multiplied by maxSpeed(this gets the desired velocity going to the target as fast as it can).



    • steeringForce = desiredVelocity - velocity


 

    • acceleration = steeringForce / mass


 

    • velocity += acceleration


 

    • Then the update function is called


 

This is quite simple to do using vectors. In code, it looks like this:

public function seek(target:Vector2D):void {
var desiredVelocity:Vector2D = target.subtract(position).normalize().multiply(maxSpeed); //subtract the position from the target to get the vector from the vehicles position to the target. Normalize it then multiply by max speed to get the maximum velocity from your position to the target.
var steeringForce:Vector2D = desiredVelocity.subtract(velocity); //subtract velocity from the desired velocity to get the force vector
velocity.add(steeringForce.divide(mass)); //divide the steeringForce by the mass(which makes it the acceleration), then add it to velocity to get the new velocity
}

You can put that in the Vehicle class, or extend the vehicle class. It will work as long as it has access to those properties.

Click here to see seeking in action!
One problem with the seek behavior, visible in that demo, is that once the vehicle arrives at the target, it continues to move and oscillate around it. This will be fixed in the arrival behavior.

Steering Behavior2DMathVector

Currently unrated


Comments

  • John R 4 years, 6 months ago

    Pretty nice post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. In any case I?ll be subscribing to your feed and I hope you write again soon!

    Link / Reply
  • Rocketman Development » Steering Behaviors: 1 year, 11 months ago

    [...] how do you go to a way point? Its actually simple, Seek [...]

    Link / Reply

New Comment

required
required (not published)
optional
  • 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
    3 years, 10 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
    4 years 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
    4 years 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
    4 years, 1 month 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
    4 years, 1 month ago
RSS / Atom