1. Comparative coordinate tracking method
The tracker will constantly compare the x and Y coordinates of itself and the target, moving each x and y to a single cycle, although the algorithm is simple and easy to use, but impractical and not intelligent, if the number of trackers increases, the route will appear monotonous, because all the first diagonal to go straight, is likely to cause multiple trackers to jam together.
Code:
1 while(! (rigesha.x==killer.x&&rigesha.y==killer.y)) {2 3 if(rigesha.x>killer.x) {4Killer.goright ();5}else if (rigesha.x<killer.x){6Killer.goleft ();7 }8 9 if(rigesha.y>killer.y) {TenKiller.godown (); One}else if (RIGESHA.Y<KILLER.Y){ AKiller.goup (); - } - the}
2. Ray Dynamic Tracking method
First a line between the tracer and the target, and then through the Triangle area formula method, simulate the upstream or left a unit, distance from the line distance, compare the two distances, perform the operation of the shortest distance.
This way, the tracking route is as close to the line as possible, and the AI effect is more realistic.
In the tracking process, if the target change location is monitored (This example assumes that the target is running at every frame), the tracker will reestablish the line between the two, and then continue tracking () along that line.
Code:
1Rigeshax =rigesha.x;2Rigeshay =rigesha.y;3Killerx =killer.x;4Killery =killer.y; 7 while(! (rigesha.x==killer.x&&rigesha.y==killer.y)) {8 9 if(Random (0,1) >0.5){Ten if(rigesha.x>killer.x) {rigesha.goright ();}Else{rigesha.goleft ();} One}Else{ A if(RIGESHA.Y>KILLER.Y) {Rigesha.godown ();}Else{Rigesha.goup ();} - } -Rigesha.move =true; the } - - if(rigesha.move) { - caculaterline (); + } - + floatRightdistanse = xory (+ +killer.x,killer.y); A floatLeftdistance = Xory (---killer.x), killer.y); at floatUpdistance = Xory (++killer.x),--killer.y); - floatDowndistance = Xory (killer.x,++ (+ +)killer.y)); -killer.y--; - - if(rigesha.x<=killer.x&&rigesha.y<=killer.y) { - if(leftdistance<=updistance) { in killer.goleft (); -}Else{ to Killer.goup (); + } -Rigesha.move =false; the Continue; * } $ Panax Notoginseng if(rigesha.x>=killer.x&&rigesha.y<=killer.y) { - if(rightdistance<=updistance) { the killer.goright (); +}Else{ A Killer.goup (); the } +Rigesha.move =false; - Continue; $ } $ - if(rigesha.x>=killer.x&&rigesha.y>=killer.y) { - if(rightdistance<=downdistance) { the killer.goright (); -}Else{Wuyi Killer.godown (); the } -Rigesha.move =false; Wu Continue; - } About $ if(rigesha.x<=killer.x&&rigesha.y>=killer.y) { - if(leftdistance<=downdistance) { - killer.goleft (); -}Else{ A Killer.godown (); + } theRigesha.move =false; - Continue; $ } the the } the the voidCaculaterline () { -Rigeshax =rigesha.x; inRigeshay =rigesha.y; theKillerx =killer.x; theKillery =killer.y; About } the the floatxory (x, y) { the floatS = x*killery + Killerx*rigeshay + rigeshax*y-x*rigeshay-killerx*y-rigeshax*Killery; + if(s<0) {S =-S;} - floatLengthdi = sqrt ((killerx-rigashax) * (Killerx-rigashax) + (killery-rigashay) * (killery-Rigashay)) theDistance = s/Lengthdi;Bayi returndistance; the}
AI simple translation Tracking algorithm