Ok so I’ve tried that in a recursive way, changing the “return false” with a call of the same function. In some cases it simply jumps there, in other cases it prints a StackOverflowError (it goes on an infinite loop, not moving the object for some reason).
That’s the edited code:
private void moveOneStep()
{
float deltaTime = (System.currentTimeMillis() - lastUpdate);
//Work out the distance to travel
float distance = (float) Math.sqrt(Math.pow(destination.x - this.getX(), 2) + Math.pow(destination.y - this.getY(), 2));
System.out.println(distance);
//Adjust the position of the game object, factoring in deltaTime for time spent on the game loop
this.setLocation((int)(this.getX()+x_velocity*deltaTime), (int)(this.getY()+x_velocity*deltaTime));
System.out.println(this.getLocation());
//Check if you have moved past the destination by comparing new distance with the old distance
if((Math.sqrt(Math.pow(destination.x - this.getX(), 2) + Math.pow(destination.y - this.getY(), 2))) >= distance){
//Move the position to the destination to compensate for overshooting the destination
this.setLocation(destination.x, destination.y);
return;
} else {
moveOneStep();
}
}
I’ve tried making a different function for motion, and it works very well in most cases, except in some where it decides to move only in one axis then jump into the destination when it reaches a greater distance than the initial.
private void moveOneStep(double distance)
{
double magnitude=1;
//velocity ratio
double x_offset=(double)(this.getX()), y_offset=(double)(this.getY());
//offset is the distance that the ship has already moved. it adds up to see, for example, for how much traveled x should it travel y.
int sign_x = (int) Math.signum(destination.getX()-this.getX()), sign_y = (int) Math.signum(destination.getY()-this.getY());
//signs, in order to move both forward and backwards and not only backwards
boolean isMagniX=false;
//is the magnitude applied on X? else, apply on Y.
if (x_velocity>y_velocity)
{
magnitude = y_velocity/(x_velocity==0?y_velocity:x_velocity);
//calculate magnitude for y
}
else if (x_velocity<y_velocity)
{
magnitude = x_velocity/(y_velocity==0?x_velocity:y_velocity);
isMagniX=true;
//calculate magnitude for x
}
for (int i=0; i<10; i++)//for number of frames per 100 milisec
{
if (x_velocity>max_velocity)
System.err.println(x_velocity);
if (y_velocity>max_velocity)
System.err.println(y_velocity);
if (isMagniX)
{
y_offset+=1.0D*sign_y;
x_offset+=magnitude*sign_x;
//add up the offsets with magnitude for x
System.out.println("offset X:"+x_offset+"; offset Y: "+y_offset);
}
else
{
x_offset+=1.0D*sign_x;
y_offset+=magnitude*sign_y;
//add up the offsets with magnitude for y (if not needed, in which case x_velocity=y_velocity [45 degrees]: magnitude = 1 and won't affect the motion)
System.out.println("offset X: "+x_offset+"; offset Y: "+y_offset);
}
this.setLocation((int)(x_offset), (int)(y_offset));
System.out.println("not in range: "+this.getLocation());
//finally, update the object's location
if((Math.sqrt(Math.pow(destination.getX() - this.getX(), 2) + Math.pow(destination.getY() - this.getY(), 2))) >= distance)
break;
try {
Thread.sleep(10);
//wait for 10ms (10ms*10=100ms)
} catch (InterruptedException e) {
e.printStackTrace();
}
}
setPath(destination);
//reset path in case of drift
double newdistance = Math.sqrt(Math.pow(destination.getX() - this.getX(), 2) + Math.pow(destination.getY() - this.getY(), 2));
//calculate current distance
if((Math.sqrt(Math.pow(destination.getX() - this.getX(), 2) + Math.pow(destination.getY() - this.getY(), 2))) >= distance)
{
//Move the position to the destination to compensate for overshooting the destination
System.out.println((Math.sqrt(Math.pow(destination.getX() - this.getX(), 2) + Math.pow(destination.getY() - this.getY(), 2)))+" "+distance);
this.setLocation(destination.x, destination.y);
return;
}
//if it arrived, stop recursion
moveOneStep(newdistance);
//recursion
}
Neither of them works properly, so again I’m calling for help 