Make an object follow another object slowly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Josh Argent
    New Member
    • Dec 2010
    • 28

    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:
    Code:
    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;		
    	}
    AdvanceWalkPhas e() 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.
    Last edited by Josh Argent; Jan 19 '13, 02:15 PM. Reason: Updating code
  • Anas Mosaad
    New Member
    • Jan 2013
    • 185

    #2
    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.

    Comment

    • Josh Argent
      New Member
      • Dec 2010
      • 28

      #3
      That's just about it really. I need it to walk quite slowly.

      Comment

      • Anas Mosaad
        New Member
        • Jan 2013
        • 185

        #4
        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:
        Code:
        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);
        Last edited by Anas Mosaad; Jan 20 '13, 08:11 AM. Reason: Updated a & b variables assignment

        Comment

        • Josh Argent
          New Member
          • Dec 2010
          • 28

          #5
          Thanks, that seems to work quite well.

          Comment

          • Anas Mosaad
            New Member
            • Jan 2013
            • 185

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

            Comment

            Working...