I've got the following code snipped :
var distanceFromDestinationSquared = (
Math.pow(this.orders.to.x - this.x, 2) +
Math.pow(this.orders.to.y - this.y, 2));
if (distanceFromDestinationSquared < Math.pow(this.radius / game.gridSize, 2)) {
this.orders = { type: "float" };
} else {
this.moveTo(this.orders.to);
}
Summary :
x and y are always integers (-1, 0, 1, 2, .. )
The x and y coordinates of the destination and starting point get subtracted from each other, then the square number is taken and eventually those results are added to each other.
this.radius / game.gridSize
gives you the equalent size related the coordinate system.
e.g game.gridSize = 20px
- thus one field in the coordinate system is equalent to 20x20px
I've used this formula with the example of (2/2) as destination and (4/4) as starting point. As result I end up at the line if (distanceFromDestinationSquared < Math.pow(this.radius / game.gridSize, 2))
with 8 < (18 / 20)²
= 8 < 0.81
What's the reason behind this apperently trivial formula of movement? Why is it important to take square number of the delta of x and y and add it together?
On one hand it avoides negative values but there would be other ways to deal more efficient with that issue.
Can someone point me to an explanation of this mathematical formula?