My unit teleports instead of moves, A*PathFinder Slick2d

Here’s the important bits of the unit class I use to extend Entity, the init, findPath and the method I use to move. The update method is empty and the render method just draws the sprite at it’s position. For whatever reason it just teleports the unit to the goal, it doesn’t even draw the sprite at the start of the game, just at it’s final location.


public void init(float x, float y) throws SlickException{
		Image [] mup = {new Image("data/cv_up_1.png"),new Image("data/cv_up_2.png"),new Image("data/cv_up_3.png"),new Image("data/cv_up_4.png")};
		Image [] mdown = {new Image("data/cv_dwn_1.png"),new Image("data/cv_dwn_2.png"),new Image("data/cv_dwn_3.png"),new Image("data/cv_dwn_4.png")};
		Image [] mleft = {new Image("data/cv_lft_1.png"),new Image("data/cv_lft_2.png"),new Image("data/cv_lft_3.png")};
		Image [] mright = {new Image("data/cv_rt_1.png"),new Image("data/cv_rt_2.png"),new Image("data/cv_rt_3.png")};
		Image [] midle = {new Image("data/cv_idle_1.png"),new Image("data/cv_idle_2.png"),new Image("data/cv_idle_3.png")};
		initxpos = x;
		initypos = y;
		xposition = x;
		yposition = y;
		Tile_Map = new IA_EMBLEM_MAP();
		PathFinder = new AStarPathFinder(Tile_Map,100,false, new ManhattanHeuristic(1));
		
		up = new Animation(mup, duration1, true);
		down = new Animation(mdown, duration1, true);
		left = new Animation(mleft, duration2, true);
		right = new Animation(mright, duration2, true);
		idle = new Animation(midle, duration2, true);
		sprite = idle;
	}
public Path findPath(int targetx,int targety){
		return PathFinder.findPath(this, (int)initxpos, (int)initypos, targetx, targety);
	}
public void move(boolean flag, Path p){
		for(int j=0; j<p.getLength();j++){
			Log.debug("Step" + j+ ": " + "x: " + p.getStep(j).getX() + " y: " + p.getStep(j).getY());
		}
		for(int i=1; i<p.getLength();i++){
			if(p.getStep(i).getX() > p.getStep(i-1).getX()){
				sprite = right;
	            xposition += 16f;
	            if(xposition >= xposition +16){
	            	xposition = xposition +16;
	            }
	            if(xposition >= 496){
	                    xposition = 496;
	            }
			}
			else if(p.getStep(i).getX() < p.getStep(i-1).getX()){
				sprite = left;
	            xposition -= 16f;
	            if(xposition <= initxpos - 16){
	            	xposition = initxpos - 16;
	            }
	            if(xposition <= 0){
	                    xposition = 0;
	            }
			}
			else if(p.getStep(i).getY() < p.getStep(i-1).getY()){
				sprite = up;
	            yposition -= 16f;
	            if(yposition <= 0){
	                    yposition = 0;
	            }
			}
			else if(p.getStep(i).getY() > p.getStep(i-1).getY()){
				sprite = down;
	            yposition +=  16f;
	            if(yposition >= 496){
	                    yposition = 496;
	            }
			}
			else
			{
				sprite = idle;
			}
		}
	}

And here’s the play state’s update method:


public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException 
	{
		if (played == false){
			played = true;
				unit1.move(played, p);
		}
	}

Any Idea why it could be doing this?

  1. What is the output of the debug log at line #26? i.e. is the A* routine actually generating a path? If so then…

  2. What is purpose of the loop at #28? Seems to me that the move() method runs the sprite through the entire path in one go with the result that it teleports to the end of the path - exactly what you’re seeing? Unless this is running in a separate thread shouldn’t move() be iteratively updating the position of the sprite on each call to move() then returning to the rendering loop?

i.e. you would have a class member that is the current index into the path, it would start at one, and move() would increment it to the next position and update the x/y positions, until it reaches the end of the path.

Never used Slick so this might be complete cobblers ofc :wink:

  • stride

the output of the debug log looks like this:

Mon Dec 09 11:39:24 EST 2013 DEBUG:Step0: x: 3 y: 4
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step1: x: 3 y: 5
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step2: x: 3 y: 6
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step3: x: 3 y: 7
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step4: x: 3 y: 8
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step5: x: 3 y: 9
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step6: x: 3 y: 10
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step7: x: 3 y: 11
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step8: x: 3 y: 12
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step9: x: 3 y: 13
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step10: x: 3 y: 14
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step11: x: 3 y: 15
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step12: x: 3 y: 16
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step13: x: 3 y: 17
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step14: x: 3 y: 18
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step15: x: 3 y: 19
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step16: x: 3 y: 20
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step17: x: 3 y: 21
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step18: x: 3 y: 22
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step19: x: 3 y: 23
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step20: x: 3 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step21: x: 4 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step22: x: 5 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step23: x: 6 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step24: x: 7 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step25: x: 8 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step26: x: 9 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step27: x: 10 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step28: x: 11 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step29: x: 12 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step30: x: 13 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step31: x: 14 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step32: x: 15 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step33: x: 16 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step34: x: 17 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step35: x: 18 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step36: x: 19 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step37: x: 20 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step38: x: 21 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step39: x: 22 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step40: x: 23 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step41: x: 24 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step42: x: 25 y: 24

The loop was stepping through the path, does it not need to do that? It seemed to me to be the only way to get individual steps to the method. I don’t know if there is a current step method. I know that there’s not a next step method.

If I’m understanding correctly then, in update it would be something like:

update{
move()
}

in move(){
getCurrentStep()
increment unit()
getNextStep()
}
?

Ok so I’ve gotten it to move to the target and then display that it’s reached it’s goal, but it’s moving like super fast, is there a way to slow him down? (The Logs are there for me to make sure he’s following the right path. diagonal movement is disabled). If I change the position assignments it just changes the destination and doesn’t get to the target.


public Step getCurrentStep(Path p, int index){
		Step s = null;
		if(index == 0){
			s = this.getFirstStep();
		}
		if(index > 0 && index <= p.getLength()){
		 s = this.findPath(25, 24).getStep(index);
		}
		else{
			System.out.println("We're hitting the end");
		}
		return s;
		
	}
	public Step getFirstStep(){
		Step s = this.findPath(25, 24).getStep(0);
		return s;
	}
	public Step getNextStep(Path p, int index){
		Step s = null;
		if(index < p.getLength())
			s = p.getStep(index + 1);
		else{
			System.out.println("We're at the end");
		}
		return s;
	}
	public Step getPreviousStep(Path p, int index){
		Step s = null;
		if(index > 0)
			s = p.getStep(index - 1);
		else{
			System.out.println("We're at the end");
		}
		return s;
	}
	public int incrementCurrentStep(){
		if(!(getIndex() >= this.findPath(25, 24).getLength()))
			setIndex(getIndex() + 1);
		else{
			System.out.println("I'm sorry Dave I can't do that");
			setIndex(-1);
		}
		return getIndex();
	}
	public int move(Path p,int delta){
		int state = 1;
		Step s1 = this.getCurrentStep(p, getIndex());
		Step s2 = this.getNextStep(p, getIndex());
		Log.debug("current step: " +"(" +s1.getX() + " , " + s1.getY() +")" + " next step: " + "(" +s2.getX() + " , " + s2.getY() +")");
		if(s1.getX() < s2.getX()){
			sprite = right;
            xposition += 16f;
            if(xposition > xposition  + 16){
            	xposition = xposition + 16;
            }
            if(xposition >= 496){
                    xposition = 496;
            }
		}
		else if(s1.getX() > s2.getX()){
			sprite = left;
            xposition -= 16f;
            if(xposition < xposition - 16){
            	xposition = xposition - 16;
            }
            if(xposition <= 0){
                    xposition = 0;
            }
		}
		else if(s1.getY() > s2.getY()){
			sprite = up;
            yposition -= 16f;
            if(yposition < yposition - 16){
            	yposition = yposition - 16;
            }
            if(yposition <= 0){
                    yposition = 0;
            }
		}
		else if(s1.getY() < s2.getY()){
			sprite = down;
            yposition += 16f;
            if(yposition > yposition + 16){
            	yposition = yposition + 16;
            }
            if(yposition >= 496){
                    yposition = 496;
            }
		}
		this.incrementCurrentStep();
		if(getIndex() == p.getLength()-1){
			if(this.getCurrentStep(p, getIndex()).getX() == 25 && this.getCurrentStep(p, getIndex()).getY() == 24)
				state = 2;
		}
		if(getIndex() == -1){
			setIndex(state);
		}
		return state;
	}

The update method now looks like this:


public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException 
	{
		int state = 1;
		
				state = unit1.move(p, delta);
				Log.debug("Index: " +unit1.getIndex());
				Log.debug("Step" + unit1.getIndex()+ ": " + "x: " + p.getStep(unit1.getIndex()).getX() + " y: " + p.getStep(unit1.getIndex()).getY());
		
		if(state == 2){
			sbg.enterState(state);
		}
		if(state == -1){
			System.out.println("There's been an error maaan");
		}
	}

There are two ways to slow the movement, either 1) Make your movement increments smaller or 2) Make a timer of some sort to handle the event. I recommend just using a byte/short here and just moving once every few frames. This means you’ll need to do something like this:


short moveFrame;
if(moveFrame < 255){
moveFrame++;
}else{
move();
moveFrame = 0;
}

Those are the ways I would do it based on glancing at your code momentarily.

To slow down the movement I would personally use a timer to tickby.

If I´m guessing you´re moving one tile every update. That makes your movement “fps tiles per second” so a timer to cap your movement speed would be a good thing.