lastPosition always equals current Position

I’m having the problem that is said in the title and have no idea why it’s happening. This is the code that i believe is causing the problem. There’s gotta be something weird going on with it.

public void moveTo(double x, double y){
				
		double x1 = pos.x;
		double y1 = pos.y;
		lastPos.x = x1;
		lastPos.y = y1;
		if(map.get((int)x,(int) y).walkable){
			
			this.pos.x = x;
			this.pos.y = y;
		}
}

Well you probably don’t want to update the last position unless you can actually move to the new one, and there’s no need for the x1 and y1 variables. I can’t really see what would be causing your problem but I’d change the method to something like this:


public void moveTo(double x, double y) {
   if(map.get((int)x, (int)y).walkable) {
      lastPos.x = pos.x;
      lastPos.y = pos.y;
      pos.x = x;
      pos.y = y;
   }
}

Have you tried printing all the variables before and after to see what’s actually happening? That can be helpful.

Try printing out what the values are at given breakpoints. :smiley:

I plugged in a couple of println statements for lastPos.x and it equals where i am going to move for some reason, even before i set lastPos.x = pos.x. I had the x1 and y1 in their because I thought it was some sort of pointer problem.

what are pos and lastpos? vecmath vectors?

if not, what type is x and y?

It might be a problem with the if statement. The position will never change. I think that’s your only solution here, because other than that, there’s nothing seem to be wrong.

@deepthought
yeah, they’re vectors.

@Agro
movement works fine other than the fact that the lastPos variable is always wrong.

initialize a new vector of lastpos that has the value lastx and lasty

Do you perhaps assign lastPos = pos somewhere in your code? It can happen if you´re not careful, thinking you´re copying the x,y-values of pos when you´re actually copying the reference to pos. It can cause quite a mess.