455,805 Members | 1,280 Online Need help? Post your question and get tips & solutions from a community of 455,805 IT Pros & Developers. It's quick & easy.

# Make an object follow another object slowly

 P: 28 Hi, I'm writing a little 2d java zombie apocalypse game where the player moves around the screen and shoots zombies that spawn. I have done most of the logic except for the zombie AI. I need them to follow the player slowly. Right now I am using the same code for bullets as they're similar really. They both have a start and end point. The current code is: Expand|Select|Wrap|Line Numbers public void AdvanceWalkPhase()     {         int a = _target.getLocation().getX() - _location.getX();         int b = _target.getLocation().getY() - _location.getY();         int sum = a ^ 2 + b ^ 2;         if(sum < 0)         {             sum = sum * -1;         }         Steps = (int) Math.sqrt(sum);             diffX = _target.getLocation().getX() - _location.getX();         diffY = _target.getLocation().getY() - _location.getY();         if(Steps == 0)         {             Steps = 1;         }         moveX = (int) (diffX / Steps);         moveY = (int) (diffY / Steps);         Location newLoc = new Location(_location.getX() + (int)moveX, _location.getY() + (int)moveY);         _location = newLoc;             }   AdvanceWalkPhase() gets fired every game tick. This works but the zombie moves so fast that it appears to just be on top of the player and move with them. Please can someone help as I'm a little stuck on what to do. I've tried reduce the amount of times the event is fired but it just makes it jerky and it seems to stop before it reaches the player. Jan 19 '13 #1
5 Replies

 100+ P: 185 I'm not sure if I'm understanding the question right or not. I'm understanding that you don't want to make them at the same speed. For example, user walk x steps each time and you want the zombie to walk say (.98*x) steps at a time. Jan 19 '13 #2

 P: 28 That's just about it really. I need it to walk quite slowly. Jan 19 '13 #3

 100+ P: 185 I believe you should be using another way to determine the next point. Regarding the step, you may be using a fixed value for each level for the user and for the zombie. For example, user's step is 50 point in the desired direction. You should be using some lower step values (i.e. 25 point at level 1). And your method calculates the correct direction to determine the next point. Let me try to put it in some pseudo code: Expand|Select|Wrap|Line Numbers int step = steps[level]; // where level is defined somewhere   int a = _target.getLocation().getX() - _location.getX(); int b = _target.getLocation().getY() - _location.getY(); double h = (int) Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));    int xMove = (int)(a / h * step); int yMove = (int)(b / h * step);   // construct the new target point using xMove and yMove relatively of the current point Location newLoc = new Location(_location.getX() + xMove, _location.getY() + yMove);     Jan 19 '13 #4

 P: 28 Thanks, that seems to work quite well. Jan 20 '13 #5

 100+ P: 185 Perfect, I wish I'll get a chance to play that game one day. I should initiate a request for my free version now :-) Jan 21 '13 #6 