Jump to content


Photo

How to make diagonal speed equal to horizontal


  • Please log in to reply
8 replies to this topic

#1 PHL

PHL

    GMC Member

  • GMC Member
  • 170 posts

Posted 21 July 2012 - 08:45 PM

How to make diagonal speed equal to horizontal or vertical speed
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:
If:
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)

then

a=b-(b*(d-1))
OR
a=c-(c*(d-1))

(* 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.

  • 0

#2 Chris_Devl

Chris_Devl

    The Next Gabe Newell

  • GMC Member
  • 321 posts
  • Version:GM8

Posted 22 July 2012 - 03:00 AM

I've always beleived you'd just multiply the diagonal speed by half.. Just cuz it'd take twice as long to get there moving only vertically and horizontally at a time.
  • 0

#3 Erik Leppen

Erik Leppen

    GMC Member

  • GMC Member
  • 2029 posts
  • Version:GM:Studio

Posted 22 July 2012 - 03:48 PM

Wow, you can make simple stuff complicated :confused:

Because it's very simple (for anyone who has ever heard of Pythagoras' theorem). If you move 1 horizontal and 1 vertical, you are a distance of sqrt(2) away from your starting point. So your diagonal speed is sqrt(2) times your horizontal/vertical speed if you don't compensate. So to compensate, divide each component by sqrt(2).

So if your normal speed is 5 pixels per step when moving horizontally and vertically, then when moving diagonally, the speed for each coordinate axis is 5 / sqrt(2).

Posted Image

So:
horizontal: x += 5
vertical: y += 5
diagonal: x += 5 / sqrt(2); y += 5 / sqrt(2)
That's all there is to it.

(I'm assuming horizontal and vertical speeds are equal. If they aren't, then it depends on the game what should be done with non-axis-aligned movement.)

Edited by Erik Leppen, 22 July 2012 - 03:49 PM.

  • 1

#4 psycho666

psycho666

    GMC Member

  • GMC Member
  • 832 posts
  • Version:GM8

Posted 22 July 2012 - 04:09 PM

use directions...
when you want to move up and right:
speed=5
direction=45
for moving up:
speed=5
direction=90
moving up and left:
speed=5
direction=135
... you get the idea... direction=0 is moving right...
  • 1

#5 slayer 64

slayer 64

    GMC Member

  • GMC Member
  • 3278 posts
  • Version:GM8.1

Posted 22 July 2012 - 07:58 PM

normalize the movement vector
m=point_distance(0,0,movex,movey)/desiredMoveSpeed

movex/=m
movey/=m

  • 0

#6 PHL

PHL

    GMC Member

  • GMC Member
  • 170 posts

Posted 23 July 2012 - 09:22 PM

use directions...
when you want to move up and right:

speed=5
direction=45
for moving up:
speed=5
direction=90
moving up and left:
speed=5
direction=135
... you get the idea... direction=0 is moving right...


Talking about directions, I have a quest. The directions are numbered from 0 to 360. But in the drag-and-drop action you can choose a "no direction" by clicking the square in the middle of the arrows. Which direction is that middle square? Is it 0,1,2....360 ? "To infinity and beyond" ?

Edited by PHL, 23 July 2012 - 09:23 PM.

  • 0

#7 drt_t1gg3r

drt_t1gg3r

    GMC Member

  • GMC Member
  • 3230 posts
  • Version:Unknown

Posted 23 July 2012 - 09:27 PM

selecting the middle button is like saying direction = direction
  • 0

#8 le-mec

le-mec

    GMC Member

  • GMC Member
  • 75 posts
  • Version:GM:Studio

Posted 25 July 2012 - 10:03 PM

selecting the middle button is like saying direction = direction


So you use it if you only want to change speed, but not direction?
  • 0

#9 drt_t1gg3r

drt_t1gg3r

    GMC Member

  • GMC Member
  • 3230 posts
  • Version:Unknown

Posted 26 July 2012 - 12:51 AM

if you are only going to change speed than just change the speed variable
I think the D&D menu of that function has the middle button because the function wants two arguments so there must be a solution in the first argument to say no direction change
to answer your question, yes, you can change speed in that manner but as I said before if you are only going to change the speed of an object then there is no need for the above function however there is nothing wrong with using it nor is it bad behavior.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users