5. Tracking
Hunting is actually a kind of searching behavior (this sentence is familiar to me, haha, as I mentioned in the previous chapter ).
5.1
Purpose
Calculate the future goal of the target object to achieve effective pursuit
5.2
Implementation
The "future position" of the computing target, the locomotive preemptive action, and the "future goal" are waiting for the target.
5.3
Discussion
Differences between tracing and searching
, Take a look at the example
2008-05-1-compare.swf
".
5-1-compare.swf
(4.23 KB)
Downloads: 112009-12-27
Download silver 1 two
Download
(22.07 KB)
As shown in, the white locomotive moves to the right at a constant speed, and the red locomotive "finds" the locomotive in the form of seek, where the red dotted line is its route, while the blue locomotive is "pursued" by pursue, the blue dotted line is its route. in this way, the explanation is much simpler.
There are two ways to catch up with your goal. One is that you are faster than the target, and the other is to wait for him before the target.
In the first case
The maximum speed of All locomotives is the same, so this method is not discussed in this section.
Let's look at the second method and wait for it before the target. What does it mean? Just like in my example "5-1-compare.swf", the white locomotive moves right at a constant speed. to wait for it, we need to know where this "future goal" is located. we define this "future goal" as predictedtarget;
Download
(52.84 KB)
As shown in, predictedtarget should be equal to the location of the locomotive with distance. No doubt. that is, vech. position. add (distance. position); so the question below is how to calculate this distance?
The distance must be the speed multiplied by the time. The speed of the white locomotive is vech. Velocity. This is a known amount and you don't have to worry about it. The last problem is to calculate the time.
Think about how long it will take for us to escape before we get to the target? Lookaheadtime is the time when we chase him. This time is the distance between us and the target divided by our dormitory! The conversion to locomotive is as follows:
Lookaheadtime = pursuer. position. dist (vech. Position)/maxspeed;
You may ask, "the distance between pursuer and vech is not always changing. How can this problem be solved ?" Haha, it's just that my lookaheadtime is also updated in a timely manner. You can see the following code!
Public Function pursue (target: vehicle): void {
VaR lookaheadtime: Number = target. position. dist (_ Position)/_ maxspeed;
// Calculate the expected time to catch up with the target, that is, lookaheadtime.
VaR predictedtarget: vector2d = target. position. Add (target. Velocity. Multiply (lookaheadtime ));
// During this period, the target will reach the expected position at the current speed, that is, the "future goal"
Seek (predictedtarget );
// Find this "future goal"
}
In the following example
"5-1-pursue.swf"
Medium
,
I have created three objects.
,
White
Vech,
Red
Seeker
And blue
Pursuer.
Relationship between them
, Vech. Flee (seeker), Seeker. Seek (vech), pursuer. Pursue (vech ).
5-1-pursue.swf
(4.22 KB)
Downloads: 192009-12-27
Download silver 1 two
Document
Pursue
Class:
,
I will not explain it too much.
:
Package
{
Import flash. display. graphics;
Import flash. display. Sprite;
Import flash. Events. event;
Import ladeng6666.steering. vector2d;
Import ladeng6666.steering. vehicle;
Import ladeng6666.steering. targetobj;
Public class pursue extends Sprite
{
Private var vech: vehicle; // locomotive instance
Private var Seeker: vehicle; // Add a red locomotive to the seek white locomotive
Private var pursuer: Vehicle // Add the blue locomotive to pursue the white locomotive
Public Function pursue ()
{
Vech = new vehicle ();
Vech. Position = new vector2d (100,200 );
Vech. edgebehavior = vehicle. Bounce;
Seeker = new vehicle (0xff0000 );
Seeker. edgebehavior = vehicle. Bounce;
Pursuer = new vehicle (0x0000ff );
Addchild (vech );
Addchild (seeker );
Addchild (pursuer );
Addeventlistener (event. enter_frame, enterframehandler );
}
// Update the locomotive Location Based on Frame Rate
Private function enterframehandler (E: Event): void {
Vech. Flee (Seeker. position );
Vech. Update ();
Seeker. Seek (vech. position );
Seeker. Update ();
Pursuer. Pursue (vech );
Pursuer. Update ();
}
}
}
Source code of this Chapter
As follows:
Chapter5.rar
(19.76 KB)
Downloads: 352009-12-27
Download silver 1 two