#169532 - brave_orakio - Tue Jul 21, 2009 8:54 am
Alright, I just can't seem to wrap my head around this. Using pixels as a measuring value, diagonal movement is a little confusing.
If we are to move horizontally or vertically by say, 8 pixels, just add +8 to either x or y values. But if we are to move diagonally by 8 pixels, we +8 to both x and y. Now if we plot that, we will see that one side has a 90 degree angle, the other 2 has a 45 degree angle.
Now, shouldn't the angle be 60 degrees for each side to be equal? For those fellas who made games with diagonally moving objects, did you take this into account or did you leave this as is?
_________________
help me
#169533 - Cearn - Tue Jul 21, 2009 12:27 pm
brave_orakio wrote: |
Alright, I just can't seem to wrap my head around this. Using pixels as a measuring value, diagonal movement is a little confusing.
If we are to move horizontally or vertically by say, 8 pixels, just add +8 to either x or y values. But if we are to move diagonally by 8 pixels, we +8 to both x and y. Now if we plot that, we will see that one side has a 90 degree angle, the other 2 has a 45 degree angle.
Now, shouldn't the angle be 60 degrees for each side to be equal? |
Yes, but you're forgetting that the sides of triangle formed by a (8,8) movement aren't all equal. It's always a right triangle, so the diagonal is always longer than the other two sides. If you simply combine X and Y movement into diagonal movement, you're actually speeding up movement by √2. This is how things like Strafe jumping works.
Code: |
/|
8√2 / |
/ | dy=8
/______|
dx=8 |
If you don't want the speed-increase, you need to divide by √2, or use sine and cosine to get the diagonal speed.
#169535 - elwing - Tue Jul 21, 2009 2:07 pm
time to use hexagonal maps?
#169536 - sgeos - Tue Jul 21, 2009 3:16 pm
Sine, cosine, and fixed point math are wonderful things.
#169540 - Miked0801 - Tue Jul 21, 2009 9:58 pm
Think of your movement this way. When moving straight vertical or horizontal, you are moving along the Hypotenuse of a triangle with 1 side being 0 and the other being equal in length to the Hypotenuse. If you move 45 degrees, the Hypotenuse is still '8' pixels in length, but the other legs of the right triangle are shortened - in this case to 8 * sin(45), 8 * cos(45) or 5.66 each size.
#169542 - Dwedit - Wed Jul 22, 2009 12:47 am
How about just normalizing the vector? No trigonometry required.
Given some vector (for example, X=+7, Y=-11), let's say you want to make it a distance of 8.
Length of vector = sqrt(x^2+y^2)
Divide by the length, and multiply by the new desired length. Probably multiplying before dividing to keep precision. I'm sure there are some good fixed point square root and division routines out there.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."
#169560 - brave_orakio - Thu Jul 23, 2009 6:21 am
I guess that pretty much covers it. So, using pixels as a unit of measurement isn't really a good idea huh?
_________________
help me
#169561 - Dwedit - Thu Jul 23, 2009 12:40 pm
Pixels are a fine measurement unit, as long as you have a way of doing fractional amounts for diagonals.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."