473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Make an object follow another object slowly

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
5 7101
Anas Mosaad
185 128KB
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
That's just about it really. I need it to walk quite slowly.
Jan 19 '13 #3
Anas Mosaad
185 128KB
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
Thanks, that seems to work quite well.
Jan 20 '13 #5
Anas Mosaad
185 128KB
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

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

Similar topics

6
by: lawrence | last post by:
How dangerous or stupid is it for an object to have a reference to the object which contains it? If I have a class called $controllerForAll which has an arrray of all the objects that exist, what...
5
by: Diez B. Roggisch | last post by:
Hi, I've got code objects that are created from strings at runtime that look like this: def p_op1(_, args): _.add_dependency('p_op1') As you migth guess, p_op1 is supposed to become an...
3
by: Stefan | last post by:
Hi, I cannot make my COM Object external, so I can access it from other ..cpp project files. Here is a bit of the code: (globals.cpp) .... #include "globals.h" #include <oleacc.h> #include...
2
by: Marko | last post by:
Hi, There are few properties that don't follow the traditional 'by handle' values. For example: System::Runtime::Serialization::Formatter ^f = ...;...
23
by: digitalorganics | last post by:
How can an object replace itself using its own method? See the following code: class Mixin: def mixin(object, *classes): NewClass = type('Mixin', (object.__class__,) + classes, {}) newobj =...
3
by: Del Cagle | last post by:
Ok this works great, but which part of it can I change to make the "Average of the 10" stay with the current record. These averages are currently "logged" by hand, if I pull the records into a...
4
by: karen987 | last post by:
I have a column in a table, which draws out info from a database. The column is 150px wide. I want it to remain this wide, even if one word is longer. What happens now is that if i put in a web...
2
by: Ryan Liu | last post by:
In C#, what's is the difference between object vs Object, exception vs Exception? Thanks, ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~. Ryan Liu Shanghai Fengpu Software Co. Ltd
5
by: =?Utf-8?B?UGhpbCBKb2huc29u?= | last post by:
Hi, I'm aware of the differences between string and String, but wondered what the differences were between object and Object? -- Regards, Phillip Johnson (MCPD Enterprise Developer)...
9
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I am just starting to write some code in C#. One time I accidently used "object" instead of "Object". The program didn't complain at all and worked fine. I am just wondering is there...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.