I’m studying the so called “steering behaviours” described by the paper writen by Craig W. Reynolds. Another good resource for more on this subject are the serie of posts found in http://rocketmandevelopment.com/category/steering/ very nice examples and pseudo code. Without to mention the awesome Java Applets found here: http://www.red3d.com/cwr/steer/
This behaviours are aimed to simulate inteligent movement of moving objects. Starting from one simple -mother of all- behaviour (“seek”), you can stack several or one into an objects moving pattern and the resulting product will look like if the object had some kind of decition making behind it’s movement. What you basically do is to apply forces to an object’s velocity, with the intention of “steer” from or to a specific point in space. At it’s core, regardles of how complicated the calculations get, the resulting product is a force vector that one should apply to one or more objects.
The most basic behaviour it’s called “Seek“, which makes the object seems like if it were following a target. You always work with vector operations.
Steps to apply the seek behaviour to an object (pseudo code, assume a Vector class available):
- Calculate the desired velocity = Vector(target.positionVector – vehicle.positionVector).normalize().multiply( vehicleMaximalSpeed );
- Calculate seek steering (the force used to make the vehicle steer) = Vector( desiredVelocityVector – vehicleVelocityVector )
- Add this vector to the current speed: CurrentSpeedVector.add( seekSteeringVector )
Now you just move the object: object.position += velocityVector. This si the basic stuff. As an extra, you can for example, scale the steering force based on some factors, like, the mass of the object, or the distance from the target to the vehicle, etc… but the basics are done in thouse 3 steps. In every other steer behaviour, the calculations will turn arround finding an imaginary ideal position for the vehicle and then, make the vehicle seek that position.
Here what you basically do is to create a path, find the closest point form the “vehicle’s position in the future” to the path, and check if the distance from that point to the vehicle is greater than some distance you decide. If it is, then that means that the vehicle will eventually leave the path’s segment in which currently is, so we “seek” the vehicle to the farthest point in that future offset to the path. In the image below, that point would be the closest point in the red lines to the path.
So you basically calculate this imaginary point and then, back to the “Seek” behaviour. So you work here with two behaviours one on top of the other. Being the “path following above” and the “seek” bellow (in order of execution).
I attempted to implement this in flash / actionscript 3 (my weapon of choise) and this is what i got (you need Flash player 10)
My implementation in flash (just for learning): https://dl.dropbox.com/u/400263/steer.swf
This is one made in java by someone else: http://www.red3d.com/cwr/steer/PathFollow.html
That’s it, more to come.