Custom 8 direction movement, problems with speed
Ok, basically for the player speed, I have a global value, with a default value of 4.
Basically, my movement is working like this:
Repeat while [Player moves Right]:
-Set X Pos of Player to X Pos of Player + Speed
Then I went and did this for Up, Down, and Right directions, with the appropriate calculations. This allows the player to move around at the desired speed, however, I have noticed an anomaly. When moving at an angle, like say Up+Right, it seems like the player is moving slightly faster than when moving just Up or Right. It happens for all the 4 angle movements.
I'm a little confused by what is going on. To make things more clear, for Up movement I have:
Repeat while [Player Moves Up]:
-Set Y Pos of Player to Y Pos of Player - Speed
I have tried adjusting the speed when an angled direction is pressed like if Up+Right is pressed, then set Speed to [value], but this didn't work.
Basically, I want the player to move the same speed whether its the 4 regular directions or the 4 angles in 8 direction movement. At the moment, with what I'm doing, the player moves slightly faster while going one of the angle directions, even though I never change the Speed variable in any events.
Any ideas?
Re: Custom 8 direction movement, problems with speed
Well for the time being I just stopped trying to do a custom movement and use the built in 8 direction movement. But I'd still be interested to know in why I was seeing a speed difference.
Re: Custom 8 direction movement, problems with speed
Speed difference is a problem with the maths. If you think about it, you have the speed of 4 in the X and in the Y, which gives you a resultant hypotenuse (it makes a vector triangle) of almost 6. So yes, your player is moving at 1.5 times the speed it should be.
What you need for an accurate 8-direction movement is a bit of trigonometry. For the Y, multiply your speed by the sin of the angle...
4*sin(0)
4*sin(45)
4*sin(90)
... all the way up to 4*sin(315) in 45 degree increments. Then for the X multiply your speed by the cos of the angle:
4*cos(0)
4*cos(45)
4*cos(90)
... and so on again. That will give you an accurate movement. :)
Good luck!
Re: Custom 8 direction movement, problems with speed
Awesome! Thank you!
As you can probably tell, math isn't my strongest suit lol.
Re: Custom 8 direction movement, problems with speed
The thing is - as a pixel is square, moving one pixel up, or one pixel right, is shorter than moving one diagonally:
move right:
x = 10
y = 0
distance = Sqrt(10*10 + 0*0) = 10
move diagonal:
x = 10
y = 10
distance = Sqrt(10*10 + 10*10) = Sqrt(200) = 14,1421.. = 10 * Sqrt(2)
So if you want the correct diagonal movement, just divide the speed by Sqrt(2) (the square root of 2, or 2 to the power of 0.5).
You could do this by determining on how many axises the player moves and use that within the Sqrt() (or whatever it is in MMF):
speed = 10 / Sqrt(speed_x/speed_x + speed_y/speed_y)
if he moves on one axis, this will become Sqrt(1) = 1 and on two axises Sqrt(2) and on none Sqrt(0) = 0. Yay :D
Re: Custom 8 direction movement, problems with speed
Thinking about it, if you already have the angle, DizzyDoo's solution is a lot more elegant :P
Re: Custom 8 direction movement, problems with speed
Okay I think I got the calculations down for the angles, but I was wondering what I need to do to implement the speed in MMF.
Like before I was doing X Pos = X Pos + Speed for moving right.
So, using the calculation for moving right, I would assume that would be 4*cos(0), so would I do X Pos = X Pos + (4*cos(0))?
Or am I not understanding this correctly?
Re: Custom 8 direction movement, problems with speed
use X Pos = X Pos + (4*cos(0))
Y Pos = Y Pos - (4*sin(0))
the 4 is the speed
the 0 is the angle (right = 0, up = 90, left = 180, etc..)
Re: Custom 8 direction movement, problems with speed
Quote:
Originally Posted by Mook06
use X Pos = X Pos + (4*cos(0))
Y Pos = Y Pos - (4*sin(0))
the 4 is the speed
the 0 is the angle (right = 0, up = 90, left = 180, etc..)
Okay thanks! :)