By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,805 Members | 1,280 Online
Bytes IT Community
+ Ask a Question
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
  1. public void AdvanceWalkPhase()
  2.     {
  3.         int a = _target.getLocation().getX() - _location.getX();
  4.         int b = _target.getLocation().getY() - _location.getY();
  5.         int sum = a ^ 2 + b ^ 2;
  6.         if(sum < 0)
  7.         {
  8.             sum = sum * -1;
  9.         }
  10.         Steps = (int) Math.sqrt(sum);    
  11.         diffX = _target.getLocation().getX() - _location.getX();
  12.         diffY = _target.getLocation().getY() - _location.getY();
  13.         if(Steps == 0)
  14.         {
  15.             Steps = 1;
  16.         }
  17.         moveX = (int) (diffX / Steps);
  18.         moveY = (int) (diffY / Steps);
  19.         Location newLoc = new Location(_location.getX() + (int)moveX, _location.getY() + (int)moveY);
  20.         _location = newLoc;        
  21.     }
  22.  
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
Share this Question
Share on Google+
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
  1. int step = steps[level]; // where level is defined somewhere
  2.  
  3. int a = _target.getLocation().getX() - _location.getX();
  4. int b = _target.getLocation().getY() - _location.getY();
  5. double h = (int) Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); 
  6.  
  7. int xMove = (int)(a / h * step);
  8. int yMove = (int)(b / h * step);
  9.  
  10. // construct the new target point using xMove and yMove relatively of the current point
  11. Location newLoc = new Location(_location.getX() + xMove, _location.getY() + yMove);
  12.  
  13.  
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

Post your reply

Sign in to post your reply or Sign up for a free account.