Quote:
Originally Posted by BubikolRamios
Need to know if an object moved, from x1 to x2, for how much and in which direction
For our case say that y = 0.
examples
Code:
x1 x2 expected result
1 -1 -2
1 2 1
-1 -2 -1
-1 -3 -2
6 7 1
7 6 -1
etc
The simplest way to calc that ?
|
deltaX = x2 - x1 where deltaX is the distance moved on the x axis.
Bear in mind that subtracting a negative number is the same as adding the absolute value of that number.
If you take the highlighted xample:
deltaX = x2 - x1 = -3 - (-1) = -3 + 1 = -2
If deltaX is negative (less than 0), the object moved in the negative direction on the x axis. If deltaX is positive, the object moved in the positive direction on the x axis.
If the object moves on the y axis as well, then as bobleny described, you can use Pythagoras' Theorem to solve for the straight line distance between 2 points.
distance = sqrt(deltaX^2 + deltaY^2)
In bobleny's example there needs to be a minor correction