or "How to move at the same diagonal speed as horizontally or vertically".
(As an aside, you can calculate diagonal speed by getting the square root of (horizontal speed) squared plus (vertical speed) squared. But the purpose here is to make sure that the diagonal speed equals to horizontal or vertical speed in pixels.)
Provided that horizontal speed and vertical speed are the same:
a=adjusted(desired) diagonal speed in pixels
b=horizontal speed in pixels
c=vertical speed in pixels
d=square root of ( a squared + b squared)
(* means “times” or “multiplied by”)
How I got this formula. (I may be wrong.)
The pixel is the basic building block of the image display. A pixel is a square. All sides are equal in length. Each side is one pixel-unit long. So if you move at a speed of 1 pixel either up or left or down or right the distance covered per time will be same.
But the normal diagonal length of a pixel is greater than the horizontal or vertical length. It is about 1.4 pixel-units.(The exact value is the square root of the sum of the horizontal distance squared by the vertical distance squared.) So if you move diagonally by one pixel, you cover a distance of about 1.4 units, which already exceeds the horizontal or vertical distance by about 0.4 units. The excess diagonal speed is the “about 0.4 units”.
We are still at one pixel. We assume that horizontal and vertical speed are the same since a pixel is a square. The normal diagonal speed in the pixel is about 1.4 units, as we saw. To adjust the normal diagonal speed so that it equals the horizontal or vertical speed, we must subtract the excess diagonal speed. You calculate excess speed by removing 1 pixel-unit from the diagonal 1.4 pixel-units. So at the level of one pixel, the adjusted diagonal speed will be (horizontal or vertical speed)-((normal diagonal speed)-1 pixel) , or (horizontal or vertical speed)-(excess diagonal speed).
And if there are two pixels involved, meaning that horizontal or vertical speed is 2, then the adjusted diagonal speed will be (horizontal or vertical speed)-(2*((normal diagonal speed)-1 pixel)). I put 2 in the brackets because now the excess speed is 2*0.4 . Each of the pixels has an excess diagonal speed of 0.4 and since there are two pixels, we multiplied the excess diagonal speed per pixel by 2.
If the speed involves any number of pixels, for example the horizontal/vertical speed is 10 pixels per second or step or whatever , multiply the excess diagonal speed per pixel(about 0.4 units) by the number of pixels(10 in this example) and then subtract the result from the horizontal/vertical speed(10 in this example). Preferably use the exact value of the normal diagonal speed per pixel when calculating the excess diagonal speed per pixel.
Edited by PHL, 21 July 2012 - 08:47 PM.