This member has provided no bio about themself...

Report RSS Steering Behaviors

Posted by on

What’s up everyone? Hope all is well. I know I haven’t updated in a while, but don’t fret, I am still alive and kicking. You may be wondering why I named this post Steering Behaviors. For quite a long time I sought out a way to create an algorithm that would simulate heat-seeking missiles. While the past version of it did work, it was not realistic. I had my work set out for me since my skills in Math and Physics were in the days of the past. It was time to research.

The Original Algorithm

I was pretty excited when I got this working. The concept was pretty simple and in some ways sort of easy. When a missile would launch from the player it set the target location x and y coordinate and off it went. Each frame update of the game would monitor the target’s current x and y coordinate, rotate, and move accordingly. It was pretty awesome seeing watching this work. In some of the older videos that I have made show it in action.

There was only one thing that I didn’t like and that was the simple fact that when the missile would launch from the player it would make a bee-line trajectory towards its target. The intended behavior that I really wanted was for it to launch out of the ship at a constant rate of acceleration and then it would start to seek out its target when it reached the desired altitude. I tried many hacks, and while some worked (in a way), it was not the sort of behavior that I was “seeking” :).

The New Seek Algorithm

I took to the web to research ways that I could implement the functionality that I really wanted. I then stumbled across Craig Reynolds 1999 paper on Steering Behaviors for Autonomous Characters. This was exactly what I needed. It was the exact functionality I sought out. There was a lot of sample code out there that implemented it, but it didn’t directly related to Objective-C. Before even translating the multiple examples on the web, I needed to understand exactly what it was this algorithm was doing. Here is some sample code in Objective-C for the steering algorithm:


- (void) seek : (CFTimeInterval) dt toTarget : (CGPoint) target
{
    /* DESIRED VECTOR */
    CGPoint desired     = [Vector2D normalize:[Vector2D offset:self.position withTarget:target]];
    desired             = [Vector2D multiply:desired withScalar:self.maxSpeed];
    
    /* STEERING */
    CGPoint steering    = [Vector2D subtract:self.velocity from:desired];
    steering            = [Vector2D limit:steering withMax:self.maxForce];
    
    self.acceleration   = [Vector2D add:self.acceleration to:steering];
    
    self.velocity       = [Vector2D add:self.velocity to:self.acceleration];
    self.velocity       = [Vector2D limit:self.velocity withMax:self.maxSpeed];
    
    
    /* POSITION */
    self.position       = [Vector2D add:CGPointMake(self.velocity.x * dt, self.velocity.y * dt) to:self.position];
    
    /* FACING */
    self.zRotation      = [Vector2D angle:self.velocity];
    self.zRotation      = DegreesToRadians(ceilf(RadiansToDegrees(self.zRotation)));
    
    return ;
}


What my original algorithm was missing was the steering component and acceleration. The velocity was completely computed by the desired vector at a set maxSpeed. Essentially the missile would launch at full speed and make a bee-line directly toward the target. This solution accelerates at a slower pace while limiting the velocity to max speed and limiting the steering force by a max force. I was pretty amazed at how awesome this worked. I would definitely suggest reading that paper I linked above and search for other steering examples that he has proposed in his paper. What would all this jargon be without an example? Well, take a look at this video and in the very beginning you will see me set the target. Watch how the missile travels upwards and then comes back down seeking the target location:

Have a great day all!

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: