Forum Games [AI] Walking toward a player.

Discussion relating to game development with Flash

[AI] Walking toward a player.

Postby Toby » January 30th, 2010, 9:09 pm

Okay,
I've just got my zombie going on everyone's screens and they now walk toward the player. Plan is, the setTarget function will find a path to the player using some "nodes" placed around the map. Then when the zombie is on the last node (the player) he will walk directly toward the player.

I have this setting for the zombie to walk toward the player:
Code: Select all
public function setTarget(target:MovieClip){
   endTarg = target;
        //Path-finding to go here.
   curTarg = target;
   var xd:Number = x - curTarg.x;
   var yd:Number = y - curTarg.y;
   var theta:Number = Math.atan2(yd, xd) * (180/Math.PI);         
   xSpeed = 0.1 * Math.cos(theta);
   ySpeed = 0.1 * Math.sin(theta);
}

This works fine. However, because the player is constantly moving, I have to continually update the target:
Code: Select all
if(curTarg == endTarg){
       setTarget(curTarg);
}


Here's the onEnter function (t is getTimer()):
Code: Select all
public function Tick(t){
         if(curTarg == endTarg){
            setTarget(curTarg);
         }
         var diff = t-lastUpdate;
         var x2Speed:Number = diff*xSpeed;
         var y2Speed:Number = diff*ySpeed;
         
         x += x2Speed;
         y += y2Speed;
         lastUpdate = t;
      }

It works fine if the player walks parallel to the vector joining the two. The problem is, if you move any other direction the zombie will spin around like a loon until you stop, he will then proceed toward you.

Any ideas?
User avatar
Toby
 
Posts: 86
Joined: January 14th, 2010, 4:01 pm

Re: [AI] Walking toward a player.

Postby Cyclone103 » January 30th, 2010, 9:45 pm

Double check that the zombies are sober before inserting them into gameplay?


On a serious note, do you have any idea what could be causing that? That's an amusing glitch.
Cyclone103
 
Posts: 155
Joined: January 18th, 2010, 6:47 pm

Re: [AI] Walking toward a player.

Postby Benjaminsen » January 30th, 2010, 11:07 pm

Thats some pretty interesting behavior. The explanation for it is a bit complex so I won't attempt it now. However your problem is that you swapped the argument order for your x and y offsets. The result of this is that instead of moving towards your figure the zombie will move away.

There are two ways to fix this, either swap the arguments or add PI to your angle. I expect you attempted the later but got it slightly wrong. Adding PI to the angle is however a waste of cpu cycles so here is the code as it should look.

Code: Select all
public function setTarget(target:MovieClip){
   endTarg = target;
        //Path-finding to go here.
   curTarg = target;
   var xd:Number = x - curTarg.x;
   var yd:Number = y - curTarg.y ;
   var theta:Number = Math.atan2(yd, xd)
   xSpeed = 0.1 * Math.cos(theta);
   ySpeed = 0.1 * Math.sin(theta);
}
Benjaminsen
.IO
 
Posts: 1444
Joined: January 12th, 2010, 11:54 am
Location: Denmark

Re: [AI] Walking toward a player.

Postby Toby » January 31st, 2010, 12:01 am

Was the removal of deg => radians part intentional?

I can't seem to get it to work with or without, I get the feeling something else is bork'd here. :D
User avatar
Toby
 
Posts: 86
Joined: January 14th, 2010, 4:01 pm

Re: [AI] Walking toward a player.

Postby Toby » January 31st, 2010, 1:53 am

Updated first post with the enterFrame function. These are the only three things which affect movement...
User avatar
Toby
 
Posts: 86
Joined: January 14th, 2010, 4:01 pm

Re: [AI] Walking toward a player.

Postby 404 » January 31st, 2010, 2:35 am

Code: Select all
public function setTarget(target:MovieClip){
   endTarg = target;
        //Path-finding to go here.
   curTarg = target;
   var xd:Number = x - curTarg.x;
   var yd:Number = y - curTarg.y;
   var theta:Number = Math.atan2(yd, xd);         
   xSpeed = -0.1 * Math.cos(theta);
   ySpeed = -0.1 * Math.sin(theta);
}


fix'd
404
 
Posts: 3
Joined: January 31st, 2010, 2:13 am

Re: [AI] Walking toward a player.

Postby Toby » January 31st, 2010, 2:37 am

/facepalm
User avatar
Toby
 
Posts: 86
Joined: January 14th, 2010, 4:01 pm

Re: [AI] Walking toward a player.

Postby Benjaminsen » January 31st, 2010, 10:00 am

This is the file I used to test with.

At the same time I suggest that you do not use the elements x/y properties directly. The reason for this is that Display-Objects ignore movements smaller than 0.05 giving you some nasty precision errors over time.
Attachments
move.fla.zip
Working move test
(5.58 KiB) Downloaded 559 times
Benjaminsen
.IO
 
Posts: 1444
Joined: January 12th, 2010, 11:54 am
Location: Denmark

Re: [AI] Walking toward a player.

Postby 404 » January 31st, 2010, 11:40 am

Also, here is a function that does a similar thing while completely avoiding all those tricky sin/cos/atan2. It might also run faster because it doesn't access that Math class, I don't know that for sure though.

It's in as2, because it's from something I was working on a few months back and I'm too lazy to convert it to as3:

Code: Select all
function obGo(o1,o2,s) {
   var xd = o1._x - o2._x;
   var yd = o1._y - o2._y;
   
   var d = Math.sqrt(xd*xd+yd*yd);
   
   if (d < s) {
      o1._x = o2._x;
      o1._y = o2._y;
   } else {
      var d2 = d-s;
      var p = d2/d;
      o1._x = o2._x + xd*p;
      o1._y = o2._y + yd*p;
   }
}


o1,2 = object 1, 2
s = speed
404
 
Posts: 3
Joined: January 31st, 2010, 2:13 am

Re: [AI] Walking toward a player.

Postby Toby » January 31st, 2010, 2:08 pm

404,
I can't help but feel I read somewhere that Math.sqrt is the most costly of all the functions to run. Though I do quite a few of the sin/cos/tan functions so it may balance out.
User avatar
Toby
 
Posts: 86
Joined: January 14th, 2010, 4:01 pm


Return to Games



cron